feat: hide statistics, work time, expenses and finances from regular users

This commit is contained in:
2024-12-09 20:17:32 +04:00
parent 2ee0ef3a52
commit d5598a10b8
4 changed files with 66 additions and 24 deletions

View File

@@ -1,10 +1,11 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { jwtDecode, JwtPayload } from "jwt-decode";
import { jwtDecode, JwtPayload as JwtPayloadBase } from "jwt-decode";
interface AuthState {
isAuthorized: boolean;
accessToken: string;
isGuest: boolean;
role: string;
}
const initialState = (): AuthState => {
@@ -16,20 +17,26 @@ const initialState = (): AuthState => {
accessToken: "",
isAuthorized: false,
isGuest: false,
role: "user",
};
};
interface JwtPayload extends JwtPayloadBase {
role: string;
}
const authSlice = createSlice({
name: "auth",
initialState,
reducers: {
login: (state, action: PayloadAction<{ accessToken: string }>) => {
try {
const { sub } = jwtDecode<JwtPayload>(
action.payload.accessToken
const { sub, role } = jwtDecode<JwtPayload>(
action.payload.accessToken,
);
state.accessToken = action.payload.accessToken;
state.isAuthorized = true;
state.role = role;
if (sub === "guest") state.isGuest = true;
} catch (_) {
const url = window.location.href;