From 00e0bcd929f93df6e8ce45e9d2c416b5fe669397 Mon Sep 17 00:00:00 2001 From: gibbyb Date: Thu, 10 Oct 2024 03:50:09 -0500 Subject: [PATCH] Login now works. User is created in database as well --- .gitignore | 3 +- assets/fonts/SpaceMono-Regular.ttf | Bin components/auth/SignInScreen.tsx | 80 ++++++++++++++------ example/assets/fonts/SpaceMono-Regular.ttf | Bin example/scripts/reset-project.js | 0 package-lock.json | 82 ++++++++++----------- 6 files changed, 102 insertions(+), 63 deletions(-) mode change 100755 => 100644 assets/fonts/SpaceMono-Regular.ttf mode change 100755 => 100644 example/assets/fonts/SpaceMono-Regular.ttf mode change 100755 => 100644 example/scripts/reset-project.js diff --git a/.gitignore b/.gitignore index 6623142..2066475 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ npm-debug.* *.mobileprovision *.orig.* web-build/ +.env # macOS .DS_Store @@ -17,4 +18,4 @@ web-build/ # The following patterns were generated by expo-cli expo-env.d.ts -# @end expo-cli \ No newline at end of file +# @end expo-cli diff --git a/assets/fonts/SpaceMono-Regular.ttf b/assets/fonts/SpaceMono-Regular.ttf old mode 100755 new mode 100644 diff --git a/components/auth/SignInScreen.tsx b/components/auth/SignInScreen.tsx index e6fe116..645a2f9 100644 --- a/components/auth/SignInScreen.tsx +++ b/components/auth/SignInScreen.tsx @@ -35,44 +35,82 @@ export default function SignInScreen({ onSignIn }: { onSignIn: () => void }) { projectId: projectId }); - console.log(credential.user, credential.email, credential.fullName, credential.fullName?.givenName, credential.fullName?.familyName, pushToken); + console.log(credential.user, credential.email, credential.fullName?.givenName, credential.fullName?.familyName, pushToken); - - const response = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/users/createUser`, { - method: 'POST', + const checkUserResponse = await + fetch(`${process.env.EXPO_PUBLIC_API_URL}/users/getUserByAppleId?appleId=${credential.user}`, { + method: 'GET', headers: { 'Content-Type': 'application/json', 'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '', }, - body: JSON.stringify({ - appleId: credential.user, - appleEmail: credential.email, - fullName: `${credential.fullName?.givenName} ${credential.fullName?.familyName}`, - pushToken: pushToken.data, - }), }); - if (!response.ok) { - throw new Error('Failed to create user'); + if (checkUserResponse.status === 404) { + if (!credential.user || !credential.email || !credential.fullName?.givenName + || !credential.fullName?.familyName) { + Alert.alert('Sign In Error', 'Unable to create account. Please try again or contact support.'); + throw new Error('Incomplete user data for new account creation'); + } + + const response = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/users/createUser`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '', + }, + body: JSON.stringify({ + appleId: credential.user, + appleEmail: credential.email, + fullName: `${credential.fullName?.givenName} ${credential.fullName?.familyName}`, + pushToken: pushToken.data, + }), + }); + if (!response.ok) { + const errorBody = await response.text(); + console.error('API Error:', response.status, errorBody); + throw new Error(`Failed to create user: ${response.status} ${errorBody}`); + } + const userData = await response.json(); + await saveUserData(userData); + } else if (checkUserResponse.ok) { + const userData = await checkUserResponse.json(); + console.log('Existing user found:', JSON.stringify(userData)); + // check if push token should be updated + if (userData.pushToken !== pushToken.data) { + const updatePushTokenResponse = + await fetch(`${process.env.EXPO_PUBLIC_API_URL}/users/updatePushTokenByAppleId`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '', + }, + body: JSON.stringify({ + appleId: credential.user, + pushToken: pushToken.data, + }), + }); + if (!updatePushTokenResponse.ok) { + throw new Error('Failed to update push token'); + } + } else { + console.log('Push token is up to date'); + } + userData.pushToken = pushToken.data; + await saveUserData(userData); } - const userData = await response.json(); - await saveUserData(userData); onSignIn(); - } catch (error) { + console.error('Error signing in:', error); if (error.code === 'ERR_REQUEST_CANCELLED') { - // Handle when user cancels sign in - console.error('User canceled sign in', error); - Alert.alert('An error occurred', 'User canceled sign in'); + Alert.alert('Sign In Cancelled', 'It seems like you canceled the sign in process.'); } else { - console.error('An unknown error occurred', error); - Alert.alert('Unknown error', 'An unknown error occurred'); + Alert.alert('Sign In Error', 'An error occurred while signing in. Please try again or contact support.'); } } }; - return (