-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSignInForm.tsx
More file actions
174 lines (153 loc) · 5.33 KB
/
SignInForm.tsx
File metadata and controls
174 lines (153 loc) · 5.33 KB
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
import { useState, useContext } from 'react';
import * as React from 'react';
import { useNavigate } from 'react-router';
import { useTranslation } from 'react-i18next';
import { BootstrapFormInput, BootstrapFormCheckbox, ErrorDisplay } from '@neinteractiveliterature/litform';
import AuthenticationModalContext from './AuthenticationModalContext';
import useAsyncFunction from '../useAsyncFunction';
import useAfterSessionChange from './useAfterSessionChange';
import { AuthenticityTokensContext } from '../AuthenticityTokensContext';
import errorReporting from 'ErrorReporting';
async function signIn(authenticityToken: string, email: string, password: string, rememberMe: boolean) {
const formData = new FormData();
formData.append('user[email]', email);
formData.append('user[password]', password);
if (rememberMe) {
formData.append('user[remember_me]', '1');
}
const response = await fetch('/users/sign_in', {
method: 'POST',
body: formData,
credentials: 'include',
headers: {
Accept: 'application/json',
'X-CSRF-Token': authenticityToken,
},
});
if (!response.ok) {
if (response.headers.get('Content-type')?.startsWith('application/json')) {
throw new Error((await response.json()).error || response.statusText);
}
throw new Error((await response.text()) || response.statusText);
}
return response.url;
}
function SignInForm(): React.JSX.Element {
const { t } = useTranslation();
const navigate = useNavigate();
const {
close: closeModal,
setCurrentView,
afterSignInPath,
unauthenticatedError,
setUnauthenticatedError,
} = useContext(AuthenticationModalContext);
const manager = useContext(AuthenticityTokensContext);
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [rememberMe, setRememberMe] = useState(false);
const afterSessionChange = useAfterSessionChange();
const onSubmit = async (event: React.SyntheticEvent) => {
event.preventDefault();
const authenticityToken = manager.tokens?.signIn;
if (!authenticityToken) {
throw new Error('No authenticity token received from server');
}
try {
const location = await signIn(authenticityToken, email, password, rememberMe);
await afterSessionChange(afterSignInPath || location, {
title: 'Login',
body: 'Logged in successfully!',
autoDismissAfter: 1000 * 60,
});
} catch (e) {
if (!(e instanceof Error && e.message.match(/invalid email or password/i))) {
errorReporting().error(e as string | Error);
}
// we're doing suppressError below specifically so that we can not capture invalid email
// or password errors
throw e;
}
};
const onCancel = (event: React.SyntheticEvent) => {
event.preventDefault();
if (unauthenticatedError) {
navigate('/');
closeModal();
setUnauthenticatedError(false);
} else {
closeModal();
}
};
const [submit, submitError, submitInProgress] = useAsyncFunction(onSubmit, {
suppressError: true,
});
return (
<>
<form onSubmit={submit}>
<div className="modal-header bg-light align-items-center">
<div className="lead flex-grow-1">{t('authentication.signInForm.header')}</div>
</div>
<div className="modal-body">
<BootstrapFormInput
type="email"
label={t('authentication.signInForm.emailLabel')}
value={email}
onTextChange={setEmail}
disabled={submitInProgress}
/>
<BootstrapFormInput
type="password"
label={t('authentication.signInForm.passwordLabel')}
value={password}
onTextChange={setPassword}
disabled={submitInProgress}
/>
<BootstrapFormCheckbox
type="checkbox"
label={t('authentication.signInForm.rememberMeLabel')}
checked={rememberMe}
onCheckedChange={setRememberMe}
disabled={submitInProgress}
/>
<ErrorDisplay stringError={(submitError || {}).message} />
</div>
<div className="modal-footer bg-light">
<div className="flex-grow-1 d-flex flex-column align-items-start">
<button
type="button"
className="btn btn-link p-0 mb-1"
onClick={() => {
setCurrentView('signUp');
}}
>
{t('authentication.signUpLink')}
</button>
<button
type="button"
className="btn btn-link p-0"
onClick={() => {
setCurrentView('forgotPassword');
}}
>
{t('authentication.forgotPasswordLink')}
</button>
</div>
<div>
<button type="button" className="btn btn-secondary me-2" disabled={submitInProgress} onClick={onCancel}>
{t('buttons.cancel')}
</button>
<input
type="submit"
className="btn btn-primary"
disabled={submitInProgress}
value={t('authentication.signInForm.logInButton').toString()}
aria-label={t('authentication.signInForm.logInButton')}
/>
</div>
</div>
</form>
</>
);
}
export default SignInForm;