Skip to content

Commit e4511e1

Browse files
committed
fixes admin login bug with lost cookie bug
1 parent f962081 commit e4511e1

File tree

5 files changed

+26
-39
lines changed

5 files changed

+26
-39
lines changed

src/App.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import "./App.css";
44
import { Provider } from "react-redux";
55
import store from "./store";
66
import jwt_decode from "jwt-decode";
7-
import { setAuthToken } from "./utils/setAuthToken";
7+
import { allowCredentialsInHeader } from "./utils/allowCredentialsInHeader";
88
import { setCurrentUser, logoutUser } from "./actions/authAction";
99
import "./css/main.scss";
1010
import ReactGA from "react-ga";
1111

12+
allowCredentialsInHeader()
13+
1214
function App() {
1315
useEffect(() => {
1416
ReactGA.initialize("UA-173245995-1");
15-
setAuthToken()
17+
1618
});
1719
return (
1820
<Provider store={store}>

src/actions/adminAction.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import axios from 'axios'
22
import { errorHandler } from '../utils/errorHandler'
33
import { setRequestStatus } from '../utils/setRequestStatus'
44
import { SET_ADMIN, GET_ADMIN } from './types'
5-
import { setAuthToken } from '../utils/setAuthToken'
65
import jwt_decode from 'jwt-decode';
76
import { setCurrentUser } from './authAction'
87
import { BASE_URL } from './baseApi'
@@ -27,31 +26,27 @@ export const createAdmin = (adminInfo) => async (dispatch) => {
2726
export const loginAdmin = (adminInfo, history) => async (dispatch) => {
2827
try {
2928
const res = await axios.post(`${BASE_URL}/auth/login/`, adminInfo)
30-
dispatch(setRequestStatus(false));
31-
if (res.status === 200) {
29+
dispatch(setRequestStatus(false));
30+
if (res.status === 200) {
31+
dispatch(setRequestStatus(true));
3232

33-
const token = res.data.token;
34-
dispatch(setRequestStatus(true));
33+
// update state with user
34+
localStorage.setItem('userId', res.data.user)
35+
dispatch(setCurrentUser(res.data.user._id));
3536

36-
localStorage.setItem("jwtToken", (token));
37-
setAuthToken(token);
37+
// update localStorage with admin status
38+
localStorage.setItem('username', `${res.data.user.name.firstName} ${res.data.user.name.lastName}`)
39+
localStorage.setItem('admin', res.data.user.isAdmin)
40+
localStorage.setItem('ticketModerator', res.data.user.isTicketsModerator)
41+
localStorage.setItem('orgId', res.data.user.orgId);
3842

39-
// update state with user
40-
const decodedData = await jwt_decode(token);
41-
localStorage.setItem('userId', decodedData._id)
42-
dispatch(setCurrentUser(decodedData));
43-
44-
// update localStorage with admin status
45-
localStorage.setItem('admin', true)
46-
47-
dispatch({
48-
type: SET_ADMIN,
49-
payload: true
50-
})
51-
52-
history.push("/dashboard");
43+
dispatch({
44+
type: SET_ADMIN,
45+
payload: res.data.user.isAdmin
46+
})
47+
history.push("/dashboard");
5348
}
5449
} catch (error) {
5550
dispatch(errorHandler(error))
5651
}
57-
}
52+
}

src/actions/authAction.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { SET_CURRENT_USER, GET_USER_PROFILE, PASSWORD_SUCCESSFULLY_CHANGED, PASSWORD_CHANGE_REQUEST_SUCCESS, SET_ADMIN } from './types';
22
import axios from 'axios';
3-
import { setAuthToken } from '../utils/setAuthToken';
43
import { errorHandler } from '../utils/errorHandler';
54
import { setRequestStatus } from '../utils/setRequestStatus';
65
import { BASE_URL } from './baseApi';
@@ -146,8 +145,6 @@ export const logoutUser = () => async (dispatch) => {
146145
const orgId = localStorage.getItem('orgId');
147146
localStorage.clear()
148147
localStorage.setItem('orgId', orgId)
149-
// delete authorization from the header
150-
setAuthToken(false);
151148
// set user to {}
152149
setCurrentUser({});
153150
// move to home
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import axios from "axios";
2+
3+
export const allowCredentialsInHeader = () => {
4+
axios.defaults.withCredentials = true;
5+
}

src/utils/setAuthToken.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)