-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPasswordless.js
353 lines (331 loc) · 10.2 KB
/
Passwordless.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* global Package */
import React, { useState } from 'react';
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
const isEmailValid = email =>
email &&
email.trim() &&
/^[A-Z0-9'.1234z_%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(email);
const isTokenValid = token => token && token.trim() && token.length === 6;
const defaultErrorHandler = ({ error, message }) => {
if (Package['quave:logs']) {
Package['quave:logs'].loggerClient.error({
message: `accounts-passwordless-react: ${message}`,
error,
});
return;
}
console.error(`accounts-passwordless-react: ${message}`, error);
};
const noop = () => {};
const Field = props => (
<input
className="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
{...props}
/>
);
const Label = (props = {}) => (
<label className="block text-sm font-medium text-gray-700" {...props} />
);
const Link = props => {
const { children } = props;
return (
<div className="mt-0 flex justify-end">
<div className="text-sm">
<a
className="font-medium text-indigo-600 hover:text-indigo-500 cursor-pointer"
{...props}
>
{children}
</a>
</div>
</div>
);
};
const Button = props => {
const { children } = props;
return (
<button
type="submit"
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
{...props}
>
{children}
</button>
);
};
const Message = ({ message, setMessage, isError = true }) => {
if (!message) {
return null;
}
return (
<div className={`rounded-md ${isError ? 'bg-red-50' : 'bg-green-50'} p-4`}>
<div className="flex">
<div>
<p
className={`text-sm font-medium ${
isError ? 'text-red-800' : 'text-green-800'
}`}
>
{message}
</p>
</div>
<div className="ml-auto pl-3">
<div className="-mx-1.5 -my-1.5">
<button
onClick={() => setMessage('')}
type="button"
className={`inline-flex ${
isError ? 'bg-red-50' : 'bg-green-50'
} rounded-md p-1.5 ${
isError ? 'text-red-500' : 'text-green-500'
} hover:${
isError ? 'bg-red-100' : 'bg-green-100'
} focus:outline-none focus:ring-2 focus:ring-offset-2 focus:${
isError ? 'ring-offset-red-50' : 'ring-offset-green-50'
} focus:${isError ? 'ring-red-600' : 'ring-green-600'}`}
>
<span className="sr-only">Dismiss</span>
</button>
</div>
</div>
</div>
</div>
);
};
const Error = Message;
const Success = props => <Message isError={false} {...props} />;
export const Passwordless = ({
onRequestToken = noop,
onRequestError = defaultErrorHandler,
onInvalidEmail = noop,
onEnterToken = noop,
onEnterError = defaultErrorHandler,
onInvalidToken = noop,
userData = {},
inputProps = {},
emailInputProps = {},
tokenInputProps = {},
twoFactorCodeInputProps = {},
labelProps = {},
linkProps = {},
buttonProps = {},
containerClassName = 'mt-8 sm:mx-auto sm:w-full sm:max-w-md',
wrapperClassName = 'bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10',
formClassName = 'space-y-6',
emailLabel = 'Email address',
tokenLabel = 'Token',
emailLinkLabel = 'I already have a token',
tokenLinkLabel = "I don't have a token yet",
requestButtonLabel = 'Request Access',
enterButtonLabel = 'Sign in',
emailValidationErrorMessage = 'Invalid email.',
tokenValidationErrorMessage = 'Invalid token.',
twoFactorCodeLabel = '2FA Code',
twoFactorCodeValidationErrorMessage = 'Invalid 2FA code.',
getSuccessRequestTokenMessage = ({ isNewUser }) =>
isNewUser
? 'Please check your email to confirm your account and get your access token.'
: 'Welcome back. Check your email to access with your token!',
ErrorComponent = Error,
SuccessComponent = Success,
extra,
}) => {
const [isWaitingToInformToken, setIsWaitingToInformToken] = useState(false);
const [email, setEmail] = useState('');
const [token, setToken] = useState('');
const [twoFactorCode, setTwoFactorCode] = useState('');
const [needsTwoFactorCode, setNeedsTwoFactorCode] = useState(false);
const [isCreating, setIsCreating] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const [successRequestTokenMessage, setSuccessRequestTokenMessage] = useState(
''
);
const askToken = e => {
e.preventDefault();
setErrorMessage('');
setSuccessRequestTokenMessage('');
if (!isEmailValid(email)) {
setErrorMessage(emailValidationErrorMessage);
onInvalidEmail();
return;
}
Accounts.requestLoginTokenForUser(
{
selector: { email },
userData: { email, ...userData },
options: { extra },
},
(error, { isNewUser } = {}) => {
if (error) {
onRequestError({
error,
message: 'Error requesting login token for user',
});
setEmail('');
setToken('');
return;
}
setIsWaitingToInformToken(true);
setIsCreating(isNewUser);
setSuccessRequestTokenMessage(
getSuccessRequestTokenMessage({ isNewUser })
);
onRequestToken({ isNewUser });
}
);
};
const enterToken = e => {
e.preventDefault();
setErrorMessage('');
setSuccessRequestTokenMessage('');
if (!isEmailValid(email)) {
setErrorMessage(emailValidationErrorMessage);
onInvalidEmail();
return;
}
if (!isTokenValid(token)) {
setErrorMessage(tokenValidationErrorMessage);
onInvalidToken();
return;
}
if (needsTwoFactorCode) {
Meteor.passwordlessLoginWithTokenAnd2faCode({ email }, token, twoFactorCode, error => {
if (error) {
onEnterError({
error,
message: 'Error entering 2FA code',
});
setErrorMessage(twoFactorCodeValidationErrorMessage);
setTwoFactorCode('');
return;
}
onEnterToken({ isNewUser: isCreating });
});
return;
}
Meteor.passwordlessLoginWithToken({ email }, token, error => {
if (error) {
if (error.error === 'no-2fa-code') {
setNeedsTwoFactorCode(true);
return;
}
onEnterError({
error,
message: 'Error entering login token',
});
setErrorMessage("Your token doesn't match. Please try again.");
setToken('');
return;
}
onEnterToken({ isNewUser: isCreating });
});
};
const onSubmit = isWaitingToInformToken ? enterToken : askToken;
return (
<div className={containerClassName}>
<div className={wrapperClassName}>
<form className={formClassName} onSubmit={onSubmit}>
<div>
<Label htmlFor="email" {...labelProps}>
{emailLabel}
</Label>
<div className="mt-1">
<Field
id="email"
name="email"
type="email"
autoComplete="email"
required
value={email}
onChange={({ target: { value } }) => setEmail(value)}
{...inputProps}
{...emailInputProps}
/>
</div>
</div>
{isWaitingToInformToken && (
<div>
<Label htmlFor="token" {...labelProps}>
{tokenLabel}
</Label>
<div className="mt-1">
<Field
id="token"
name="token"
autoComplete="off"
required
value={token}
onChange={({ target: { value } }) => setToken(value)}
{...inputProps}
{...tokenInputProps}
/>
</div>
</div>
)}
{needsTwoFactorCode && (
<div>
<Label htmlFor="twoFactorCode" {...labelProps}>
{twoFactorCodeLabel}
</Label>
<div className="mt-1">
<Field
id="twoFactorCode"
name="twoFactorCode"
autoComplete="off"
required
value={twoFactorCode}
onChange={({ target: { value } }) => setTwoFactorCode(value)}
{...inputProps}
{...twoFactorCodeInputProps}
/>
</div>
</div>
)}
{ErrorComponent && (
<ErrorComponent
message={errorMessage}
setMessage={setErrorMessage}
/>
)}
{SuccessComponent && (
<SuccessComponent
message={successRequestTokenMessage}
setMessage={setSuccessRequestTokenMessage}
/>
)}
<div>
{isWaitingToInformToken ? (
<Button {...buttonProps}>{enterButtonLabel}</Button>
) : (
<Button {...buttonProps}>{requestButtonLabel}</Button>
)}
</div>
{isWaitingToInformToken ? (
<Link
onClick={() => {
setIsWaitingToInformToken(false);
setErrorMessage('');
setSuccessRequestTokenMessage('');
}}
{...linkProps}
>
{tokenLinkLabel}
</Link>
) : (
<Link
onClick={() => {
setIsWaitingToInformToken(true);
setErrorMessage('');
setSuccessRequestTokenMessage('');
}}
{...linkProps}
>
{emailLinkLabel}
</Link>
)}
</form>
</div>
</div>
);
};