From 526738ebbb8723249412958eeb61e0bcc6a84939 Mon Sep 17 00:00:00 2001 From: Yannick Tian Date: Fri, 26 Feb 2021 18:37:10 +0100 Subject: [PATCH 1/3] feat: allow to disable client-side redirect for email provider --- components/header.js | 5 ++++ pages/email.js | 70 ++++++++++++++++++++++++++++++++++++++++++++ src/client/index.js | 5 +++- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 pages/email.js 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..2e9324360f --- /dev/null +++ b/pages/email.js @@ -0,0 +1,70 @@ +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 From 8e50af2f7a6e9d31269e35ded1f04cebf1d122fd Mon Sep 17 00:00:00 2001 From: Yannick Tian Date: Mon, 1 Mar 2021 10:50:16 +0100 Subject: [PATCH 2/3] docs(client): mention that redirect can also be disabled for email provider --- www/docs/getting-started/client.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 { From 843f7a841fbe9810ca34f716be484e5154612c4c Mon Sep 17 00:00:00 2001 From: Yannick Tian Date: Mon, 1 Mar 2021 10:54:40 +0100 Subject: [PATCH 3/3] feat: only display one email input in email page --- pages/email.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/pages/email.js b/pages/email.js index 2e9324360f..4e38762917 100644 --- a/pages/email.js +++ b/pages/email.js @@ -47,20 +47,16 @@ export default function Page () { return (

    Test different flows for Email login

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

    Response: