Skip to content

Commit

Permalink
Merge branch 'main' into adi/otp-signup
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 authored Oct 21, 2023
2 parents 19be41a + 863013d commit f72ea4d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/app/auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ function LoginScreen() {
return (
<SafeAreaView style={globalStyles.container}>
<Text style={globalStyles.h1}>Auth</Text>
<Button title="Login" onPress={() => router.push('/auth/onboarding')} />
<Button title="Sign Up" onPress={() => router.push('/auth/signup')} />
<Button
title="Login"
onPress={() => router.replace('/auth/onboarding')}
/>
<Button title="Sign Up" onPress={() => router.replace('/auth/signup')} />
<Button title="[temp] Home" onPress={() => router.push('/home')} />
</SafeAreaView>
);
Expand Down
20 changes: 12 additions & 8 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,21 +12,25 @@ 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);
const { error, data } = await sessionHandler.signInWithEmail(
email,
password,
);
const { error } = await sessionHandler.signInWithEmail(email, password);

if (error) Alert.alert(error.message);
setLoading(false);
};

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

return (
<View style={globalStyles.auth_container}>
<View style={[globalStyles.verticallySpaced, globalStyles.mt20]}>
Expand Down
10 changes: 5 additions & 5 deletions src/app/auth/onboarding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useSession } from '../../utils/AuthContext';
import supabase from '../../utils/supabase';

function OnboardingScreen() {
const { session, signOut } = useSession();
const { session } = useSession();
const [loading, setLoading] = useState(true);
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
Expand All @@ -30,7 +30,7 @@ function OnboardingScreen() {
.eq('user_id', session?.user.id)
.single();

if (error && status !== 406) {
if (error && status !== 406 && error instanceof Error) {
throw error;
}

Expand Down Expand Up @@ -82,12 +82,12 @@ function OnboardingScreen() {
.eq('user_id', session?.user.id)
.select('*');

if (error) throw error;
if (error && error instanceof Error) throw error;
} else {
// Create user if they don't exist
const { error } = await supabase.from('profiles').insert(updates);

if (error) throw error;
if (error && error instanceof Error) throw error;
}

Alert.alert('Succesfully updated user!');
Expand Down Expand Up @@ -154,7 +154,7 @@ function OnboardingScreen() {
/>
</View>
<View style={globalStyles.verticallySpaced}>
<Button title="Skip" onPress={() => router.push('/home')} />
<Button title="Skip" onPress={() => router.replace('/home')} />
</View>
</ScrollView>
);
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 @@ -48,7 +48,7 @@ export function AuthContextProvider({
setSession(newSession);
});

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

0 comments on commit f72ea4d

Please sign in to comment.