| 1 |
import { useContext, useRef, useState, useEffect } from 'react' |
| 2 |
import { AuthContext } from './AuthContext' |
| 3 |
|
| 4 |
export function useAuth () { |
| 5 |
return useContext(AuthContext)?.auth |
| 6 |
} |
| 7 |
|
| 8 |
export function useConfig () { |
| 9 |
return useContext(AuthContext)?.config |
| 10 |
} |
| 11 |
|
| 12 |
export function useUser () { |
| 13 |
const auth = useContext(AuthContext)?.auth |
| 14 |
return authInfo(auth).user |
| 15 |
} |
| 16 |
|
| 17 |
export function useAuthInfo () { |
| 18 |
const auth = useContext(AuthContext)?.auth |
| 19 |
return authInfo(auth) |
| 20 |
} |
| 21 |
|
| 22 |
function authInfo (auth) { |
| 23 |
const isAuthenticated = auth.status === 200 || (auth.status === 401 && auth.meta.is_authenticated) |
| 24 |
const requiresReauthentication = isAuthenticated && auth.status === 401 |
| 25 |
const pendingFlow = auth.data?.flows?.find(flow => flow.is_pending) |
| 26 |
return { isAuthenticated, requiresReauthentication, user: isAuthenticated ? auth.data.user : null, pendingFlow } |
| 27 |
} |
| 28 |
|
| 29 |
export const AuthChangeEvent = Object.freeze({ |
| 30 |
LOGGED_OUT: 'LOGGED_OUT', |
| 31 |
LOGGED_IN: 'LOGGED_IN', |
| 32 |
REAUTHENTICATED: 'REAUTHENTICATED', |
| 33 |
REAUTHENTICATION_REQUIRED: 'REAUTHENTICATION_REQUIRED', |
| 34 |
FLOW_UPDATED: 'FLOW_UPDATED' |
| 35 |
}) |
| 36 |
|
| 37 |
function determineAuthChangeEvent (fromAuth, toAuth) { |
| 38 |
let fromInfo = authInfo(fromAuth) |
| 39 |
const toInfo = authInfo(toAuth) |
| 40 |
if (toAuth.status === 410) { |
| 41 |
return AuthChangeEvent.LOGGED_OUT |
| 42 |
} |
| 43 |
// Corner case: user ID change. Treat as if we're transitioning from anonymous state. |
| 44 |
if (fromInfo.user && toInfo.user && fromInfo.user?.id !== toInfo.user?.id) { |
| 45 |
fromInfo = { isAuthenticated: false, requiresReauthentication: false, user: null } |
| 46 |
} |
| 47 |
if (!fromInfo.isAuthenticated && toInfo.isAuthenticated) { |
| 48 |
// You typically don't transition from logged out to reauthentication required. |
| 49 |
return AuthChangeEvent.LOGGED_IN |
| 50 |
} else if (fromInfo.isAuthenticated && !toInfo.isAuthenticated) { |
| 51 |
return AuthChangeEvent.LOGGED_OUT |
| 52 |
} else if (fromInfo.isAuthenticated && toInfo.isAuthenticated) { |
| 53 |
if (toInfo.requiresReauthentication) { |
| 54 |
return AuthChangeEvent.REAUTHENTICATION_REQUIRED |
| 55 |
} else if (fromInfo.requiresReauthentication) { |
| 56 |
return AuthChangeEvent.REAUTHENTICATED |
| 57 |
} else if (fromAuth.data.methods.length < toAuth.data.methods.length) { |
| 58 |
// If you do a page reload when on the reauthentication page, both fromAuth |
| 59 |
// and toAuth are authenticated, and it won't see the change when |
| 60 |
// reauthentication without this. |
| 61 |
return AuthChangeEvent.REAUTHENTICATED |
| 62 |
} |
| 63 |
} else if (!fromInfo.isAuthenticated && !toInfo.isAuthenticated) { |
| 64 |
const fromFlow = fromInfo.pendingFlow |
| 65 |
const toFlow = toInfo.pendingFlow |
| 66 |
if (toFlow?.id && fromFlow?.id !== toFlow.id) { |
| 67 |
return AuthChangeEvent.FLOW_UPDATED |
| 68 |
} |
| 69 |
} |
| 70 |
// No change. |
| 71 |
return null |
| 72 |
} |
| 73 |
|
| 74 |
export function useAuthChange () { |
| 75 |
const auth = useAuth() |
| 76 |
const ref = useRef({ prevAuth: auth, event: null, didChange: false }) |
| 77 |
const [, setForcedUpdate] = useState(0) |
| 78 |
useEffect(() => { |
| 79 |
if (ref.current.prevAuth) { |
| 80 |
ref.current.didChange = true |
| 81 |
const event = determineAuthChangeEvent(ref.current.prevAuth, auth) |
| 82 |
if (event) { |
| 83 |
ref.current.event = event |
| 84 |
setForcedUpdate(gen => gen + 1) |
| 85 |
} |
| 86 |
} |
| 87 |
ref.current.prevAuth = auth |
| 88 |
}, [auth, ref]) |
| 89 |
const didChange = ref.current.didChange |
| 90 |
if (didChange) { |
| 91 |
ref.current.didChange = false |
| 92 |
} |
| 93 |
const event = ref.current.event |
| 94 |
if (event) { |
| 95 |
ref.current.event = null |
| 96 |
} |
| 97 |
|
| 98 |
return [auth, event] |
| 99 |
} |
| 100 |
|
| 101 |
export function useAuthStatus () { |
| 102 |
const auth = useAuth() |
| 103 |
return [auth, authInfo(auth)] |
| 104 |
} |