-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelectFactor.jsx
97 lines (89 loc) · 2.89 KB
/
SelectFactor.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import IconButton from "../components/IconButton";
import SignUpWithPassword from "./SignUpWithPassword";
import LogInWithPassword from "./LogInWithPassword";
import Divider from "../components/Divider";
import ErrorMessage from "../components/ErrorMessage";
import { log } from "../services/logging";
const keyFor = (factor) => `${factor.channel}-${factor.strategy}`;
/**
* A view prompting the user to select a factor. May also include the
* password view inline.
*
* @param {object} props
* @param {object} props.flow - the auth flow in use
* @param {boolean} props.isCompact - if true, show a "Username and password" button to
* open the password view. If false, show the password view inline. Default false.
* @param {array} props.allowedSecondFactors - which second factors are allowed, if this is the second factor.
* @param {boolean} props.isSecondFactor - if true, use allowedSecondFactors. If false, use flow.firstFactors.
* @param {boolean} props.isLogin - if true, this is a login form; if false, this is a signup form. Default false.
* @param {object} error - a Userfront error to display
* @param {function} onEvent
*/
const SelectFactor = ({
flow,
isCompact = false,
onEvent,
allowedSecondFactors,
isSecondFactor = false,
isLogin = false,
error,
}) => {
const _onEvent = onEvent || ((evt) => log("event", evt));
const factors = isSecondFactor ? allowedSecondFactors : flow.firstFactors;
const displayItems = [];
const handleSelectFactor = (factor) => {
_onEvent({
factor,
type: "selectFactor",
});
};
let showingPassword = false;
const PasswordForm = isLogin ? LogInWithPassword : SignUpWithPassword;
// Build list of buttons for factors,
// with password button if in compact view,
// or password entry if in default view
for (let i = 0; i < factors.length; i++) {
const factor = factors[i];
if (factor.strategy !== "password") {
displayItems.push(
<IconButton
factor={factor}
onClick={() => handleSelectFactor(factor)}
key={keyFor(factor)}
/>
);
} else if (isCompact) {
displayItems.push(
<IconButton
factor={factor}
onClick={() => handleSelectFactor(factor)}
key={keyFor(factor)}
/>
);
} else {
showingPassword = true;
// Put dividers before and after, as appropriate
if (i !== 0) {
displayItems.push(<Divider text="OR" key="before_password" />);
}
displayItems.push(
<PasswordForm
key={keyFor(factor)}
onEvent={_onEvent}
allowBack={false}
error={error}
/>
);
if (i < factors.length - 1) {
displayItems.push(<Divider text="OR" key="after_password" />);
}
}
}
return (
<>
{displayItems}
{!showingPassword && <ErrorMessage error={error} />}
</>
);
};
export default SelectFactor;