|
| 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 | + |
| 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 | +}); |
0 commit comments