Skip to content

Commit

Permalink
Clear router stack when on home and logging in/signing up
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Oct 21, 2023
1 parent 9d1fb82 commit 8b22289
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/app/auth/login.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Redirect, Link } from 'expo-router';
import { Link, router } from 'expo-router';
import React, { useState } from 'react';
import { Alert, View } from 'react-native';
import { Button, Input } from 'react-native-elements';
Expand All @@ -12,9 +12,12 @@ function LoginScreen() {
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);

if (sessionHandler.session) {
return <Redirect href="/home" />;
}
const resetAndPushToRouter = (path: string) => {
while (router.canGoBack()) {
router.back();
}
router.replace(path);
};

const signInWithEmail = async () => {
setLoading(true);
Expand All @@ -24,6 +27,10 @@ function LoginScreen() {
setLoading(false);
};

if (sessionHandler.session) {
resetAndPushToRouter('/home');
}

return (
<View style={globalStyles.auth_container}>
<View style={[globalStyles.verticallySpaced, globalStyles.mt20]}>
Expand Down
15 changes: 13 additions & 2 deletions src/app/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Redirect, router } from 'expo-router';
import { router } from 'expo-router';
import { useEffect } from 'react';
import { Text, View } from 'react-native';
import { Button } from 'react-native-elements';
import { SafeAreaView } from 'react-native-safe-area-context';
Expand All @@ -9,7 +10,17 @@ import { useSession } from '../utils/AuthContext';
function SettingsScreen() {
const { session, signOut } = useSession();

if (!session) return <Redirect href="/auth/login" />;
const resetAndPushToRouter = (path: string) => {
while (router.canGoBack()) {
router.back();
}
router.replace(path);
};

useEffect(() => {
if (!session) resetAndPushToRouter('/auth/login');
}, [session]);

return (
<SafeAreaView style={globalStyles.container}>
<Text style={globalStyles.h1}>Settings</Text>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function AuthContextProvider({
setSession(newSession);
});

supabase.auth.onAuthStateChange((event, newSession) => {
supabase.auth.onAuthStateChange((_event, newSession) => {
setSession(newSession);
});
}, []);
Expand Down

0 comments on commit 8b22289

Please sign in to comment.