/
auth.tsx
293 lines (280 loc) · 9.49 KB
/
auth.tsx
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
import * as React from "react";
import { Formik, Form, Field, ErrorMessage } from "formik";
import axios from "axios";
import { Button, Modal, Dropdown, Nav, Navbar } from "react-bootstrap";
import * as yup from "yup";
import { Message } from "./fields";
import { AccessStatus } from "./types";
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN";
axios.defaults.xsrfCookieName = "csrftoken";
let LoginSchema = yup.object().shape({
username: yup.string().required("Username is required."),
password: yup.string().required("Password is required.")
});
let SignupSchema = yup.object().shape({
username: yup.string().required("Username is required."),
email: yup
.string()
.email("Must be a valid email address.")
.required("Email address is required."),
password1: yup.string().required("Password is required."),
password2: yup
.string()
.oneOf([yup.ref("password1"), null], "Passwords don't match")
.required("Confirmation password is required.")
});
const tos = (
<p className="text-muted mt-3">
By creating an account, you agree to the
<a href="/terms/"> Terms of Service</a>. For more information about Compute Studio's privacy
practices, see the Compute Studio <a href="/privacy/">Privacy Statement</a>. We'll occasionally
send you account-related emails.
</p>
);
export const LoginForm = ({ setAuthStatus }) => (
<div>
<Formik
initialValues={{ username: "", password: "" }}
validationSchema={LoginSchema}
onSubmit={(values, actions) => {
let formdata = new FormData();
formdata.append("username", values.username);
formdata.append("password", values.password);
axios
.post("/rest-auth/login/", formdata)
.then(resp => {
setAuthStatus(true);
})
.catch(err => {
if (err.response.status == 400) {
actions.setStatus({ errors: err.response.data });
} else {
throw err;
}
});
}}
render={({ handleSubmit, status }) => (
<Form>
{status && status.errors && status.errors.non_field_errors ? (
<div className="alert alert-danger" role="alert">
{" "}
{status.errors.non_field_errors}
</div>
) : null}
<div className="mt-1">
<label> Username:</label>
<Field name="username" className="form-control" />
<ErrorMessage name="username" render={msg => <Message msg={msg} />} />
</div>
<div className="mt-1">
<label> Password: </label>
<Field name="password" type="password" className="form-control" />
<ErrorMessage name="password" render={msg => <Message msg={msg} />} />
</div>
<Button
onClick={e => {
e.preventDefault();
handleSubmit(e);
}}
variant="primary"
className="mt-2"
>
{" "}
Sign in{" "}
</Button>
</Form>
)}
></Formik>
</div>
);
export const SignupForm = ({ setAuthStatus }) => (
<div>
<Formik
initialValues={{ username: "", email: "", password1: "", password2: "" }}
validationSchema={SignupSchema}
onSubmit={(values, actions) => {
let formdata = new FormData();
formdata.append("username", values.username);
formdata.append("email", values.email);
formdata.append("password1", values.password1);
formdata.append("password2", values.password2);
axios
.post("/rest-auth/registration/", formdata)
.then(resp => {
setAuthStatus(true);
})
.catch(err => {
if (err.response.status == 400) {
actions.setStatus({ errors: err.response.data });
} else {
throw err;
}
});
}}
render={({ handleSubmit, status }) => (
<Form>
{status && status.errors && status.errors.email ? (
<div className="alert alert-danger" role="alert">
{" "}
{status.errors.email}
</div>
) : null}
{status && status.errors && status.errors.username ? (
<div className="alert alert-danger" role="alert">
{" "}
{status.errors.username}
</div>
) : null}
{status && status.errors && status.errors.password1 ? (
<div className="alert alert-danger" role="alert">
{" "}
<ul>
{status.errors.password1.map(msg => (
<li>{msg}</li>
))}
</ul>
</div>
) : null}
<div className="mt-1">
<label> Username:</label>
<Field name="username" className="form-control" />
<ErrorMessage name="username" render={msg => <Message msg={msg} />} />
</div>
<div className="mt-1">
<label> Email:</label>
<Field name="email" className="form-control" type="email" />
<ErrorMessage name="email" render={msg => <Message msg={msg} />} />
</div>
<div className="mt-1">
<label> Password: </label>
<Field name="password1" type="password" className="form-control" />
<ErrorMessage name="password1" render={msg => <Message msg={msg} />} />
</div>
<div className="mt-1">
<label> Password confirmation: </label>
<Field name="password2" type="password" className="form-control" />
<ErrorMessage name="password2" render={msg => <Message msg={msg} />} />
</div>
<Button
onClick={e => {
e.preventDefault();
handleSubmit(e);
}}
variant="success"
className="mt-2"
>
{" "}
Signup{" "}
</Button>
{tos}
</Form>
)}
></Formik>
</div>
);
export const AuthDialog: React.FC<{
show: boolean;
setShow: (show: boolean) => void;
initialAction: "sign-in" | "sign-up";
resetAccessStatus: () => void;
}> = ({ show, setShow, initialAction, resetAccessStatus }) => {
const [action, setAction] = React.useState(initialAction);
const [authenticated, setAuthenticated] = React.useState(false);
const getVariant = action => (action === "sign-in" ? "outline-success" : "outline-primary");
const toggleAction = action => {
setAction(action === "sign-in" ? "sign-up" : "sign-in");
};
if (authenticated) {
resetAccessStatus();
setShow(false);
}
return (
<Modal show={show} onHide={() => setShow(false)}>
<Modal.Header closeButton>
<Modal.Title>You must be logged in to run simulations.</Modal.Title>
</Modal.Header>
<Modal.Body>
<div className="mt-2">
{action === "sign-in" ? (
<LoginForm setAuthStatus={setAuthenticated} />
) : (
<SignupForm setAuthStatus={setAuthenticated} />
)}
</div>
<Button className="mt-3" variant={getVariant(action)} onClick={() => toggleAction(action)}>
{action === "sign-in" ? "Sign up" : "Sign in"}
</Button>
</Modal.Body>
<Modal.Footer>
<Button variant="outline-secondary" onClick={() => setShow(false)}>
Close
</Button>
</Modal.Footer>
</Modal>
);
};
interface AuthButtonState {
show: boolean;
initialAction: "sign-in" | "sign-up";
}
export const AuthButtons: React.FC<{
accessStatus: AccessStatus;
resetAccessStatus: () => void;
}> = ({ accessStatus, resetAccessStatus }) => {
const [state, setState] = React.useState({
show: false,
initialAction: "sign-in"
} as AuthButtonState);
const setShow = show => {
setState({ ...state, ...{ show: show } });
};
if (accessStatus.username) {
return (
<Dropdown className="mobile-mb-1">
<Dropdown.Toggle
className="nav-link btn btn-outline-match-nav dropdown-toggle mb-1"
style={{ display: "inline-block" }}
id="navbarDropdown-prof"
variant={null}
>
{accessStatus.username}
</Dropdown.Toggle>
<Dropdown.Menu className="desktop-menu-right" style={{ width: "180px" }}>
<Dropdown.Item href="/">Dashboard</Dropdown.Item>
<Dropdown.Item href="/publish/">Publish</Dropdown.Item>
<Dropdown.Divider />
<Dropdown.Item href="/users/settings/">Settings</Dropdown.Item>
<Dropdown.Item href="/users/logout/">Sign out</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
);
} else {
return (
<>
{state.show ? (
<AuthDialog setShow={setShow} {...state} resetAccessStatus={resetAccessStatus} />
) : null}
<ul className="navbar-nav mr-auto mt-2 mt-lg-0">
<li className="nav-item mr-2 mobile-mb-3">
<Button
className="btn-match-nav"
style={{ display: "inline-block", border: 0 }}
onClick={() => setState({ show: true, initialAction: "sign-in" })}
>
Sign in
</Button>
</li>
<li className="nav-item mobile-mb-1">
<Button
className="btn-outline-match-nav mb-1 mobile-p-1"
style={{ display: "inline-block" }}
onClick={() => setState({ show: true, initialAction: "sign-up" })}
>
Sign up
</Button>
</li>
</ul>
</>
);
}
};