diff --git a/components/header.js b/components/header.js index 739a529228..3e0a05af6b 100644 --- a/components/header.js +++ b/components/header.js @@ -100,6 +100,11 @@ export default function Header () { Credentials +
  • + + Email + +
  • diff --git a/pages/email.js b/pages/email.js new file mode 100644 index 0000000000..4e38762917 --- /dev/null +++ b/pages/email.js @@ -0,0 +1,66 @@ +import * as React from 'react' +import { signIn, signOut, useSession } from 'next-auth/client' +import Layout from 'components/layout' + +export default function Page () { + const [response, setResponse] = React.useState(null) + const [email, setEmail] = React.useState('') + + const handleChange = (event) => { + setEmail(event.target.value) + } + + const handleLogin = (options) => async (event) => { + event.preventDefault() + + if (options.redirect) { + return signIn('email', options) + } + const response = await signIn('email', options) + setResponse(response) + } + + const handleLogout = (options) => async (event) => { + if (options.redirect) { + return signOut(options) + } + const response = await signOut(options) + setResponse(response) + } + + const [session] = useSession() + + if (session) { + return ( + +

    Test different flows for Email logout

    + Default: +
    + No redirect: +
    +

    Response:

    +
    {JSON.stringify(response, null, 2)}
    +
    + ) + } + + return ( + +

    Test different flows for Email login

    +
    +
    + Default: + +
    +
    + No redirect: + +
    +

    Response:

    +
    {JSON.stringify(response, null, 2)}
    +
    + ) +} diff --git a/src/client/index.js b/src/client/index.js index 964943f662..568ff022d0 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -258,6 +258,9 @@ export async function signIn (provider, options = {}, authorizationParams = {}) return } const isCredentials = providers[provider].type === 'credentials' + const isEmail = providers[provider].type === 'email' + const canRedirectBeDisabled = isCredentials || isEmail + const signInUrl = isCredentials ? `${baseUrl}/callback/${provider}` : `${baseUrl}/signin/${provider}` @@ -279,7 +282,7 @@ export async function signIn (provider, options = {}, authorizationParams = {}) const _signInUrl = `${signInUrl}?${new URLSearchParams(authorizationParams)}` const res = await fetch(_signInUrl, fetchOptions) const data = await res.json() - if (redirect || !isCredentials) { + if (redirect || !canRedirectBeDisabled) { const url = data.url ?? callbackUrl window.location = url // If url contains a hash, the browser does not reload the page. We reload manually diff --git a/www/docs/getting-started/client.md b/www/docs/getting-started/client.md index 2093f70eae..2486ea12fd 100644 --- a/www/docs/getting-started/client.md +++ b/www/docs/getting-started/client.md @@ -212,7 +212,18 @@ The URL must be considered valid by the [redirect callback handler](/configurati #### Using the redirect: false option -When you use the `credentials` provider, you might not want the user to redirect to an error page if an error occurs, so you can handle any errors (like wrong credentials given by the user) on the same page. For that, you can pass `redirect: false` in the second parameter object. `signIn` then will return a Promise, that resolves to the following: +:::note +The redirect option is only available for `credentials` and `email` providers. +::: + +In some cases, you might want to deal with the sign in response on the same page and disable the default redirection. For example, if an error occurs (like wrong credentials given by the user), you might want to handle the error on the same page. For that, you can pass `redirect: false` in the second parameter object. + +e.g. + +- `signIn('credentials', { redirect: false, password: 'password' })` +- `signIn('email', { redirect: false, email: 'bill@fillmurray.com' })` + +`signIn` will then return a Promise, that resolves to the following: ```ts {