source: jobvista-frontend/src/redux/reducers/authReducer.js@ 4d97b63

main
Last change on this file since 4d97b63 was befb988, checked in by 223021 <daniel.ilievski.2@…>, 5 months ago

Added an edit profile page for both job seekers and recruiters, where they can upload profile pictures/company logos and edit their profile data. Added profile page specifically for recruiters. Refactored existing code.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import { createSlice } from '@reduxjs/toolkit';
2import { AUTH_TOKEN } from "../../axios/axiosInstance";
3import {isExpired} from "react-jwt";
4import {CURRENT_USER, SIGN_IN, SIGN_OUT, UPDATE_TOKEN} from "../actionTypes";
5import {jwtDecode} from "jwt-decode";
6
7const initialState = {
8 currentUser: "",
9 token: ""
10};
11
12const AuthReducer = (state = initialState, action) => {
13 switch (action.type) {
14 case SIGN_IN:
15 localStorage.setItem(AUTH_TOKEN, action.payload.token);
16 //localStorage.setItem(CURRENT_USER, JSON.stringify(action.payload.user));
17 return {
18 ...state,
19 currentUser: action.payload.user,
20 token: action.payload.token
21 };
22 case UPDATE_TOKEN:
23 let token = action.payload;
24 let decodedToken;
25 let currentUser = "";
26 if(token !=null) {
27 try {
28 decodedToken = jwtDecode(token);
29 } catch (error) {
30 console.log("Failed to decode token: " + error)
31 }
32 }
33
34 if(!isExpired(token)) {
35 localStorage.setItem(AUTH_TOKEN, token);
36 currentUser = {
37 name: decodedToken.name,
38 role: decodedToken.role,
39 access: decodedToken.access,
40 id: decodedToken.id
41 };
42 } else {
43 //localStorage.removeItem(CURRENT_USER);
44 localStorage.removeItem(AUTH_TOKEN);
45 currentUser = "";
46 token = "";
47 }
48 return {
49 ...state,
50 currentUser: currentUser,
51 token: token
52 };
53 case SIGN_OUT:
54 //localStorage.removeItem(CURRENT_USER);
55 localStorage.removeItem(AUTH_TOKEN);
56 return {
57 ...state,
58 currentUser: "",
59 token: "",
60 }
61
62 default:
63 return {
64 ...state,
65 };
66 }
67}
68export default AuthReducer
69
70//naredna prilika :(
71// const authSlice = createSlice({
72// name: 'auth',
73// initialState,
74// reducers: {
75// signIn: (state, action) => {
76// console.log("stigam tuka")
77// const { token, user } = action.payload;
78// localStorage.setItem(AUTH_TOKEN, token);
79// localStorage.setItem("CURRENT_USER", JSON.stringify(user));
80// state.currentUser = user;
81// state.token = token;
82// },
83// }
84// });
85//
86// //export const { signIn, signOut } = authSlice.actions;
87// export default authSlice.reducer;
Note: See TracBrowser for help on using the repository browser.