forked from hackforla/VRMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuth.jsx
More file actions
162 lines (146 loc) · 4.39 KB
/
Auth.jsx
File metadata and controls
162 lines (146 loc) · 4.39 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
import React, { useState } from 'react';
import { Redirect } from 'react-router-dom';
import { useHistory } from 'react-router-dom';
import { checkUser, checkAuth } from '../../services/user.service';
import { authLevelRedirect } from '../../utils/authUtils';
import {
Typography,
Button,
FormControl,
Box,
TextField
} from '@mui/material';
import useAuth from '../../hooks/useAuth';
import '../../sass/AdminLogin.scss';
/** At the moment only users with the 'admin' accessLevel can login
* and see the dashboard
**/
const Auth = () => {
const LOG_IN = 'LOG_IN';
const ADMIN = 'admin';
const USER = 'user';
const pattern = /\b[a-z0-9._]+@[a-z0-9.-]+\.[a-z]{2,4}\b/i;
const history = useHistory();
const { auth } = useAuth();
const [email, setEmail] = useState('');
const [isDisabled, setIsDisabled] = useState(true);
const [isError, setIsError] = useState(false);
const [errorMessage, setErrorMessage] = useState(' ');
const validateEmail = () => {
if (email.search(pattern) !== -1) {
setIsDisabled(false);
return true;
} else {
setIsDisabled(true);
showError('Please enter a valid email address');
return false;
}
};
const showError = (message) => {
setIsError(true);
setErrorMessage(message);
};
const handleLogin = async (e) => {
e.preventDefault();
const isEmailValid = validateEmail();
if (isEmailValid) {
const userData = await checkUser(email, LOG_IN);
if (userData) {
if (
userData.user.accessLevel !== ADMIN &&
userData.user.accessLevel === USER &&
userData.user.managedProjects.length === 0
) {
showError(
"You don't have the correct access level to view the dashboard"
);
return;
}
const isAuth = await checkAuth(email, LOG_IN);
if (isAuth) {
history.push('/emailsent');
} else {
showError(
'We don’t recognize your email address. Please, create an account.'
);
}
} else {
showError(
'We don’t recognize your email address. Please, create an account.'
);
}
}
};
function handleInputChange(e) {
const inputValue = e.currentTarget.value.toString();
validateEmail();
if (!inputValue) {
setIsDisabled(true);
showError('Please enter a valid email address');
} else {
setIsDisabled(false);
setIsError(false);
setEmail(e.currentTarget.value.toString());
}
}
// This allows users who are not admin, but are allowed to manage projects, to login
let loginRedirect = '';
if (auth?.user) {
loginRedirect = authLevelRedirect(auth.user);
}
return auth?.user ? (
<Redirect to={loginRedirect} />
) : (
<div className="flex-container">
<div className="adminlogin-container">
<div className="adminlogin-headers">
<Typography variant="h3" sx={{fontSize:'2.8em'}}>Welcome Back!</Typography>
</div>
<FormControl
onSubmit={handleLogin}
className="form-check-in"
autoComplete="off"
>
<div className="form-row">
<div className="form-input-text">
<Box className="form-row">
<Box className="form-input-text">
<TextField
label = "Enter your email address:"
type="email"
name="email"
placeholder="Email Address"
required="required"
onChange={handleInputChange}
aria-label="Email Address"
data-test="input-email"
autoComplete="email"
/>
</Box>
</Box>
</div>
</div>
</FormControl>
<div
className="adminlogin-warning"
style={{ visibility: isError ? 'visible' : 'hidden' }}
>
{errorMessage}
</div>
<div className="form-input-button">
<Button
type="submit"
onClick={handleLogin}
className="login-button"
data-test="login-btn"
disabled={isDisabled}
sx={{color: 'black'}}
>
LOGIN
</Button>
</div>
</div>
</div>
);
};
export default Auth;