diff --git a/src/Components/LoginChecker/LoginChecker.tsx b/src/Components/LoginChecker/LoginChecker.tsx index a32ca87..c99bee3 100644 --- a/src/Components/LoginChecker/LoginChecker.tsx +++ b/src/Components/LoginChecker/LoginChecker.tsx @@ -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 ; } diff --git a/src/Components/PreviousSessionChecker/PreviousSessionChecker.tsx b/src/Components/PreviousSessionChecker/PreviousSessionChecker.tsx index 76b6dfd..d0945af 100644 --- a/src/Components/PreviousSessionChecker/PreviousSessionChecker.tsx +++ b/src/Components/PreviousSessionChecker/PreviousSessionChecker.tsx @@ -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(); }, []); diff --git a/src/Features/Redux/Slices/OldSession/OldSessionSlice.tsx b/src/Features/Redux/Slices/OldSession/OldSessionSlice.tsx new file mode 100644 index 0000000..04e37e3 --- /dev/null +++ b/src/Features/Redux/Slices/OldSession/OldSessionSlice.tsx @@ -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; diff --git a/src/Interfaces/Interfaces.tsx b/src/Interfaces/Interfaces.tsx index 403b08f..35ce41a 100644 --- a/src/Interfaces/Interfaces.tsx +++ b/src/Interfaces/Interfaces.tsx @@ -15,6 +15,12 @@ export interface LoginState { }; } +export interface OldSessionState { + old_session_checked: { + value: boolean; + }; +} + export interface LoggedInUserState { logged_in_user: { value: { diff --git a/src/Plugins/Redux/Store/Store.tsx b/src/Plugins/Redux/Store/Store.tsx index 169e668..a7e36b1 100644 --- a/src/Plugins/Redux/Store/Store.tsx +++ b/src/Plugins/Redux/Store/Store.tsx @@ -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, }, });