Skip to content

Commit a7971a6

Browse files
committed
supabase config and authcontext
1 parent 6a1aef0 commit a7971a6

File tree

4 files changed

+139
-9
lines changed

4 files changed

+139
-9
lines changed

app/(auth)/auth.tsx

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { Button, Input } from '@rneui/themed';
2+
import React, { useState } from 'react';
3+
import { Alert, StyleSheet, View, AppState } from 'react-native';
4+
5+
import { supabase } from '../../utils/supabase';
6+
7+
AppState.addEventListener('change', (state) => {
8+
if (state === 'active') {
9+
supabase.auth.startAutoRefresh();
10+
} else {
11+
supabase.auth.stopAutoRefresh();
12+
}
13+
});
14+
15+
export default function Auth() {
16+
const [email, setEmail] = useState('');
17+
const [password, setPassword] = useState('');
18+
const [loading, setLoading] = useState(false);
19+
20+
async function signInWithEmail() {
21+
setLoading(true);
22+
const { error } = await supabase.auth.signInWithPassword({
23+
email,
24+
password,
25+
});
26+
27+
if (error) Alert.alert(error.message);
28+
setLoading(false);
29+
}
30+
31+
async function signUpWithEmail() {
32+
setLoading(true);
33+
const {
34+
data: { session },
35+
error,
36+
} = await supabase.auth.signUp({
37+
email,
38+
password,
39+
});
40+
41+
if (error) Alert.alert(error.message);
42+
if (!session) Alert.alert('Please check your inbox for email verification!');
43+
setLoading(false);
44+
}
45+
46+
return (
47+
<View style={styles.container}>
48+
<View style={[styles.verticallySpaced, styles.mt20]}>
49+
<Input
50+
label="Email"
51+
leftIcon={{ type: 'font-awesome', name: 'envelope' }}
52+
onChangeText={(text) => setEmail(text)}
53+
value={email}
54+
placeholder="[email protected]"
55+
autoCapitalize="none"
56+
/>
57+
</View>
58+
<View style={styles.verticallySpaced}>
59+
<Input
60+
label="Password"
61+
leftIcon={{ type: 'font-awesome', name: 'lock' }}
62+
onChangeText={(text) => setPassword(text)}
63+
value={password}
64+
secureTextEntry
65+
placeholder="Password"
66+
autoCapitalize="none"
67+
/>
68+
</View>
69+
<View style={[styles.verticallySpaced, styles.mt20]}>
70+
<Button title="Sign in" disabled={loading} onPress={() => signInWithEmail()} />
71+
</View>
72+
<View style={styles.verticallySpaced}>
73+
<Button title="Sign up" disabled={loading} onPress={() => signUpWithEmail()} />
74+
</View>
75+
</View>
76+
);
77+
}
78+
79+
const styles = StyleSheet.create({
80+
container: {
81+
marginTop: 40,
82+
padding: 12,
83+
},
84+
verticallySpaced: {
85+
paddingTop: 4,
86+
paddingBottom: 4,
87+
alignSelf: 'stretch',
88+
},
89+
mt20: {
90+
marginTop: 20,
91+
},
92+
});

app/(auth)/index.tsx

-9
This file was deleted.

context/AuthContext.tsx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Session } from '@supabase/supabase-js';
2+
import { createContext, PropsWithChildren, useContext, useEffect, useState } from 'react';
3+
import { ActivityIndicator } from 'react-native';
4+
5+
import { supabase } from '~/utils/supabase';
6+
7+
const AuthContext = createContext({});
8+
export default function AuthContextProvider({ children }: PropsWithChildren) {
9+
const [session, setSession] = useState<Session | null>(null);
10+
const [isReady, setIsReady] = useState(false);
11+
12+
useEffect(() => {
13+
supabase.auth.getSession().then(({ data: { session } }) => {
14+
setSession(session);
15+
setIsReady(true);
16+
});
17+
18+
const { data: subscription } = supabase.auth.onAuthStateChange((_event, session) => {
19+
setSession(session);
20+
});
21+
22+
return () => subscription?.unsubscribe();
23+
}, []);
24+
25+
if (!isReady) {
26+
return <ActivityIndicator />;
27+
}
28+
return (
29+
<AuthContext.Provider value={{ session, user: session?.user }}>{children}</AuthContext.Provider>
30+
);
31+
}
32+
33+
export const useAuth = () => useContext(AuthContext);

utils/supabase.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import AsyncStorage from '@react-native-async-storage/async-storage';
2+
import { createClient } from '@supabase/supabase-js';
3+
4+
const supabaseUrl = process.env.EXPO_PUBLIC_SUPABASE_URL;
5+
const supabaseAnonKey = process.env.EXPO_PUBLIC_SUPABASE_ANON_KEY;
6+
7+
export const supabase = createClient(supabaseUrl || '', supabaseAnonKey || '', {
8+
auth: {
9+
storage: AsyncStorage,
10+
autoRefreshToken: true,
11+
persistSession: true,
12+
detectSessionInUrl: false,
13+
},
14+
});

0 commit comments

Comments
 (0)