-
Notifications
You must be signed in to change notification settings - Fork 15
/
UserContext.js
143 lines (127 loc) · 3.83 KB
/
UserContext.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
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
import React from "react";
import md5 from 'js-md5';
import cookie from 'js-cookie';
import axios from 'axios';
var UserStateContext = React.createContext();
var UserDispatchContext = React.createContext();
function userReducer(state, action) {
switch (action.type) {
case "LOGIN_SUCCESS":
return { ...state, isAuthenticated: true };
case "LOGIN_FAILURE":
return { ...state, isAuthenticated: false };
case "SIGN_OUT_SUCCESS":
return { ...state, isAuthenticated: false };
case "REGISTER_SUCCESS":
return { ...state, isAuthenticated: false };
case "REGISTER_FAILURE":
return { ...state, isAuthenticated: false };
default: {
throw new Error(`Unhandled action type: ${action.type}`);
}
}
}
function UserProvider({ children }) {
var [state, dispatch] = React.useReducer(userReducer, {
isAuthenticated: !!cookie.get("id_token"),
});
return (
<UserStateContext.Provider value={state}>
<UserDispatchContext.Provider value={dispatch}>
{children}
</UserDispatchContext.Provider>
</UserStateContext.Provider>
);
}
function useUserState() {
var context = React.useContext(UserStateContext);
if (context === undefined) {
throw new Error("useUserState must be used within a UserProvider");
}
return context;
}
function useUserDispatch() {
var context = React.useContext(UserDispatchContext);
if (context === undefined) {
throw new Error("useUserDispatch must be used within a UserProvider");
}
return context;
}
export { UserProvider, useUserState, useUserDispatch, loginUser, signOut, registerUser };
// ###########################################################
function loginUser(dispatch, name, password, history, setIsLoading, setError) {
setError(false);
setIsLoading(true);
password = md5.hex(password);
if (!!name && !!password) {
var loginData = {
username : name,
password : password
}
axios.post('/User/login', loginData)
.then(res => {
if (res.request.response === "Succeed."){
setTimeout(() => {
cookie.set('id_token', 1, { expires: 30 })
setError(null)
setIsLoading(false)
dispatch({ type: 'LOGIN_SUCCESS' })
history.push('/app/dashboard')
}, 300);
}
else{
alert(res.request.response);
}
})
axios.post('/User/searchUserByUsername', {username: name})
.then(res => {
if (res.status === 200 && Object.keys(res.request.response).length > 0){
setTimeout(() => {
const data = JSON.parse(res.request.response);
cookie.set('uuid', data.id, { expires: 30 });
cookie.set('username', data.username, { expires: 30 });
}, 300);
}
})
}
dispatch({ type: "LOGIN_FAILURE" });
setError(true);
setIsLoading(false);
}
function signOut(dispatch, history) {
cookie.remove("id_token");
cookie.remove('uuid');
cookie.remove('username');
dispatch({ type: "SIGN_OUT_SUCCESS" });
history.push("/login");
}
function registerUser(dispatch, name, email, password, history, setIsLoading, setError) {
setError(false);
setIsLoading(true);
if (!!email && !!name && !!password) {
var registerData = {
email : email,
username : name,
password : password,
createdBy : name,
lastUpdatedBy : name
}
axios.post('/User/register', registerData)
.then(res => {
if (res.request.response === "Succeed."){
setTimeout(() => {
setError(null)
setIsLoading(false)
dispatch({ type: 'REGISTER_SUCCESS' })
history.push('/login')
}, 300);
}
else{
alert(res.request.response);
}
})
}
dispatch({ type: "REGISTER_FAILURE" });
setError(true);
setIsLoading(false);
}