Unverified Commit c5d470a4 authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

docs: fix Auth0 typo in authentication (#61171)

parent e6763488
...@@ -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 an 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 in. The form above has two input fields for capturing the user's email and password. On submission, it calls the `authenticate` Server Action.
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 */}
- [AuthO](https://auth0.com/docs/quickstart/webapp/nextjs/01-login) - [Auth0](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)
......
...@@ -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'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment