| 1 |
import { useState, useEffect } from 'react' |
| 2 |
import { AuthChangeRedirector, AnonymousRoute, AuthenticatedRoute } from './auth' |
| 3 |
import { |
| 4 |
createBrowserRouter, |
| 5 |
RouterProvider |
| 6 |
} from 'react-router-dom' |
| 7 |
import Calculator from './Calculator' |
| 8 |
import Login from './account/Login' |
| 9 |
import RequestLoginCode from './account/RequestLoginCode' |
| 10 |
import ConfirmLoginCode from './account/ConfirmLoginCode' |
| 11 |
import Logout from './account/Logout' |
| 12 |
import Signup from './account/Signup' |
| 13 |
import ProviderSignup from './socialaccount/ProviderSignup' |
| 14 |
import ProviderCallback from './socialaccount/ProviderCallback' |
| 15 |
import Home from './Home' |
| 16 |
import ChangeEmail from './account/ChangeEmail' |
| 17 |
import ManageProviders from './socialaccount/ManageProviders' |
| 18 |
import VerifyEmail, { loader as verifyEmailLoader } from './account/VerifyEmail' |
| 19 |
import VerifyEmailByCode from './account/VerifyEmailByCode' |
| 20 |
import VerificationEmailSent from './account/VerificationEmailSent' |
| 21 |
import RequestPasswordReset from './account/RequestPasswordReset' |
| 22 |
import ConfirmPasswordResetCode from './account/ConfirmPasswordResetCode' |
| 23 |
import ChangePassword from './account/ChangePassword' |
| 24 |
import MFAOverview, { loader as mfaOverviewLoader } from './mfa/MFAOverview' |
| 25 |
import ActivateTOTP, { loader as activateTOTPLoader } from './mfa/ActivateTOTP' |
| 26 |
import DeactivateTOTP from './mfa/DeactivateTOTP' |
| 27 |
import RecoveryCodes, { loader as recoveryCodesLoader } from './mfa/RecoveryCodes' |
| 28 |
import AddWebAuthn from './mfa/AddWebAuthn' |
| 29 |
import SignupByPasskey from './mfa/SignupByPasskey' |
| 30 |
import ReauthenticateWebAuthn from './mfa/ReauthenticateWebAuthn' |
| 31 |
import ListWebAuthn, { loader as listWebAuthnLoader } from './mfa/ListWebAuthn' |
| 32 |
import GenerateRecoveryCodes, { loader as generateRecoveryCodesLoader } from './mfa/GenerateRecoveryCodes' |
| 33 |
import { resetPasswordByLinkLoader, ResetPasswordByCode, ResetPasswordByLink } from './account/ResetPassword' |
| 34 |
import AuthenticateTOTP from './mfa/AuthenticateTOTP' |
| 35 |
import AuthenticateRecoveryCodes from './mfa/AuthenticateRecoveryCodes' |
| 36 |
import AuthenticateWebAuthn from './mfa/AuthenticateWebAuthn' |
| 37 |
import ReauthenticateRecoveryCodes from './mfa/ReauthenticateRecoveryCodes' |
| 38 |
import ReauthenticateTOTP from './mfa/ReauthenticateTOTP' |
| 39 |
import CreateSignupPasskey from './mfa/CreateSignupPasskey' |
| 40 |
import Trust from './mfa/Trust' |
| 41 |
import Reauthenticate from './account/Reauthenticate' |
| 42 |
import Sessions from './usersessions/Sessions' |
| 43 |
import Root from './Root' |
| 44 |
import { useConfig } from './auth/hooks' |
| 45 |
|
| 46 |
function createRouter (config) { |
| 47 |
return createBrowserRouter([ |
| 48 |
{ |
| 49 |
path: '/', |
| 50 |
element: <AuthChangeRedirector><Root /></AuthChangeRedirector>, |
| 51 |
children: [ |
| 52 |
{ |
| 53 |
path: '/', |
| 54 |
element: <Home /> |
| 55 |
}, |
| 56 |
{ |
| 57 |
path: '/calculator', |
| 58 |
element: <Calculator /> |
| 59 |
}, |
| 60 |
{ |
| 61 |
path: '/account/login', |
| 62 |
element: <AnonymousRoute><Login /></AnonymousRoute> |
| 63 |
}, |
| 64 |
{ |
| 65 |
path: '/account/login/code', |
| 66 |
element: <AnonymousRoute><RequestLoginCode /></AnonymousRoute> |
| 67 |
}, |
| 68 |
{ |
| 69 |
path: '/account/login/code/confirm', |
| 70 |
element: <AnonymousRoute><ConfirmLoginCode /></AnonymousRoute> |
| 71 |
}, |
| 72 |
{ |
| 73 |
path: '/account/email', |
| 74 |
element: <AuthenticatedRoute><ChangeEmail /></AuthenticatedRoute> |
| 75 |
}, |
| 76 |
{ |
| 77 |
path: '/account/logout', |
| 78 |
element: <Logout /> |
| 79 |
}, |
| 80 |
{ |
| 81 |
path: '/account/provider/callback', |
| 82 |
element: <ProviderCallback /> |
| 83 |
}, |
| 84 |
{ |
| 85 |
path: '/account/provider/signup', |
| 86 |
element: <AnonymousRoute><ProviderSignup /></AnonymousRoute> |
| 87 |
}, |
| 88 |
{ |
| 89 |
path: '/account/providers', |
| 90 |
element: <AuthenticatedRoute><ManageProviders /></AuthenticatedRoute> |
| 91 |
}, |
| 92 |
{ |
| 93 |
path: '/account/signup', |
| 94 |
element: <AnonymousRoute><Signup /></AnonymousRoute> |
| 95 |
}, |
| 96 |
{ |
| 97 |
path: '/account/signup/passkey', |
| 98 |
element: <AnonymousRoute><SignupByPasskey /></AnonymousRoute> |
| 99 |
}, |
| 100 |
{ |
| 101 |
path: '/account/signup/passkey/create', |
| 102 |
element: <AnonymousRoute><CreateSignupPasskey /></AnonymousRoute> |
| 103 |
}, |
| 104 |
{ |
| 105 |
path: '/account/verify-email', |
| 106 |
element: config.data.account.email_verification_by_code_enabled ? <VerifyEmailByCode /> : <VerificationEmailSent /> |
| 107 |
}, |
| 108 |
{ |
| 109 |
path: '/account/verify-email/:key', |
| 110 |
element: <VerifyEmail />, |
| 111 |
loader: verifyEmailLoader |
| 112 |
}, |
| 113 |
{ |
| 114 |
path: '/account/password/reset', |
| 115 |
element: <AnonymousRoute><RequestPasswordReset /></AnonymousRoute> |
| 116 |
}, |
| 117 |
{ |
| 118 |
path: '/account/password/reset/confirm', |
| 119 |
element: <AnonymousRoute><ConfirmPasswordResetCode /></AnonymousRoute> |
| 120 |
}, |
| 121 |
{ |
| 122 |
path: '/account/password/reset/complete', |
| 123 |
element: <AnonymousRoute><ResetPasswordByCode /></AnonymousRoute> |
| 124 |
}, |
| 125 |
{ |
| 126 |
path: '/account/password/reset/key/:key', |
| 127 |
element: <AnonymousRoute><ResetPasswordByLink /></AnonymousRoute>, |
| 128 |
loader: resetPasswordByLinkLoader |
| 129 |
}, |
| 130 |
{ |
| 131 |
path: '/account/password/change', |
| 132 |
element: <AuthenticatedRoute><ChangePassword /></AuthenticatedRoute> |
| 133 |
}, |
| 134 |
{ |
| 135 |
path: '/account/2fa', |
| 136 |
element: <AuthenticatedRoute><MFAOverview /></AuthenticatedRoute>, |
| 137 |
loader: mfaOverviewLoader |
| 138 |
}, |
| 139 |
{ |
| 140 |
path: '/account/reauthenticate', |
| 141 |
element: <AuthenticatedRoute><Reauthenticate /></AuthenticatedRoute> |
| 142 |
}, |
| 143 |
{ |
| 144 |
path: '/account/reauthenticate/totp', |
| 145 |
element: <AuthenticatedRoute><ReauthenticateTOTP /></AuthenticatedRoute> |
| 146 |
}, |
| 147 |
{ |
| 148 |
path: '/account/reauthenticate/recovery-codes', |
| 149 |
element: <AuthenticatedRoute><ReauthenticateRecoveryCodes /></AuthenticatedRoute> |
| 150 |
}, |
| 151 |
{ |
| 152 |
path: '/account/reauthenticate/webauthn', |
| 153 |
element: <AuthenticatedRoute><ReauthenticateWebAuthn /></AuthenticatedRoute> |
| 154 |
}, |
| 155 |
{ |
| 156 |
path: '/account/authenticate/totp', |
| 157 |
element: <AnonymousRoute><AuthenticateTOTP /></AnonymousRoute> |
| 158 |
}, |
| 159 |
{ |
| 160 |
path: '/account/2fa/trust', |
| 161 |
element: <AnonymousRoute><Trust /></AnonymousRoute> |
| 162 |
}, |
| 163 |
{ |
| 164 |
path: '/account/authenticate/recovery-codes', |
| 165 |
element: <AnonymousRoute><AuthenticateRecoveryCodes /></AnonymousRoute> |
| 166 |
}, |
| 167 |
{ |
| 168 |
path: '/account/authenticate/webauthn', |
| 169 |
element: <AnonymousRoute><AuthenticateWebAuthn /></AnonymousRoute> |
| 170 |
}, |
| 171 |
{ |
| 172 |
path: '/account/2fa/totp/activate', |
| 173 |
element: <AuthenticatedRoute><ActivateTOTP /></AuthenticatedRoute>, |
| 174 |
loader: activateTOTPLoader |
| 175 |
}, |
| 176 |
{ |
| 177 |
path: '/account/2fa/totp/deactivate', |
| 178 |
element: <AuthenticatedRoute><DeactivateTOTP /></AuthenticatedRoute> |
| 179 |
}, |
| 180 |
{ |
| 181 |
path: '/account/2fa/recovery-codes', |
| 182 |
element: <AuthenticatedRoute><RecoveryCodes /></AuthenticatedRoute>, |
| 183 |
loader: recoveryCodesLoader |
| 184 |
}, |
| 185 |
{ |
| 186 |
path: '/account/2fa/recovery-codes/generate', |
| 187 |
element: <AuthenticatedRoute><GenerateRecoveryCodes /></AuthenticatedRoute>, |
| 188 |
loader: generateRecoveryCodesLoader |
| 189 |
}, |
| 190 |
{ |
| 191 |
path: '/account/2fa/webauthn', |
| 192 |
element: <AuthenticatedRoute><ListWebAuthn /></AuthenticatedRoute>, |
| 193 |
loader: listWebAuthnLoader |
| 194 |
}, |
| 195 |
{ |
| 196 |
path: '/account/2fa/webauthn/add', |
| 197 |
element: <AuthenticatedRoute><AddWebAuthn /></AuthenticatedRoute> |
| 198 |
}, |
| 199 |
{ |
| 200 |
path: '/account/sessions', |
| 201 |
element: <AuthenticatedRoute><Sessions /></AuthenticatedRoute> |
| 202 |
} |
| 203 |
] |
| 204 |
} |
| 205 |
]) |
| 206 |
} |
| 207 |
|
| 208 |
export default function Router () { |
| 209 |
// If we create the router globally, the loaders of the routes already trigger |
| 210 |
// even before the <AuthContext/> trigger the initial loading of the auth. |
| 211 |
// state. |
| 212 |
const [router, setRouter] = useState(null) |
| 213 |
const config = useConfig() |
| 214 |
useEffect(() => { |
| 215 |
setRouter(createRouter(config)) |
| 216 |
}, [config]) |
| 217 |
return router ? <RouterProvider router={router} /> : null |
| 218 |
} |