SigninPage.js
3.67 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
import React, { useState } from 'react';
import { Button, Form, FormGroup, Label, Input} from 'reactstrap';
import { FacebookLoginButton } from 'react-social-login-buttons';
const SigninPage = (props) => {
const [userName, setUserName] = useState('');
const [userPw, setUserPw] = useState('');
const signinApi = (user) => {
return fetch('/api/signin', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
}).then(response => response.json())
}
const handleSubmit = async (e) => {
e.preventDefault();
if (!userName || !userPw) {
return;
}
try {
const response = await signinApi({
username: userName,
password: userPw
});
if (response.message === "Token issue") {
localStorage.setItem("user_token", JSON.stringify(response.token));
alert('Login success');
props.history.push('/');
} else if(response.message === "user does not exist"){
alert('User does not exist');
props.history.push('/signin');
setUserName('');
setUserPw('');
} else if(response.message === "invalid password") {
alert('Invalid Password');
props.history.push('/signin');
setUserName('');
setUserPw('');
} else if(response.message === "server error") {
alert('Server Error');
props.history.push('/signin');
setUserName('');
setUserPw('');
}
} catch (err) {
alert('Signin Failed');
setUserName('');
setUserPw('');
props.history.push('/signin');
}
};
const onChangeUsername = (e) => {
setUserName(e.target.value);
};
const onChangePassword = (e) => {
setUserPw(e.target.value);
};
return (
<>
<Form style={{
width:'100%',
maxWidth:'330px',
padding:'15px',
margin:'auto',
height:'100%'
}}>
<a href='/' style={{'color':'#000000', textDecoration:'none'}}>
<h1 className="text-center">
<span className="font-weight-bold">MEALKHU</span>.com
</h1>
</a>
<h2 className="text-center"><br/>Sign In</h2>
<FormGroup>
<Label>Username</Label>
<Input required="required" value={userName} onChange={onChangeUsername} type="name" placeholder="Enter your name" ></Input>
</FormGroup>
<FormGroup>
<Label>Password</Label>
<Input required="required" type="password" value={userPw} onChange={onChangePassword} placeholder="Enter your password"></Input>
</FormGroup>
<FormGroup>
<Button type="submit" onClick={handleSubmit} className="btn-lg btn-dark btn-block">Sign in</Button>
</FormGroup>
<div className="text-center pt-3">
Or continue with your social account
</div>
<FacebookLoginButton className="mt-3 mb-3"/>
<div className="text-center">
<a href="/signup">Sign up here!</a>
</div>
</Form>
</>
);
};
export default SigninPage;