Debian Codegraph @

examples/react-spa/frontend/src/account/AuthenticateFlow.js

raw
1 import { Link, useLocation } from 'react-router-dom'
2 import { pathForFlow } from '../auth'
3 import { Flows, AuthenticatorType } from '../lib/allauth'
4
5 const flowLabels = {}
6 flowLabels[Flows.REAUTHENTICATE] = 'Use your password'
7 flowLabels[`${Flows.MFA_REAUTHENTICATE}:${AuthenticatorType.TOTP}`] = 'Use your authenticator app'
8 flowLabels[`${Flows.MFA_REAUTHENTICATE}:${AuthenticatorType.RECOVERY_CODES}`] = 'Use a recovery code'
9 flowLabels[`${Flows.MFA_REAUTHENTICATE}:${AuthenticatorType.WEBAUTHN}`] = 'Use security key'
10
11 function flowsToMethods (flows) {
12 const methods = []
13 flows.forEach(flow => {
14 if (flow.id === Flows.MFA_REAUTHENTICATE) {
15 flow.types.forEach(typ => {
16 methods.push({
17 label: flowLabels[`${flow.id}:${typ}`] || flow.id,
18 id: flow.id,
19 path: pathForFlow(flow, typ)
20 })
21 })
22 } else {
23 methods.push({
24 label: flowLabels[flow.id] || flow.id,
25 id: flow.id,
26 path: pathForFlow(flow)
27 })
28 }
29 })
30 return methods
31 }
32
33 export default function ReauthenticateFlow (props) {
34 const location = useLocation()
35 const methods = flowsToMethods(location.state.reauth.data.flows)
36
37 return (
38 <div>
39 <h1>Confirm Access</h1>
40 <p>
41 Please reauthenticate to safeguard your account.
42 </p>
43 {props.children}
44
45 {methods.length > 1
46 ? <><h2>Alternative Options</h2>
47 <ul>
48 {methods.filter(method => method.id !== props.method).map(method => {
49 return (
50 <li key={method.id}>
51 <Link replace state={location.state} to={method.path + location.search}>{method.label}</Link>
52 </li>
53 )
54 })}
55 </ul>
56 </>
57 : null}
58 </div>
59
60 )
61 }