Skip to content

Commit e65e8a5

Browse files
committed
switch to anon on auth sync signup to allow the creation of a new account; stop login page from redirecting to callbackUrl on auth sync signup if there's already a session
1 parent 5a57a77 commit e65e8a5

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

components/login.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import { emailSchema } from '@/lib/validate'
1111
import { OverlayTrigger, Tooltip } from 'react-bootstrap'
1212
import { datePivot } from '@/lib/time'
1313
import * as cookie from 'cookie'
14-
import { cookieOptions } from '@/lib/auth'
14+
import { cookieOptions, MULTI_AUTH_ANON, MULTI_AUTH_POINTER } from '@/lib/auth'
1515
import Link from 'next/link'
16+
import useCookie from './use-cookie'
1617

1718
export function EmailLoginForm ({ text, callbackUrl, multiAuth }) {
1819
const disabled = multiAuth
@@ -71,9 +72,18 @@ export function authErrorMessage (error, signin) {
7172
return message
7273
}
7374

74-
export default function Login ({ providers, callbackUrl, multiAuth, error, text, Header, Footer, signin }) {
75+
export default function Login ({ providers, callbackUrl, multiAuth, error, text, Header, Footer, signin, syncSignup }) {
7576
const [errorMessage, setErrorMessage] = useState(authErrorMessage(error, signin))
7677
const router = useRouter()
78+
const [, setPointerCookie] = useCookie(MULTI_AUTH_POINTER)
79+
80+
// we can't signup if we're already logged in to another account
81+
// for signups with auth sync, we first need to switch to anon.
82+
useEffect(() => {
83+
if (syncSignup) {
84+
setPointerCookie(MULTI_AUTH_ANON, cookieOptions({ httpOnly: false }))
85+
}
86+
}, [syncSignup, setPointerCookie])
7787

7888
// signup/signin awareness cookie
7989
useEffect(() => {

pages/api/auth/sync.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ function handleNoSession (res, domainName, redirectUri, signup = false) {
107107

108108
// create SN login URL and add our sync callback URL
109109
const loginRedirectUrl = new URL(signup ? '/signup' : '/login', SN_MAIN_DOMAIN)
110+
if (signup) loginRedirectUrl.searchParams.set('syncSignup', 'true')
110111
loginRedirectUrl.searchParams.set('callbackUrl', syncUrl.href)
111112

112113
// redirect user to login page

pages/login.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Login from '@/components/login'
77
import { isExternal } from '@/lib/url'
88
import { MULTI_AUTH_ANON, MULTI_AUTH_POINTER } from '@/lib/auth'
99

10-
export async function getServerSideProps ({ req, res, query: { callbackUrl, multiAuth = false, error = null } }) {
10+
export async function getServerSideProps ({ req, res, query: { callbackUrl, multiAuth = false, syncSignup = null, error = null } }) {
1111
let session = await getServerSession(req, res, getAuthOptions(req))
1212

1313
// required to prevent infinite redirect loops if we switch to anon
@@ -30,9 +30,9 @@ export async function getServerSideProps ({ req, res, query: { callbackUrl, mult
3030
callbackUrl = '/'
3131
}
3232

33-
if (session && callbackUrl && !multiAuth) {
33+
if (session && callbackUrl && !multiAuth && !syncSignup) {
3434
// in the case of auth linking we want to pass the error back to settings
35-
// in the case of multi auth, don't redirect if there is already a session
35+
// in the case of multi auth or auth sync signup, don't redirect if there is already a session
3636
if (error) {
3737
const url = new URL(callbackUrl, process.env.NEXT_PUBLIC_URL)
3838
url.searchParams.set('error', error)
@@ -54,7 +54,8 @@ export async function getServerSideProps ({ req, res, query: { callbackUrl, mult
5454
providers,
5555
callbackUrl,
5656
error,
57-
multiAuth
57+
multiAuth,
58+
syncSignup
5859
}
5960
}
6061
}

0 commit comments

Comments
 (0)