Skip to content

Commit b4bd9be

Browse files
committed
[Landing] Login when receiving appToken query param
1 parent b9f71d5 commit b4bd9be

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

landing/src/context/AuthContext.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { constants, UserPlan } from '@brunolemos/devhub-core'
2+
import { useRouter } from 'next/router'
3+
import qs from 'qs'
14
import React, {
25
useCallback,
36
useContext,
@@ -8,7 +11,6 @@ import React, {
811
useState,
912
} from 'react'
1013

11-
import { constants, UserPlan } from '@brunolemos/devhub-core'
1214
import { getDefaultDevHubHeaders } from '../helpers'
1315

1416
export interface AuthProviderProps {
@@ -84,6 +86,8 @@ export const AuthContext = React.createContext<AuthProviderState>({
8486
AuthContext.displayName = 'AuthContext'
8587

8688
export function AuthProvider(props: AuthProviderProps) {
89+
const Router = useRouter()
90+
8791
const [isLoggingIn, setIsLoggingIn] = useState(false)
8892
const [authData, setAuthData] = useState(defaultAuthData)
8993

@@ -116,6 +120,23 @@ export function AuthProvider(props: AuthProviderProps) {
116120
}
117121
}, [authData && authData._id])
118122

123+
useEffect(() => {
124+
if (typeof window === 'undefined') return
125+
126+
const appToken = Router.query.appToken as string | undefined
127+
if (!appToken) return
128+
129+
const querystring = qs.stringify(
130+
{ ...Router.query, appToken: undefined },
131+
{ addQueryPrefix: true },
132+
)
133+
Router.replace(`${Router.pathname}${querystring}`, undefined, {
134+
shallow: true,
135+
})
136+
137+
login(appToken)
138+
}, [Router.query.appToken])
139+
119140
const abortSubscriptionCancellation = useCallback(() => {
120141
if (typeof window === 'undefined' || typeof fetch !== 'function') return
121142
if (!authData.appToken) return

0 commit comments

Comments
 (0)