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

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ npm-debug.*
*.mobileprovision
*.orig.*
web-build/
.env
# macOS
.DS_Store

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

View File

@ -35,8 +35,23 @@ 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 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 ?? '',
},
});
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',
@ -51,28 +66,51 @@ export default function SignInScreen({ onSignIn }: { onSignIn: () => void }) {
pushToken: pushToken.data,
}),
});
if (!response.ok) {
throw new Error('Failed to create user');
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);
onSignIn();
} catch (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');
} 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.error('An unknown error occurred', error);
Alert.alert('Unknown error', 'An unknown error occurred');
console.log('Push token is up to date');
}
userData.pushToken = pushToken.data;
await saveUserData(userData);
}
onSignIn();
} catch (error) {
console.error('Error signing in:', error);
if (error.code === 'ERR_REQUEST_CANCELLED') {
Alert.alert('Sign In Cancelled', 'It seems like you canceled the sign in process.');
} else {
Alert.alert('Sign In Error', 'An error occurred while signing in. Please try again or contact support.');
}
}
};
return (
<ThemedView style={styles.container}>
<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"
},
"node_modules/electron-to-chromium": {
"version": "1.5.34",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.34.tgz",
"integrity": "sha512-/TZAiChbAflBNjCg+VvstbcwAtIL/VdMFO3NgRFIzBjpvPzWOTIbbO8kNb6RwU4bt9TP7K+3KqBKw/lOU+Y+GA==",
"version": "1.5.35",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.35.tgz",
"integrity": "sha512-hOSRInrIDm0Brzp4IHW2F/VM+638qOL2CzE0DgpnGzKW27C95IqqeqgKz/hxHGnvPxvQGpHUGD5qRVC9EZY2+A==",
"license": "ISC"
},
"node_modules/emittery": {
@ -10142,32 +10142,6 @@
"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": {
"version": "17.0.1",
"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==",
"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": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-4.1.1.tgz",
@ -11041,9 +11041,9 @@
}
},
"node_modules/form-data": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.2.tgz",
"integrity": "sha512-sJe+TQb2vIaIyO783qN6BlMYWMw3WBOHA1Ay2qxsnjuafEOQFJ2JakedOQirT6D5XPRxDvS7AHYyem9fTpb4LQ==",
"license": "MIT",
"dependencies": {
"asynckit": "^0.4.0",
@ -14719,9 +14719,9 @@
}
},
"node_modules/jsdom/node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.1.tgz",
"integrity": "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -17149,9 +17149,9 @@
}
},
"node_modules/react-devtools-core": {
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/react-devtools-core/-/react-devtools-core-5.3.1.tgz",
"integrity": "sha512-7FSb9meX0btdBQLwdFOwt6bGqvRPabmVMMslv8fgoSPqXyuGpgQe36kx8gR86XPw7aV1yVouTp6fyZ0EH+NfUw==",
"version": "5.3.2",
"resolved": "https://registry.npmjs.org/react-devtools-core/-/react-devtools-core-5.3.2.tgz",
"integrity": "sha512-crr9HkVrDiJ0A4zot89oS0Cgv0Oa4OG1Em4jit3P3ZxZSKPMYyMjfwMqgcJna9o625g8oN87rBm8SWWrSTBZxg==",
"license": "MIT",
"dependencies": {
"shell-quote": "^1.6.1",
@ -19435,9 +19435,9 @@
}
},
"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==",
"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",