Polished previous session checker

This commit is contained in:
keannu125 2023-03-06 20:41:45 +08:00
parent c3ae196b6c
commit 9065274a18
5 changed files with 35 additions and 6 deletions

View file

@ -1,14 +1,17 @@
import * as React from "react";
import { Navigate } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { LoginState } from "../../Interfaces/Interfaces";
import { useSelector } from "react-redux";
import { LoginState, OldSessionState } from "../../Interfaces/Interfaces";
export interface props {}
export default function LoginChecker() {
const logged_in = useSelector((state: LoginState) => state.logged_in.value);
if (!logged_in) {
const old_session_checked = useSelector(
(state: OldSessionState) => state.old_session_checked.value
);
if (!logged_in && old_session_checked) {
console.log("Not logged in. Redirecting to login page");
return <Navigate to="/Login" replace />;
}

View file

@ -1,11 +1,11 @@
import * as React from "react";
import { useNavigate } from "react-router-dom";
import { useEffect, useState } from "react";
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { CheckSavedSession, UserInfo } from "../Api/Api";
import { UserInfo } from "../Api/Api";
import { toggle_login } from "../../Features/Redux/Slices/Login/LoginSlice";
import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
import { LoginState } from "../../Interfaces/Interfaces";
import { set_checked } from "../../Features/Redux/Slices/OldSession/OldSessionSlice";
export default function PreviousSessionChecker() {
const dispatch = useDispatch();
const logged_in = useSelector((state: LoginState) => state.logged_in.value);
@ -22,6 +22,7 @@ export default function PreviousSessionChecker() {
console.log("No old session found");
localStorage.removeItem("token");
}
await dispatch(set_checked());
}
check();
}, []);

View file

@ -0,0 +1,17 @@
import { createSlice } from "@reduxjs/toolkit";
export const OldSessionSlice = createSlice({
name: "old_session_checked",
initialState: {
value: false,
},
reducers: {
set_checked: (state) => {
state.value = !state.value;
},
},
});
export const { set_checked } = OldSessionSlice.actions;
export default OldSessionSlice.reducer;

View file

@ -15,6 +15,12 @@ export interface LoginState {
};
}
export interface OldSessionState {
old_session_checked: {
value: boolean;
};
}
export interface LoggedInUserState {
logged_in_user: {
value: {

View file

@ -1,10 +1,12 @@
import { configureStore } from "@reduxjs/toolkit";
import LoginReducer from "../../../Features/Redux/Slices/Login/LoginSlice";
import LoggedInUserReducer from "../../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
import OldSessionReducer from "../../../Features/Redux/Slices/OldSession/OldSessionSlice";
export default configureStore({
reducer: {
logged_in: LoginReducer,
logged_in_user: LoggedInUserReducer,
old_session_checked: OldSessionReducer,
},
});