feat: billing guest access

This commit is contained in:
2024-08-08 07:54:43 +03:00
parent 925f1b4407
commit 1ad96e3a69
36 changed files with 644 additions and 53 deletions

View File

@@ -1,8 +1,10 @@
import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import {jwtDecode, JwtPayload} from "jwt-decode";
interface AuthState {
isAuthorized: boolean;
accessToken: string;
isGuest: boolean;
}
const initialState = (): AuthState => {
@@ -12,7 +14,8 @@ const initialState = (): AuthState => {
}
return {
accessToken: "",
isAuthorized: false
isAuthorized: false,
isGuest: false
}
}
@@ -21,8 +24,19 @@ const authSlice = createSlice({
initialState,
reducers: {
login: (state, action: PayloadAction<{ accessToken: string }>) => {
state.accessToken = action.payload.accessToken;
state.isAuthorized = true;
try {
const {sub} = jwtDecode<JwtPayload>(action.payload.accessToken);
state.accessToken = action.payload.accessToken;
state.isAuthorized = true;
if (sub === "guest")
state.isGuest = true;
} catch (_) {
const url = window.location.href;
const urlObj = new URL(url);
urlObj.search = '';
history.replaceState(null, '', urlObj);
window.location.reload();
}
},
logout: (state) => {
state.isAuthorized = false;