-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathSignUp.js
106 lines (100 loc) · 3.61 KB
/
SignUp.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
/* eslint-disable react/function-component-definition */
import { React, useState } from 'react';
import { Row, Col, Form, Button } from 'react-bootstrap';
import { showSuccessMessage, showErrorMessage } from '../helpers/alerts';
import { SignupValidation } from '../helpers/validate';
import '../styles/Signup.css';
const SignUp = () => {
const [details, setDetails] = useState({
username: '',
email: '',
password: '',
confirmPassword: '',
});
const [success, setSuccess] = useState('');
const [error, setError] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
setSuccess('');
setError('');
const check = SignupValidation(details);
const regex = /^(?=.*[0-9])(?=.*[!@#$%^&*_])(?=.*[A-Z])[a-zA-Z0-9!@#%^&*_]{8,}$/;
if (!check) {
setError('Invalid Email/Username');
} else if (details.password !== details.confirmPassword) {
setError('Passwords do not match');
} else if (!regex.test(details.password)) {
setError(
'Use 8 or more characters with a mix of upper and lowercase letters, numbers & symbols',
);
} else {
try {
fetch('https://journal-policy-tracker.herokuapp.com/users/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(details),
});
setSuccess('Signup Successful');
setTimeout(() => {
window.location.href = '/login';
}, 800);
} catch (err) {
setError('Signup Failed');
}
}
};
return (
<Row className='signup-padding signup-margin'>
<Col>
<Form className='login-form' onSubmit={handleSubmit}>
<Form.Group className='mb-3' controlId='formBasicUsername'>
{success && showSuccessMessage(success)}
{error && showErrorMessage(error)}
<Form.Label>Username</Form.Label>
<Form.Control
type='text'
placeholder='User123'
name='username'
onChange={(e) => setDetails({ ...details, username: e.target.value })}
/>
</Form.Group>
<Form.Group className='mb-3' controlId='formBasicEmail'>
<Form.Label>Email address</Form.Label>
<Form.Control
type='email'
placeholder='[email protected]'
name='email'
onChange={(e) => setDetails({ ...details, email: e.target.value })}
/>
</Form.Group>
<Form.Group className='mb-3' controlId='formBasicPassword'>
<Form.Label>Password</Form.Label>
<Form.Control
type='password'
placeholder='Password'
name='password'
onChange={(e) => setDetails({ ...details, password: e.target.value })}
/>
<Form.Text id='passwordHelpBlock' muted style={{ fontSize: '12px' }}>
Your password must be 8 or more characters long with a mix of upper and lowercase
letters, numbers & symbols
</Form.Text>
</Form.Group>
<Form.Group className='mb-3' controlId='formBasicConfirmPassword'>
<Form.Label>Confirm Password</Form.Label>
<Form.Control
type='password'
placeholder='Confirm password'
name='confirmPassword'
onChange={(e) => setDetails({ ...details, confirmPassword: e.target.value })}
/>
</Form.Group>
<Button variant='primary' type='submit'>
Sign Up
</Button>
</Form>
</Col>
</Row>
);
};
export default SignUp;