forked from hexlet-rus/runit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignIn.jsx
181 lines (170 loc) · 6.04 KB
/
SignIn.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
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
/* eslint-disable no-console */
import React, { useState, useRef, useEffect } from 'react';
import axios from 'axios';
import { Button, Card, Col, Container, Form, Row } from 'react-bootstrap';
import { object, string } from 'yup';
import { useFormik } from 'formik';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useAuth } from '../hooks';
import routes from '../routes.js';
import classes from './SignIn.module.css';
function SignIn() {
const inputRef = useRef();
const [authFailed, setAuthFailed] = useState(false);
const navigate = useNavigate();
const { t } = useTranslation();
const auth = useAuth();
const [searchParams] = useSearchParams();
const validation = object().shape({
email: string()
.email(t('signIn.validation.correctEmail'))
.required(t('signIn.validation.requiredField')),
});
const formik = useFormik({
initialValues: {
email: '',
password: '',
},
validationSchema: validation,
onSubmit: async (values, actions) => {
try {
actions.setSubmitting(true);
const response = await axios.post(routes.loginPath(), values);
setAuthFailed(false);
auth.logIn();
actions.setSubmitting(false);
navigate(routes.profilePagePath());
return response.data;
} catch (err) {
if (!err.isAxiosError) {
console.log(t('errors.unknown'));
throw err;
}
if (err.response?.status === 401) {
setAuthFailed(true);
inputRef.current.select();
} else {
console.log(t('errors.network'));
throw err;
}
actions.setSubmitting(false);
return values;
}
},
});
useEffect(() => {
const code = searchParams.get('code');
if (!code) {
inputRef?.current?.focus();
return;
}
formik.setSubmitting(true);
const oAuth = async () => {
try {
await auth.oAuth(code);
auth.logIn();
navigate(routes.profilePagePath());
} catch (e) {
formik.setSubmitting(false);
}
};
oAuth();
}, []);
return (
<Container className="h-100 bg-dark" fluid>
<Row className="justify-content-center align-content-center h-100">
<Col xs={12} md={6} xxl={5} className="mt-5 mb-5">
<Card className="shadow-sm bg-dark text-white">
<Card.Body className="p-lg-4 p-xl-5">
<h1 className="mb-4 fw-light">{t('signIn.pageHeader')}</h1>
<div className="pt-lg-3">
<Form onSubmit={formik.handleSubmit} noValidate>
<Form.Group className={classes.formGroup}>
<Form.Label htmlFor="email">
{t('signIn.emailLabel')}
</Form.Label>
<Form.Control
onChange={formik.handleChange}
value={formik.values.email}
onBlur={formik.handleBlur}
className={`form-input bg-dark text-white ${classes.signinInput}`}
name="email"
id="email"
autoComplete="email"
required
isInvalid={
(formik.touched.email && formik.errors.email) ||
authFailed
}
ref={inputRef}
/>
<Form.Control.Feedback type="invalid">
{formik.errors.email}
</Form.Control.Feedback>
</Form.Group>
<Form.Group className={classes.formGroup}>
<Form.Label htmlFor="password">
{t('signIn.passwordLabel')}
</Form.Label>
<Form.Control
onChange={formik.handleChange}
value={formik.values.password}
onBlur={formik.handleBlur}
type="password"
className={`form-input bg-dark text-white ${classes.signinInput}`}
name="password"
id="password"
autoComplete="password"
required
isInvalid={authFailed}
/>
<Form.Control.Feedback type="invalid">
{t('signIn.signInFailed')}
</Form.Control.Feedback>
</Form.Group>
{/* TODO: https://github.com/hexlet-rus/runit/issues/94 */}
{/* <div className="text-end my-3">
<a
className="text-decoration-none small"
href={routes.remindPassPagePath()}
>
{t('signIn.remindPass')}
</a>
</div> */}
<Button
type="submit"
variant="primary"
className="w-100 pb-2 pt-2"
data-disable-with="Войти"
disabled={formik.isSubmitting}
>
{t('signIn.loginButton')}
</Button>
</Form>
</div>
{/* <a
id="github-button"
className="btn btn-block btn-social btn-github text-light pl-0"
href={auth.getOAuthCodeUrl()}
>
{t('signIn.withGithub')}
</a> */}
</Card.Body>
<Card.Footer className="border-top-0 text-center py-3">
<div className="py-lg-2">
<span className="text-muted">
{t('signIn.footer.signUpHeader')}
</span>
<a className="link-light" href={routes.signUpPagePath()}>
{t('signIn.footer.signUp')}
</a>
</div>
</Card.Footer>
</Card>
</Col>
</Row>
</Container>
);
}
export default SignIn;