forked from wpengine/faustjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental App Router: Login server action (wpengine#1562)
- Loading branch information
1 parent
4a20f9b
commit b2ad517
Showing
19 changed files
with
945 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
'@faustwp/experimental-app-router': patch | ||
--- | ||
|
||
Added the `onLogin` server action to login a user: | ||
|
||
```tsx | ||
import { onLogin } from '@faustwp/experimental-app-router'; | ||
|
||
<form action={loginAction}> | ||
<fieldset> | ||
<label htmlFor="usernameEmail">Username or Email</label> | ||
<input type="name" name="usernameEmail" /> | ||
</fieldset> | ||
|
||
<fieldset> | ||
<label htmlFor="password">Password</label> | ||
<input type="password" name="password" /> | ||
</fieldset> | ||
|
||
<button type="submit">Login</button> | ||
</form>; | ||
``` |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { onLogin } from '@faustwp/experimental-app-router'; | ||
import { redirect } from 'next/navigation'; | ||
|
||
export default async function Page() { | ||
async function loginAction(formData: FormData) { | ||
'use server'; | ||
|
||
const res = await onLogin(formData); | ||
|
||
if (res.error) { | ||
/** | ||
* @TODO Next.js is still working on ways persisting error messages from | ||
* server actions to the client. | ||
* | ||
* "Displaying loading or error states currently requires using | ||
* Client Components. We are exploring options for server-side functions | ||
* to retrieve these values as we move forward in stability for Server Actions." | ||
* | ||
* @link https://nextjs.org/docs/app/building-your-application/data-fetching/forms-and-mutations#error-handling | ||
*/ | ||
console.error(res.error); | ||
} else { | ||
redirect('/gated-content'); | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<h2>Login</h2> | ||
|
||
{/* @ts-ignore */} | ||
<form action={loginAction}> | ||
<fieldset> | ||
<label htmlFor="usernameEmail">Username or Email</label> | ||
<input type="name" name="usernameEmail" /> | ||
</fieldset> | ||
|
||
<fieldset> | ||
<label htmlFor="password">Password</label> | ||
<input type="password" name="password" /> | ||
</fieldset> | ||
|
||
<button type="submit">Login</button> | ||
</form> | ||
</> | ||
); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
/** @type {import('next').NextConfig} */ | ||
export default { | ||
appDir: true, | ||
experimental: { | ||
serverActions: true, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
"esnext" | ||
], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"incremental": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
] | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
".next/types/**/*.ts", | ||
"**/*.ts", | ||
"**/*.tsx" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
Oops, something went wrong.