Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c5d470a4
Unverified
Commit
c5d470a4
authored
Jan 25, 2024
by
Lee Robinson
Committed by
GitHub
Jan 25, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
docs: fix Auth0 typo in authentication (#61171)
parent
e6763488
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
52 deletions
+40
-52
index.mdx
.../01-building-your-application/09-authentication/index.mdx
+39
-51
use-router.mdx
docs/03-pages/02-api-reference/02-functions/use-router.mdx
+1
-1
No files found.
docs/02-app/01-building-your-application/09-authentication/index.mdx
View file @
c5d470a4
...
@@ -164,32 +164,18 @@ export default async function handler(req, res) {
...
@@ -164,32 +164,18 @@ export default async function handler(req, res) {
<
AppOnly
>
<
AppOnly
>
1.
The
user
submits
their
credentials
through
a
login
form
.
1.
The
user
submits
their
credentials
through
a
login
form
.
2.
The
form
calls
a
n
API
Route
.
2.
The
form
calls
a
Server
Action
.
3.
Upon
successful
verification
,
the
process
is
completed
,
indicating
the
user
's successful authentication.
3.
Upon
successful
verification
,
the
process
is
completed
,
indicating
the
user
's successful authentication.
4. If verification is unsuccessful, an error message is shown.
4. If verification is unsuccessful, an error message is shown.
Consider a login form where users can input their credentials:
Consider a login form where users can input their credentials:
```tsx filename="
pages/index
.tsx" switcher
```tsx filename="
app/login/page
.tsx" switcher
import {
FormEvent } from '
react
'
import {
authenticate } from '
@/
app
/
lib
/
actions
'
export default function Page() {
export default function Page() {
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault()
const formData = new FormData(event.currentTarget)
const response = await fetch('
/
api
/
login
', {
method: '
POST
',
body: formData,
})
// Handle response if necessary
const data = await response.json()
// ...
}
return (
return (
<form action={
onSubmit
}>
<form action={
authenticate
}>
<input type="email" name="email" placeholder="Email" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="password" name="password" placeholder="Password" required />
<button type="submit">Login</button>
<button type="submit">Login</button>
...
@@ -198,24 +184,12 @@ export default function Page() {
...
@@ -198,24 +184,12 @@ export default function Page() {
}
}
```
```
```jsx filename="pages/index.jsx" switcher
```jsx filename="app/login/page.jsx" switcher
export default function Page() {
import { authenticate } from '
@/
app
/
lib
/
actions
'
async function onSubmit(event) {
event.preventDefault()
const formData = new FormData(event.target)
const response = await fetch('
/
api
/
login
', {
method: '
POST
',
body: formData,
})
// Handle response if necessary
const data = await response.json()
// ...
}
export default function Page() {
return (
return (
<form action={
onSubmit
}>
<form action={
authenticate
}>
<input type="email" name="email" placeholder="Email" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="password" name="password" placeholder="Password" required />
<button type="submit">Login</button>
<button type="submit">Login</button>
...
@@ -224,36 +198,50 @@ export default function Page() {
...
@@ -224,36 +198,50 @@ export default function Page() {
}
}
```
```
The form above has two input fields for capturing the user'
s
email
and
password
.
On
submission
,
it
calls
an
API
Route
to
log
i
n
.
The form above has two input fields for capturing the user'
s
email
and
password
.
On
submission
,
it
calls
the
`authenticate`
Server
Actio
n
.
You
can
then
call
your
authentication
provider
's API in the API Route to handle authentication:
You
can
then
call
your
Authentication
Provider
's API in the Server Action to handle authentication:
```ts filename="app/lib/actions.ts" switcher
'
use
server
'
```ts filename="pages/api/login.ts" switcher
import type { NextApiRequest, NextApiResponse } from '
next
'
import { signIn } from '
@/
auth
'
import { signIn } from '
@/
auth
'
export default async function handler(
export async function authenticate(formData: FormData) {
req: NextApiRequest,
res: NextApiResponse
) {
try {
try {
await signIn('
credentials
', req.body)
await signIn('
credentials
', formData)
return res.status(200).json({ success: true })
} catch (error) {
} catch (error) {
return res.status(500).json({ error })
if (error) {
switch (error.type) {
case '
CredentialsSignin
':
return '
Invalid
credentials
.
'
default:
return '
Something
went
wrong
.
'
}
}
throw error
}
}
}
}
```
```
```js filename="pages/api/login.js" switcher
```js filename="app/lib/actions.js" switcher
'
use
server
'
import { signIn } from '
@/
auth
'
import { signIn } from '
@/
auth
'
export
default function handler(req, res
) {
export
async function authenticate(formData
) {
try {
try {
await signIn('
credentials
', req.body)
await signIn('
credentials
', formData)
return res.status(200).json({ success: true })
} catch (error) {
} catch (error) {
return res.status(500).json({ error })
if (error) {
switch (error.type) {
case '
CredentialsSignin
':
return '
Invalid
credentials
.
'
default:
return '
Something
went
wrong
.
'
}
}
throw error
}
}
}
}
```
```
...
@@ -861,7 +849,7 @@ Here are authentication solutions compatible with Next.js, please refer to the q
...
@@ -861,7 +849,7 @@ Here are authentication solutions compatible with Next.js, please refer to the q
{
/* TODO: Change link to authjs.dev when new documentation is ready */
}
{
/* TODO: Change link to authjs.dev when new documentation is ready */
}
-
[
Auth
O
](
https
://
auth0
.
com
/
docs
/
quickstart
/
webapp
/
nextjs
/
01
-
login
)
-
[
Auth
0
](
https
://
auth0
.
com
/
docs
/
quickstart
/
webapp
/
nextjs
/
01
-
login
)
-
[
Clerk
](
https
://
clerk
.
com
/
docs
/
quickstarts
/
nextjs
)
-
[
Clerk
](
https
://
clerk
.
com
/
docs
/
quickstarts
/
nextjs
)
-
[
NextAuth
.
js
](
https
://
authjs
.
dev
/
guides
/
upgrade
-
to
-
v5
)
-
[
NextAuth
.
js
](
https
://
authjs
.
dev
/
guides
/
upgrade
-
to
-
v5
)
-
[
Supabase
](
https
://
supabase
.
com
/
docs
/
guides
/
getting
-
started
/
quickstarts
/
nextjs
)
-
[
Supabase
](
https
://
supabase
.
com
/
docs
/
guides
/
getting
-
started
/
quickstarts
/
nextjs
)
...
...
docs/03-pages/02-api-reference/02-functions/use-router.mdx
View file @
c5d470a4
...
@@ -101,7 +101,7 @@ export default function Page() {
...
@@ -101,7 +101,7 @@ export default function Page() {
}
}
```
```
Redirecting
the
user
to
`pages/login.js`
,
useful
for
pages
behind
[
authentication
](
/
docs
/
pages
/
building
-
your
-
application
/
routing
/
authenticating
)
:
Redirecting
the
user
to
`pages/login.js`
,
useful
for
pages
behind
[
authentication
](
/
docs
/
pages
/
building
-
your
-
application
/
authentication
)
:
```jsx
```jsx
import { useEffect } from 'react'
import { useEffect } from 'react'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment