From 90e60250cd5f5a0a57e898dadc537c88b4ca451a Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 4 Jul 2023 21:01:56 +0800 Subject: [PATCH] Redirect to onboarding page if needed and added some missing functionality to login and logout button --- src/components/Api/Api.tsx | 2 +- .../DrawerSettings/CustomDrawerContent.tsx | 2 +- src/routes/Login/Login.tsx | 29 +++++++++++++++++-- src/routes/Revalidation/Revalidation.tsx | 12 +++----- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/components/Api/Api.tsx b/src/components/Api/Api.tsx index 97dbec0..158f1db 100644 --- a/src/components/Api/Api.tsx +++ b/src/components/Api/Api.tsx @@ -70,7 +70,7 @@ export function UserLogin(user: LoginParams) { return [true]; }) .catch((error) => { - console.log("Login Failed:" + error.response.data); + console.log("Login Failed:" + JSON.stringify(error.response.data)); return [false, error.response.data]; }); } diff --git a/src/components/DrawerSettings/CustomDrawerContent.tsx b/src/components/DrawerSettings/CustomDrawerContent.tsx index 21332e3..f453b46 100644 --- a/src/components/DrawerSettings/CustomDrawerContent.tsx +++ b/src/components/DrawerSettings/CustomDrawerContent.tsx @@ -49,7 +49,7 @@ export default function CustomDrawerContent(props: {}) { onPress={async () => { dispatch(await clear()); await AsyncStorage.clear(); - navigation.navigate("Home"); + navigation.navigate("Login"); }} > diff --git a/src/routes/Login/Login.tsx b/src/routes/Login/Login.tsx index d0b9cc7..1d8daef 100644 --- a/src/routes/Login/Login.tsx +++ b/src/routes/Login/Login.tsx @@ -14,9 +14,10 @@ import LoginIcon from "../../icons/LoginIcon/LoginIcon"; import Button from "../../components/Button/Button"; import { useNavigation } from "@react-navigation/native"; import { RootDrawerParamList } from "../../interfaces/Interfaces"; -import { UserLogin } from "../../components/Api/Api"; +import { UserInfo, UserLogin } from "../../components/Api/Api"; import { ParseLoginError } from "../../components/ParseError/ParseError"; import AnimatedContainer from "../../components/AnimatedContainer/AnimatedContainer"; +import { setUser as setStateUser } from "../../features/redux/slices/AuthSlice/AuthSlice"; export default function Login() { const navigation = useNavigation(); @@ -77,10 +78,32 @@ export default function Login() { await UserLogin({ username: user.username, password: user.password, - }).then((result) => { + }).then(async (result) => { if (result[0]) { setUser({ ...user, username: "", password: "", error: "" }); - navigation.navigate("Onboarding"); + let user_info = await UserInfo(); + dispatch(setStateUser(user_info)); + // Redirect to onboarding if no year level, course, or semester specified + console.log( + "Debug", + !( + user_info.year_level || + user_info.course || + user_info.semester + ) + ); + if ( + !( + user_info.year_level || + user_info.course || + user_info.semester + ) + ) { + navigation.navigate("Onboarding"); + } else { + navigation.navigate("Home"); + } + console.log(JSON.stringify(user_info)); } else { setUser({ ...user, diff --git a/src/routes/Revalidation/Revalidation.tsx b/src/routes/Revalidation/Revalidation.tsx index 622cebf..cf1a791 100644 --- a/src/routes/Revalidation/Revalidation.tsx +++ b/src/routes/Revalidation/Revalidation.tsx @@ -1,11 +1,7 @@ import * as React from "react"; import styles from "../../styles"; import { View, Text, ActivityIndicator } from "react-native"; -import { - TokenRefresh, - UserInfo, - setAccessToken, -} from "../../components/Api/Api"; +import { TokenRefresh, UserInfo } from "../../components/Api/Api"; import { useDispatch } from "react-redux"; import { colors } from "../../styles"; import { useEffect, useState } from "react"; @@ -23,12 +19,12 @@ export default function Revalidation() { TokenRefresh().then(async (response) => { if (response[0]) { await dispatch(setUser(await UserInfo())); - setTimeout(() => { + await setTimeout(() => { navigation.navigate("Home"); }, 700); } else { - setState("Session expired"); - setTimeout(() => { + await setState("Session expired"); + await setTimeout(() => { navigation.navigate("Login"); }, 700); }