Login now works. User is created in database as well

This commit is contained in:
Gabriel Brown 2024-10-10 03:50:09 -05:00
parent 6e01a31e76
commit 00e0bcd929
6 changed files with 102 additions and 63 deletions

3
.gitignore vendored
View File

@ -9,6 +9,7 @@ npm-debug.*
*.mobileprovision *.mobileprovision
*.orig.* *.orig.*
web-build/ web-build/
.env
# macOS # macOS
.DS_Store .DS_Store
@ -17,4 +18,4 @@ web-build/
# The following patterns were generated by expo-cli # The following patterns were generated by expo-cli
expo-env.d.ts expo-env.d.ts
# @end expo-cli # @end expo-cli

0
assets/fonts/SpaceMono-Regular.ttf Executable file → Normal file
View File

View File

@ -35,44 +35,82 @@ export default function SignInScreen({ onSignIn }: { onSignIn: () => void }) {
projectId: projectId 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 checkUserResponse = await
const response = await fetch(`${process.env.EXPO_PUBLIC_API_URL}/users/createUser`, { fetch(`${process.env.EXPO_PUBLIC_API_URL}/users/getUserByAppleId?appleId=${credential.user}`, {
method: 'POST', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'x-api-key': process.env.EXPO_PUBLIC_API_KEY ?? '', '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) { if (checkUserResponse.status === 404) {
throw new Error('Failed to create user'); 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(); onSignIn();
} catch (error) { } catch (error) {
console.error('Error signing in:', error);
if (error.code === 'ERR_REQUEST_CANCELLED') { if (error.code === 'ERR_REQUEST_CANCELLED') {
// Handle when user cancels sign in Alert.alert('Sign In Cancelled', 'It seems like you canceled the sign in process.');
console.error('User canceled sign in', error);
Alert.alert('An error occurred', 'User canceled sign in');
} else { } else {
console.error('An unknown error occurred', error); Alert.alert('Sign In Error', 'An error occurred while signing in. Please try again or contact support.');
Alert.alert('Unknown error', 'An unknown error occurred');
} }
} }
}; };
return ( return (
<ThemedView style={styles.container}> <ThemedView style={styles.container}>
<AppleAuthentication.AppleAuthenticationButton <AppleAuthentication.AppleAuthenticationButton

0
example/assets/fonts/SpaceMono-Regular.ttf Executable file → Normal file
View File

0
example/scripts/reset-project.js Executable file → Normal file
View File

82
package-lock.json generated
View File

@ -9595,9 +9595,9 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.5.34", "version": "1.5.35",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.34.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.35.tgz",
"integrity": "sha512-/TZAiChbAflBNjCg+VvstbcwAtIL/VdMFO3NgRFIzBjpvPzWOTIbbO8kNb6RwU4bt9TP7K+3KqBKw/lOU+Y+GA==", "integrity": "sha512-hOSRInrIDm0Brzp4IHW2F/VM+638qOL2CzE0DgpnGzKW27C95IqqeqgKz/hxHGnvPxvQGpHUGD5qRVC9EZY2+A==",
"license": "ISC" "license": "ISC"
}, },
"node_modules/emittery": { "node_modules/emittery": {
@ -10142,32 +10142,6 @@
"expo": "*" "expo": "*"
} }
}, },
"node_modules/expo-device/node_modules/ua-parser-js": {
"version": "0.7.39",
"resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.39.tgz",
"integrity": "sha512-IZ6acm6RhQHNibSt7+c09hhvsKy9WUr4DVbeq9U8o71qxyYtJpQeDxQnMrVqnIFMLcQjHO0I9wgfO2vIahht4w==",
"funding": [
{
"type": "opencollective",
"url": "https://opencollective.com/ua-parser-js"
},
{
"type": "paypal",
"url": "https://paypal.me/faisalman"
},
{
"type": "github",
"url": "https://github.com/sponsors/faisalman"
}
],
"license": "MIT",
"bin": {
"ua-parser-js": "script/cli.js"
},
"engines": {
"node": "*"
}
},
"node_modules/expo-file-system": { "node_modules/expo-file-system": {
"version": "17.0.1", "version": "17.0.1",
"resolved": "https://registry.npmjs.org/expo-file-system/-/expo-file-system-17.0.1.tgz", "resolved": "https://registry.npmjs.org/expo-file-system/-/expo-file-system-17.0.1.tgz",
@ -10788,6 +10762,32 @@
"integrity": "sha512-b2XGFAFdWZWg0phtAWLHCk836A1Xann+I+Dgd3Gk64MHKZO44FfoD1KxyvbSh0qZsIoXQGGlVztIY+oitJPpRQ==", "integrity": "sha512-b2XGFAFdWZWg0phtAWLHCk836A1Xann+I+Dgd3Gk64MHKZO44FfoD1KxyvbSh0qZsIoXQGGlVztIY+oitJPpRQ==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/fbjs/node_modules/ua-parser-js": {
"version": "1.0.39",
"resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-1.0.39.tgz",
"integrity": "sha512-k24RCVWlEcjkdOxYmVJgeD/0a1TiSpqLg+ZalVGV9lsnr4yqu0w7tX/x2xX6G4zpkgQnRf89lxuZ1wsbjXM8lw==",
"funding": [
{
"type": "opencollective",
"url": "https://opencollective.com/ua-parser-js"
},
{
"type": "paypal",
"url": "https://paypal.me/faisalman"
},
{
"type": "github",
"url": "https://github.com/sponsors/faisalman"
}
],
"license": "MIT",
"bin": {
"ua-parser-js": "script/cli.js"
},
"engines": {
"node": "*"
}
},
"node_modules/fetch-retry": { "node_modules/fetch-retry": {
"version": "4.1.1", "version": "4.1.1",
"resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-4.1.1.tgz", "resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-4.1.1.tgz",
@ -11041,9 +11041,9 @@
} }
}, },
"node_modules/form-data": { "node_modules/form-data": {
"version": "3.0.1", "version": "3.0.2",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.2.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", "integrity": "sha512-sJe+TQb2vIaIyO783qN6BlMYWMw3WBOHA1Ay2qxsnjuafEOQFJ2JakedOQirT6D5XPRxDvS7AHYyem9fTpb4LQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"asynckit": "^0.4.0", "asynckit": "^0.4.0",
@ -14719,9 +14719,9 @@
} }
}, },
"node_modules/jsdom/node_modules/form-data": { "node_modules/jsdom/node_modules/form-data": {
"version": "4.0.0", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.1.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", "integrity": "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
@ -17149,9 +17149,9 @@
} }
}, },
"node_modules/react-devtools-core": { "node_modules/react-devtools-core": {
"version": "5.3.1", "version": "5.3.2",
"resolved": "https://registry.npmjs.org/react-devtools-core/-/react-devtools-core-5.3.1.tgz", "resolved": "https://registry.npmjs.org/react-devtools-core/-/react-devtools-core-5.3.2.tgz",
"integrity": "sha512-7FSb9meX0btdBQLwdFOwt6bGqvRPabmVMMslv8fgoSPqXyuGpgQe36kx8gR86XPw7aV1yVouTp6fyZ0EH+NfUw==", "integrity": "sha512-crr9HkVrDiJ0A4zot89oS0Cgv0Oa4OG1Em4jit3P3ZxZSKPMYyMjfwMqgcJna9o625g8oN87rBm8SWWrSTBZxg==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"shell-quote": "^1.6.1", "shell-quote": "^1.6.1",
@ -19435,9 +19435,9 @@
} }
}, },
"node_modules/ua-parser-js": { "node_modules/ua-parser-js": {
"version": "1.0.39", "version": "0.7.39",
"resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-1.0.39.tgz", "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.39.tgz",
"integrity": "sha512-k24RCVWlEcjkdOxYmVJgeD/0a1TiSpqLg+ZalVGV9lsnr4yqu0w7tX/x2xX6G4zpkgQnRf89lxuZ1wsbjXM8lw==", "integrity": "sha512-IZ6acm6RhQHNibSt7+c09hhvsKy9WUr4DVbeq9U8o71qxyYtJpQeDxQnMrVqnIFMLcQjHO0I9wgfO2vIahht4w==",
"funding": [ "funding": [
{ {
"type": "opencollective", "type": "opencollective",