Debian Codegraph @

examples/react-spa/frontend/src/auth/routing.js

raw
1 import {
2 Navigate,
3 useLocation
4 } from 'react-router-dom'
5 import { useAuthChange, AuthChangeEvent, useAuthStatus } from './hooks'
6 import { Flows, AuthenticatorType } from '../lib/allauth'
7
8 export const URLs = Object.freeze({
9 LOGIN_URL: '/account/login',
10 LOGIN_REDIRECT_URL: '/calculator',
11 LOGOUT_REDIRECT_URL: '/'
12 })
13
14 const flow2path = {}
15 flow2path[Flows.LOGIN] = '/account/login'
16 flow2path[Flows.LOGIN_BY_CODE] = '/account/login/code/confirm'
17 flow2path[Flows.SIGNUP] = '/account/signup'
18 flow2path[Flows.VERIFY_EMAIL] = '/account/verify-email'
19 flow2path[Flows.PASSWORD_RESET_BY_CODE] = '/account/password/reset/confirm'
20 flow2path[Flows.PROVIDER_SIGNUP] = '/account/provider/signup'
21 flow2path[Flows.REAUTHENTICATE] = '/account/reauthenticate'
22 flow2path[Flows.MFA_TRUST] = '/account/2fa/trust'
23 flow2path[`${Flows.MFA_AUTHENTICATE}:${AuthenticatorType.TOTP}`] = '/account/authenticate/totp'
24 flow2path[`${Flows.MFA_AUTHENTICATE}:${AuthenticatorType.RECOVERY_CODES}`] = '/account/authenticate/recovery-codes'
25 flow2path[`${Flows.MFA_AUTHENTICATE}:${AuthenticatorType.WEBAUTHN}`] = '/account/authenticate/webauthn'
26 flow2path[`${Flows.MFA_REAUTHENTICATE}:${AuthenticatorType.TOTP}`] = '/account/reauthenticate/totp'
27 flow2path[`${Flows.MFA_REAUTHENTICATE}:${AuthenticatorType.RECOVERY_CODES}`] = '/account/reauthenticate/recovery-codes'
28 flow2path[`${Flows.MFA_REAUTHENTICATE}:${AuthenticatorType.WEBAUTHN}`] = '/account/reauthenticate/webauthn'
29 flow2path[Flows.MFA_WEBAUTHN_SIGNUP] = '/account/signup/passkey/create'
30
31 export function pathForFlow (flow, typ) {
32 let key = flow.id
33 if (typeof flow.types !== 'undefined') {
34 typ = typ ?? flow.types[0]
35 key = `${key}:${typ}`
36 }
37 const path = flow2path[key] ?? flow2path[flow.id]
38 if (!path) {
39 throw new Error(`Unknown path for flow: ${flow.id}`)
40 }
41 return path
42 }
43
44 export function pathForPendingFlow (auth) {
45 const flow = auth.data.flows.find(flow => flow.is_pending)
46 if (flow) {
47 return pathForFlow(flow)
48 }
49 return null
50 }
51
52 function navigateToPendingFlow (auth) {
53 const path = pathForPendingFlow(auth)
54 if (path) {
55 return <Navigate to={path} />
56 }
57 return null
58 }
59
60 export function AuthenticatedRoute ({ children }) {
61 const location = useLocation()
62 const [, status] = useAuthStatus()
63 const next = `next=${encodeURIComponent(location.pathname + location.search)}`
64 if (status.isAuthenticated) {
65 return children
66 } else {
67 return <Navigate to={`${URLs.LOGIN_URL}?${next}`} />
68 }
69 }
70
71 export function AnonymousRoute ({ children }) {
72 const [, status] = useAuthStatus()
73 if (!status.isAuthenticated) {
74 return children
75 } else {
76 return <Navigate to={URLs.LOGIN_REDIRECT_URL} />
77 }
78 }
79
80 export function AuthChangeRedirector ({ children }) {
81 const [auth, event] = useAuthChange()
82 const location = useLocation()
83 switch (event) {
84 case AuthChangeEvent.LOGGED_OUT:
85 return <Navigate to={URLs.LOGOUT_REDIRECT_URL} />
86 case AuthChangeEvent.LOGGED_IN:
87 return <Navigate to={URLs.LOGIN_REDIRECT_URL} />
88 case AuthChangeEvent.REAUTHENTICATED:
89 {
90 const next = new URLSearchParams(location.search).get('next') || '/'
91 return <Navigate to={next} />
92 }
93 case AuthChangeEvent.REAUTHENTICATION_REQUIRED: {
94 const next = `next=${encodeURIComponent(location.pathname + location.search)}`
95 const path = pathForFlow(auth.data.flows[0])
96 return <Navigate to={`${path}?${next}`} state={{ reauth: auth }} />
97 }
98 case AuthChangeEvent.FLOW_UPDATED:
99 const pendingFlow = navigateToPendingFlow(auth)
100 if (!pendingFlow) {
101 throw new Error()
102 }
103 return pendingFlow
104 default:
105 break
106 }
107 // ...stay where we are
108 return children
109 }