-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathauthAction.js
146 lines (126 loc) · 4.1 KB
/
authAction.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
144
145
146
import { SET_CURRENT_USER, GET_USER_PROFILE, PASSWORD_SUCCESSFULLY_CHANGED, PASSWORD_CHANGE_REQUEST_SUCCESS, SET_ADMIN } from './types';
import axios from 'axios';
import { setAuthToken } from '../utils/setAuthToken';
import jwt_decode from 'jwt-decode';
import { errorHandler } from '../utils/errorHandler';
import { setRequestStatus } from '../utils/setRequestStatus';
import { BASE_URL } from './baseApi';
import { customErrorHandler } from '../utils/customErrorHandler'
let forgotPasswordToken = "";
// to register user
export const registerUser = (userInfo, history) => async (dispatch) => {
try {
const res = await axios.post(`${BASE_URL}/user`, userInfo);
dispatch(setRequestStatus(false));
if(res.status === 201) {
dispatch(setRequestStatus(true));
dispatch({
type: GET_USER_PROFILE,
payload: res.data.user
})
history.push('/');
}
} catch(error) {
console.log('register error ', error)
dispatch(errorHandler(error));
}
}
// to authenticate user
export const loginUser = (userInfo, history) => async (dispatch) => {
try {
console.log("LOGGING IN...", userInfo);
console.log('base url ', BASE_URL);
const res = await axios.post(`${BASE_URL}/auth/login`, userInfo);
dispatch(setRequestStatus(false));
if(res.status === 200){
const token = res.data.user.tokens.slice(-1)[0].token;
dispatch(setRequestStatus(true));
localStorage.setItem("jwtToken", (token));
setAuthToken(token);
// update state with user
const decodedData = await jwt_decode(token);
localStorage.setItem('userId', decodedData._id)
dispatch(setCurrentUser(decodedData));
// update user role in localStorage
localStorage.setItem('admin', res.data.user.isAdmin)
// store orgId in localStorage
localStorage.setItem('orgId', res.data.user.orgId);
// if user is admin update admin in store
dispatch({
type: SET_ADMIN,
payload: res.data.user.isAdmin
})
history.push("/dashboard");
}
} catch(error) {
console.log('login error ', error?.response)
dispatch(customErrorHandler(error?.response?.data || ""));
}
}
// forgot password
export const forgotPassword = (email) => async (dispatch) => {
try {
const res = await axios.patch(`${BASE_URL}/user/password_reset/request/`, email);
dispatch(setRequestStatus(false));
if(res.status === 200){
dispatch(setRequestStatus(true));
console.log("Forgot password request sent!!");
forgotPasswordToken = res.data.token;
dispatch({
type: PASSWORD_CHANGE_REQUEST_SUCCESS,
payload: res.data.token
})
}
} catch (error) {
dispatch(errorHandler(error));
}
}
// update password
export const changePassword = (passObj) => async (dispatch) => {
try {
const res = await axios.patch(
`${BASE_URL}/user/password_reset/${forgotPasswordToken}`,
passObj
);
dispatch(setRequestStatus(false));
if(res.status === 200){
dispatch(setRequestStatus(true));
console.log("Password updated!", res.data);
// show password updated notification from here
dispatch({
type: PASSWORD_SUCCESSFULLY_CHANGED,
payload: res.data.updated
})
}
} catch(error) {
dispatch(errorHandler(error));
}
}
// to logout user
export const logoutUser = () => async (dispatch) => {
try {
console.log('Logging out!!')
// clear token from backend
const res = await axios.post(`${BASE_URL}/user/logout`)
if (res.status === 200) {
// remove all keys from the localStorage except the orgId
const orgId = localStorage.getItem('orgId');
localStorage.clear()
localStorage.setItem('orgId', orgId)
// delete authorization from the header
setAuthToken(false);
// set user to {}
setCurrentUser({});
// move to home
window.location.href = "/";
}
} catch (error) {
dispatch(errorHandler(error))
}
}
export const setCurrentUser = (decodedData) => {
return {
type: SET_CURRENT_USER,
payload: decodedData
}
}