Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Up Supabase Backend and Email Authentication for Medica Project #130

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"favicon": "./assets/images/favicon.png"
},
"plugins": [
"@react-native-google-signin/google-signin",
"expo-router",
[
"expo-local-authentication",
Expand Down
2 changes: 1 addition & 1 deletion app/(auth)/SignIn&SignOut/LetsYouIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const LetsYouIn = () => {
<SvgXml xml={theme === "dark" ? OrLine : greyOrLine} />

<TouchableOpacity
onPress={() => router.push("/(auth)/SignIn&SignOut/YourProfile")}
onPress={() => router.push("/(auth)/SignIn&SignOut/SignInBlankForm")}
style={styles.signinBtn}
>
<Text style={[Typography.bold.large, { color: Colors.others.white }]}>
Expand Down
38 changes: 32 additions & 6 deletions app/(auth)/SignIn&SignOut/SignInBlankForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
TextInput,
TouchableWithoutFeedback,
Pressable,
AppState,
Alert
} from "react-native";
import { Ionicons } from "@expo/vector-icons";
import CheckBox from "expo-checkbox";
Expand All @@ -26,10 +28,20 @@ import {
blackArrow,
} from "@/components/Icons/Icons";
import { StatusBar } from "expo-status-bar";
import { supabase } from "@/lib/supabase";

AppState.addEventListener('change', (state) => {
if(state === 'active'){
supabase.auth.startAutoRefresh()
}else{
supabase.auth.stopAutoRefresh()
}
})

const Login = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [loading, setLoading] = useState(false)
const [secureTextEntry, setSecureTextEntry] = useState(true);
const [isChecked, setIsChecked] = useState(false);
const [emailFocused, setEmailFocused] = useState(false);
Expand Down Expand Up @@ -66,6 +78,21 @@ const Login = () => {
setPasswordFocused(false);
};

async function signInWithEmail(){
setLoading(true)
const{error} = await supabase.auth.signInWithPassword({
email: email,
password: password,
})
if(error){
Alert.alert(error.message)
setLoading(false)
}else{
await router.push('/(app)/ActionMenu');
setLoading(false);
}
}

return (
<View
style={[
Expand All @@ -88,7 +115,6 @@ const Login = () => {
>
Login to Your Account
</Text>
{/* </View> */}
</View>

<View style={styles.Buttons}>
Expand All @@ -112,7 +138,7 @@ const Login = () => {
keyboardType="email-address"
placeholderTextColor="#9E9E9E"
value={email}
onChangeText={handleEmailChange}
onChangeText={(text) => setEmail(text)}
onFocus={handleEmailFocus}
onBlur={handleEmailBlur}
/>
Expand All @@ -139,7 +165,7 @@ const Login = () => {
placeholderTextColor="#9E9E9E"
secureTextEntry={secureTextEntry}
value={password}
onChangeText={handlePasswordChange}
onChangeText={(text) => setPassword(text)}
onFocus={handlePasswordFocus}
onBlur={handlePasswordBlur}
/>
Expand Down Expand Up @@ -179,8 +205,8 @@ const Login = () => {
</View>

<View>
<TouchableOpacity
onPress={() => router.push("/(app)/ActionMenu")}
<TouchableOpacity disabled={loading}
onPress={() => signInWithEmail()}
style={styles.signinBtn}
>
<Text style={styles.signText}>Sign in</Text>
Expand Down Expand Up @@ -249,7 +275,7 @@ const Login = () => {
{ color: theme === "dark" ? "#FFFFFF" : Colors.grayScale._500 },
]}
>
Don’t have an account?{" "}
Don’t have an account?
</Text>
<Text
style={[
Expand Down
44 changes: 41 additions & 3 deletions app/(auth)/SignIn&SignOut/SignUpBlankForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useContext, useState } from "react";
import {
Alert,
StyleSheet,
Text,
View,
Expand All @@ -9,6 +10,7 @@ import {
TouchableWithoutFeedback,
KeyboardAvoidingView,
Platform,
AppState,
} from "react-native";
import { Ionicons } from "@expo/vector-icons";
import CheckBox from "expo-checkbox";
Expand All @@ -25,10 +27,21 @@ import {
} from "@/components/Icons/Icons";
import { SvgXml } from "react-native-svg";
import { appleBlackIcon, appleWhiteIcon } from "@/constants/icon";
import { supabase } from "@/lib/supabase";

AppState.addEventListener('change', (state) => {
if(state === 'active'){
supabase.auth.startAutoRefresh()
}else{
supabase.auth.stopAutoRefresh()
}
})

const Signup = () => {

const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [loading, setLoading] = useState(false);
const [secureTextEntry, setSecureTextEntry] = useState(true);
const [isChecked, setIsChecked] = useState(false);
const [isFocused, setIsFocused] = useState(false);
Expand Down Expand Up @@ -61,6 +74,25 @@ const Signup = () => {
setPasswordFocused(false);
};

async function signUpWithEmail(){
setLoading(true)
const{
data:{ session },
error,
} = await supabase.auth.signUp({
email: email,
password: password,
})

if(error){
Alert.alert(error.message)
if(!session) Alert.alert('Please check your inbox for email verification!')
setLoading(false)
}else{
await router.push('/(auth)/SignIn&SignOut/YourProfile')
}
}

return (
<View
style={[
Expand Down Expand Up @@ -108,9 +140,10 @@ const Signup = () => {
keyboardType="email-address"
placeholderTextColor="#9E9E9E"
value={email}
onChangeText={handleEmailChange}
onChangeText={(text) => setEmail(text)}
onFocus={handleEmailFocused}
onBlur={handleEmailBlur}
autoCapitalize={'none'}
/>
</View>

Expand All @@ -131,9 +164,12 @@ const Signup = () => {
placeholderTextColor="#9E9E9E"
secureTextEntry={secureTextEntry}
value={password}
onChangeText={handlePasswordChange}
onChangeText={(text) => setPassword(text)}
onFocus={handlePasswordFocused}
onBlur={handlePasswordBlur}
autoCapitalize={'none'}


/>
<View
style={[
Expand Down Expand Up @@ -173,7 +209,7 @@ const Signup = () => {
</View>

<TouchableOpacity
onPress={() => router.push("/(auth)/SignIn&SignOut/SignInBlankForm")}
onPress={() => signUpWithEmail()}
style={styles.signinBtn}
>
<Text style={[Typography.bold.large, { color: Colors.others.white }]}>
Expand Down Expand Up @@ -335,6 +371,8 @@ const styles = StyleSheet.create({
email: {
fontSize: 16,
flex: 1,


},
signupText: {
color: "#246BFD",
Expand Down
14 changes: 14 additions & 0 deletions lib/supabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import { createClient } from '@supabase/supabase-js'

const supabaseUrl = "https://smyciiybvwvwisdmnrtk.supabase.co";
const supabaseAnonKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNteWNpaXlidnd2d2lzZG1ucnRrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTc1NjMyMjAsImV4cCI6MjAzMzEzOTIyMH0.Ca0dmDaXJDXXKdwnbW8I_Itpy10Q_oh1ailg5KArpGc";

export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
auth: {
storage: AsyncStorage,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: false,
},
})
Loading
Loading