forked from irinazheltisheva/fastsite-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
78 lines (70 loc) · 2.25 KB
/
App.js
File metadata and controls
78 lines (70 loc) · 2.25 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
import React, { Fragment, useEffect, useState } from 'react'
import axios from 'axios'
import Sites from './Sites'
const App = () => {
const initialFormState = { username: '', password: '' }
const [ user, setUser ] = useState(initialFormState)
const [ auth, setAuth ] = useState(false)
const handleInputChange = event => {
const { name, value } = event.target
setUser({ ...user, [name]: value })
}
const logout = event => {
localStorage.removeItem('token');
setAuth(false);
}
const doAuth = (token) => {
axios.get('/.netlify/functions/auth?token=' + token)
.then((response) => {
console.log(response)
localStorage.setItem('token', token);
setAuth(true)
setUser(initialFormState)
})
.catch((err) => {
if (err.response && err.response.status && err.response.status === 401) {
alert("Invalid Password");
logout();
}else{
console.error(err)
}
})
}
useEffect(() => {
const token = localStorage.getItem('token');
if(token){
doAuth(token)
}
}, []) // eslint-disable-line react-hooks/exhaustive-deps
return (
<div className="container">
<h1>FastSite Console</h1>
{auth ? (
<Fragment>
<div className="right-align"><button onClick={logout} className="button muted-button">
Logout
</button></div>
<Sites />
</Fragment>
) : (
<form
onSubmit={event => {
event.preventDefault()
if (!user.username || !user.password){
alert("Please provide username and password");
return
}
doAuth(user.password);
}}
>
<label>Username</label>
<input type="text" name="username" value={user.username} onChange={handleInputChange} className="text-box"/>
<label>Password</label>
<input type="password" name="password" value={user.password} onChange={handleInputChange} className="text-box"/>
<button>Login</button>
</form>
)}
</div>
)
}
export default App