Add Sign in with Apple sorta
This commit is contained in:
parent
c62926b8f2
commit
708c0553f5
@ -27,7 +27,7 @@ const AppleSignIn: React.FC<AppleSignInProps> = ({
|
|||||||
const handleAppleSignIn = async () => {
|
const handleAppleSignIn = async () => {
|
||||||
try {
|
try {
|
||||||
onSignInStart?.();
|
onSignInStart?.();
|
||||||
|
|
||||||
// Get credentials from Apple
|
// Get credentials from Apple
|
||||||
const credential = await AppleAuthentication.signInAsync({
|
const credential = await AppleAuthentication.signInAsync({
|
||||||
requestedScopes: [
|
requestedScopes: [
|
||||||
@ -35,36 +35,36 @@ const AppleSignIn: React.FC<AppleSignInProps> = ({
|
|||||||
AppleAuthentication.AppleAuthenticationScope.EMAIL,
|
AppleAuthentication.AppleAuthenticationScope.EMAIL,
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!credential.email) {
|
if (!credential.email) {
|
||||||
throw new Error('Email is required for Apple Sign In');
|
throw new Error('Email is required for Apple Sign In');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract user information
|
// Extract user information
|
||||||
const { email, fullName, user: appleUserId } = credential;
|
const { email, fullName, user: appleUserId } = credential;
|
||||||
|
|
||||||
// Create a name from the fullName object if available
|
// Create a name from the fullName object if available
|
||||||
let name = null;
|
let name = null;
|
||||||
if (fullName?.givenName || fullName?.familyName) {
|
if (fullName?.givenName || fullName?.familyName) {
|
||||||
name = `${fullName?.givenName || ''} ${fullName?.familyName || ''}`.trim();
|
name = `${fullName?.givenName || ''} ${fullName?.familyName || ''}`.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a deterministic password based on the Apple user ID
|
// Create a deterministic password based on the Apple user ID
|
||||||
// This way the user can sign in again with the same password
|
// This way the user can sign in again with the same password
|
||||||
const password = `Apple-${appleUserId.substring(0, 16)}`;
|
const password = `Apple-${appleUserId.substring(0, 16)}`;
|
||||||
|
|
||||||
// First try to sign in (in case the user already exists)
|
// First try to sign in (in case the user already exists)
|
||||||
const { data: signInData, error: signInError } = await supabase.auth.signInWithPassword({
|
const { data: signInData, error: signInError } = await supabase.auth.signInWithPassword({
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!signInError && signInData?.user) {
|
if (!signInError && signInData?.user) {
|
||||||
// User exists and signed in successfully
|
// User exists and signed in successfully
|
||||||
onSignInComplete?.();
|
onSignInComplete?.();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If sign-in failed, create a new user
|
// If sign-in failed, create a new user
|
||||||
const { data: signUpData, error: signUpError } = await supabase.auth.signUp({
|
const { data: signUpData, error: signUpError } = await supabase.auth.signUp({
|
||||||
email,
|
email,
|
||||||
@ -72,26 +72,25 @@ const AppleSignIn: React.FC<AppleSignInProps> = ({
|
|||||||
options: {
|
options: {
|
||||||
data: {
|
data: {
|
||||||
full_name: name,
|
full_name: name,
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (signUpError) {
|
if (signUpError) {
|
||||||
throw signUpError;
|
throw signUpError;
|
||||||
}
|
}
|
||||||
|
|
||||||
// User created successfully
|
// User created successfully
|
||||||
onSignInComplete?.();
|
onSignInComplete?.();
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Apple sign in error:', error);
|
console.error('Apple sign in error:', error);
|
||||||
|
|
||||||
if (error.code === 'ERR_REQUEST_CANCELED') {
|
if (error.code === 'ERR_REQUEST_CANCELED') {
|
||||||
console.log('Sign in was canceled');
|
console.log('Sign in was canceled');
|
||||||
} else {
|
} else {
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
'Sign in error',
|
'Sign in error',
|
||||||
'An error occurred while signing in with Apple. Please try again.'
|
'An error occurred while signing in with Apple. Please try again.',
|
||||||
);
|
);
|
||||||
onSignInError?.(error);
|
onSignInError?.(error);
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ const LoginPage = () => {
|
|||||||
const [email, setEmail] = useState('');
|
const [email, setEmail] = useState('');
|
||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
// Set up auto-refreshing for web
|
// Set up auto-refreshing for web
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (Platform.OS === 'web') {
|
if (Platform.OS === 'web') {
|
||||||
@ -32,7 +32,7 @@ const LoginPage = () => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const signInWithEmail = async () => {
|
const signInWithEmail = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const { error } = await supabase.auth.signInWithPassword({
|
const { error } = await supabase.auth.signInWithPassword({
|
||||||
@ -42,7 +42,7 @@ const LoginPage = () => {
|
|||||||
if (error) Alert.alert(error.message);
|
if (error) Alert.alert(error.message);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const signUpWithEmail = async () => {
|
const signUpWithEmail = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const {
|
const {
|
||||||
@ -56,7 +56,7 @@ const LoginPage = () => {
|
|||||||
else if (!session) Alert.alert('Please check your inbox for email verification!');
|
else if (!session) Alert.alert('Please check your inbox for email verification!');
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemedView style={styles.container}>
|
<ThemedView style={styles.container}>
|
||||||
<ThemedView style={styles.titleContainer}>
|
<ThemedView style={styles.titleContainer}>
|
||||||
@ -65,7 +65,7 @@ const LoginPage = () => {
|
|||||||
Tech Tracker
|
Tech Tracker
|
||||||
</ThemedText>
|
</ThemedText>
|
||||||
</ThemedView>
|
</ThemedView>
|
||||||
|
|
||||||
<ThemedView style={[styles.verticallySpaced]}>
|
<ThemedView style={[styles.verticallySpaced]}>
|
||||||
<ThemedTextInput
|
<ThemedTextInput
|
||||||
fontSize={24}
|
fontSize={24}
|
||||||
@ -74,7 +74,7 @@ const LoginPage = () => {
|
|||||||
placeholder='email@address.com'
|
placeholder='email@address.com'
|
||||||
/>
|
/>
|
||||||
</ThemedView>
|
</ThemedView>
|
||||||
|
|
||||||
<ThemedView style={styles.verticallySpaced}>
|
<ThemedView style={styles.verticallySpaced}>
|
||||||
<ThemedTextInput
|
<ThemedTextInput
|
||||||
fontSize={24}
|
fontSize={24}
|
||||||
@ -84,7 +84,7 @@ const LoginPage = () => {
|
|||||||
placeholder='Password'
|
placeholder='Password'
|
||||||
/>
|
/>
|
||||||
</ThemedView>
|
</ThemedView>
|
||||||
|
|
||||||
<ThemedView style={[styles.verticallySpaced, styles.mt20]}>
|
<ThemedView style={[styles.verticallySpaced, styles.mt20]}>
|
||||||
<ThemedTextButton
|
<ThemedTextButton
|
||||||
text='Sign in'
|
text='Sign in'
|
||||||
@ -93,7 +93,7 @@ const LoginPage = () => {
|
|||||||
fontSize={24}
|
fontSize={24}
|
||||||
/>
|
/>
|
||||||
</ThemedView>
|
</ThemedView>
|
||||||
|
|
||||||
<ThemedView style={styles.verticallySpaced}>
|
<ThemedView style={styles.verticallySpaced}>
|
||||||
<ThemedTextButton
|
<ThemedTextButton
|
||||||
text='Sign up'
|
text='Sign up'
|
||||||
@ -102,10 +102,10 @@ const LoginPage = () => {
|
|||||||
fontSize={24}
|
fontSize={24}
|
||||||
/>
|
/>
|
||||||
</ThemedView>
|
</ThemedView>
|
||||||
|
|
||||||
{/* Apple Sign In - Only shows on iOS */}
|
{/* Apple Sign In - Only shows on iOS */}
|
||||||
<ThemedView style={[styles.verticallySpaced, styles.mt20]}>
|
<ThemedView style={[styles.verticallySpaced, styles.mt20]}>
|
||||||
<AppleSignIn
|
<AppleSignIn
|
||||||
onSignInStart={() => setLoading(true)}
|
onSignInStart={() => setLoading(true)}
|
||||||
onSignInComplete={() => setLoading(false)}
|
onSignInComplete={() => setLoading(false)}
|
||||||
onSignInError={() => setLoading(false)}
|
onSignInError={() => setLoading(false)}
|
||||||
|
@ -31,8 +31,9 @@ const ThemedTextInput: React.FC<ThemedTextInputProps> = ({
|
|||||||
{
|
{
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
borderColor: Colors[scheme].accent },
|
borderColor: Colors[scheme].accent,
|
||||||
containerStyle,
|
},
|
||||||
|
containerStyle,
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
<TextInput
|
<TextInput
|
||||||
|
@ -28,18 +28,10 @@ const config = {
|
|||||||
teamId: process.env.APPLE_TEAM_ID || '',
|
teamId: process.env.APPLE_TEAM_ID || '',
|
||||||
clientId: process.env.AUTH_APPLE_ID || '',
|
clientId: process.env.AUTH_APPLE_ID || '',
|
||||||
keyId: process.env.APPLE_KEY_ID || '',
|
keyId: process.env.APPLE_KEY_ID || '',
|
||||||
privateKeyPath: path.resolve(
|
privateKeyPath: path.resolve(__dirname, process.env.APPLE_PRIVATE_KEY_PATH || ''),
|
||||||
__dirname,
|
|
||||||
process.env.APPLE_PRIVATE_KEY_PATH || '',
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (
|
if (!config.teamId || !config.clientId || !config.keyId || !config.privateKeyPath) {
|
||||||
!config.teamId ||
|
|
||||||
!config.clientId ||
|
|
||||||
!config.keyId ||
|
|
||||||
!config.privateKeyPath
|
|
||||||
) {
|
|
||||||
console.error('Missing necessary Apple configuration');
|
console.error('Missing necessary Apple configuration');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user