Skip to content

Commit

Permalink
[Landing] Login when receiving appToken query param
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolemos committed Nov 21, 2019
1 parent b9f71d5 commit b4bd9be
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion landing/src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { constants, UserPlan } from '@brunolemos/devhub-core'
import { useRouter } from 'next/router'
import qs from 'qs'
import React, {
useCallback,
useContext,
Expand All @@ -8,7 +11,6 @@ import React, {
useState,
} from 'react'

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

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

export function AuthProvider(props: AuthProviderProps) {
const Router = useRouter()

const [isLoggingIn, setIsLoggingIn] = useState(false)
const [authData, setAuthData] = useState(defaultAuthData)

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

useEffect(() => {
if (typeof window === 'undefined') return

const appToken = Router.query.appToken as string | undefined
if (!appToken) return

const querystring = qs.stringify(
{ ...Router.query, appToken: undefined },
{ addQueryPrefix: true },
)
Router.replace(`${Router.pathname}${querystring}`, undefined, {
shallow: true,
})

login(appToken)
}, [Router.query.appToken])

const abortSubscriptionCancellation = useCallback(() => {
if (typeof window === 'undefined' || typeof fetch !== 'function') return
if (!authData.appToken) return
Expand Down

0 comments on commit b4bd9be

Please sign in to comment.