Added previous session checker

This commit is contained in:
keannu125 2023-04-16 18:37:51 +08:00
parent 3c188f9ad7
commit d2c6667536
2 changed files with 35 additions and 0 deletions

View file

@ -1,6 +1,7 @@
import { View, Image, Text } from "react-native";
import type { DrawerNavigationOptions } from "@react-navigation/drawer";
import AppIcon from "../../Icons/AppIcon/AppIcon";
import PreviousSessionChecker from "../../PreviousSessionChecker/PreviousSessionChecker";
const DrawerScreenSettings: DrawerNavigationOptions = {
headerTitleStyle: { color: "white", fontSize: 26 },
unmountOnBlur: true,
@ -18,6 +19,7 @@ const DrawerScreenSettings: DrawerNavigationOptions = {
<View
style={{ flexDirection: "row", marginRight: 16, alignItems: "center" }}
>
<PreviousSessionChecker />
<AppIcon size={32} color="white" />
</View>
),

View file

@ -0,0 +1,33 @@
import * as React from "react";
import { View, Text, TextInput, ScrollView } from "react-native";
import { useEffect, useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import { UserInfo } from "../Api/Api";
import { Toggle_Login } from "../../Features/Redux/Slices/LoginSlice/LoginSlice";
import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
import { set_checked } from "../../Features/Redux/Slices/OldSession/OldSessionSlice";
import { RootState } from "../../Features/Redux/Store/Store";
export default function PreviousSessionChecker() {
const dispatch = useDispatch();
const logged_in = useSelector((state: RootState) => state.logged_in.value);
// Function to check for previous login session
const check = useCallback(async () => {
if (await UserInfo()) {
if (logged_in !== true) {
console.log("Previous session found. Restoring");
await dispatch(Toggle_Login());
await dispatch(SetUser(await UserInfo()));
}
} else {
console.log("No old session found");
localStorage.removeItem("token");
}
await dispatch(set_checked());
}, [dispatch, logged_in]);
useEffect(() => {
if (!logged_in) {
check();
}
}, [check, logged_in]);
return <View />;
}