All 3 auth methods work & update public profile table as well

This commit is contained in:
2025-03-10 13:59:04 -05:00
parent 07bf94d393
commit f9fd5dafc5
6 changed files with 140 additions and 83 deletions

View File

@ -1,11 +1,12 @@
import React, { useState, useEffect } from 'react';
import { supabase } from '@/lib/supabase';
import * as AppleAuthentication from 'expo-apple-authentication'
import * as AppleAuthentication from 'expo-apple-authentication';
import { useColorScheme } from '@/hooks/useColorScheme';
import { ThemedView } from '@/components/theme';
import { StyleSheet, Platform } from 'react-native';
import { StyleSheet, Platform } from 'react-native';
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import type { updateUser } from '@/constants/Types';
const AppleSignInButton = () => {
const scheme = useColorScheme() ?? 'dark';
@ -21,18 +22,14 @@ const AppleSignInButton = () => {
//const projectId = Constants.expoConfig?.extra?.projectId;
//if (!projectId) throw new Error('No projectId found in expo.config.json');
//const pushToken = await Notifications.getExpoPushTokenAsync({
//projectId,
//projectId,
//});
if (credential.identityToken) {
const email = credential.email;
const full_name = (
credential.fullName &&
credential.fullName.givenName &&
credential.fullName.familyName
)
? `${credential.fullName.givenName} ${credential.fullName.familyName}`
: null;
const full_name =
credential.fullName && credential.fullName.givenName && credential.fullName.familyName
? `${credential.fullName.givenName} ${credential.fullName.familyName}`
: null;
const {
error,
data: { user, session },
@ -40,32 +37,32 @@ const AppleSignInButton = () => {
provider: 'apple',
token: credential.identityToken,
});
console.log(JSON.stringify({ error, user }, null, 2))
if (!error) {
if (user && session) {
const data: any = {};
if (email) data.email = email;
if (full_name) data.full_name = full_name;
const { error: updateError } = await supabase.auth.updateUser({
console.log(JSON.stringify({ error, user }, null, 2));
if (!error && session) {
if (email) {
const data: updateUser = {
email,
full_name: full_name ?? '',
};
const { error: authUpdateError } = await supabase.auth.updateUser({
data,
})
const { error: updateError } = await supabase
.from('profiles')
.upsert({
id: session.user.id,
full_name,
email,
provider: 'apple',
updated_at: new Date(),
});
if (updateError) {
console.error('Error updating user metadata:', updateError);
}
const { error: updateProfileError } =
await supabase.from('profiles').upsert({
id: session.user.id,
username: data?.email.split('@')[0],
full_name: data?.full_name,
updated_at: new Date(),
});
if (updateProfileError) {
console.error('Error updating profile:', updateProfileError);
}
}
}
} else {
throw new Error('No identityToken.')
throw new Error('No identityToken.');
}
} catch (e: any) {
if (e.code === 'ERR_REQUEST_CANCELED') {
@ -76,26 +73,26 @@ const AppleSignInButton = () => {
console.log('Error signing in with Apple:', e);
}
}
}
};
if (Platform.OS !== 'ios') return <ThemedView />;
else return (
<ThemedView style={styles.verticallySpaced}>
<AppleAuthentication.AppleAuthenticationButton
buttonType={AppleAuthentication.AppleAuthenticationButtonType.SIGN_IN}
buttonStyle={
scheme === 'light'
? AppleAuthentication.AppleAuthenticationButtonStyle.BLACK
: AppleAuthentication.AppleAuthenticationButtonStyle.WHITE
}
cornerRadius={5}
style={{ width: 200, height: 64 }}
onPress={signInWithApple}
/>
</ThemedView>
);
}
else
return (
<ThemedView style={styles.verticallySpaced}>
<AppleAuthentication.AppleAuthenticationButton
buttonType={AppleAuthentication.AppleAuthenticationButtonType.SIGN_IN}
buttonStyle={
scheme === 'light'
? AppleAuthentication.AppleAuthenticationButtonStyle.BLACK
: AppleAuthentication.AppleAuthenticationButtonStyle.WHITE
}
cornerRadius={5}
style={{ width: 200, height: 64 }}
onPress={signInWithApple}
/>
</ThemedView>
);
};
export default AppleSignInButton;
const styles = StyleSheet.create({
@ -105,4 +102,4 @@ const styles = StyleSheet.create({
alignItems: 'center',
marginTop: 20,
},
});
});