rewrite finished i believe
This commit is contained in:
@ -1,14 +1,19 @@
|
||||
import React from "react";
|
||||
import * as AppleAuthentication from "expo-apple-authentication";
|
||||
import { StyleSheet, Alert } from "react-native";
|
||||
import { ThemedView } from "@/components/theme/Theme";
|
||||
import { useColorScheme } from "@/hooks/useColorScheme";
|
||||
import * as Notifications from "expo-notifications";
|
||||
import Constants from "expo-constants";
|
||||
import { saveUser, saveInitialData } from "@/components/services/SecureStore";
|
||||
import type { InitialData, User } from "@/constants/Types";
|
||||
import React from 'react';
|
||||
import * as AppleAuthentication from 'expo-apple-authentication';
|
||||
import { StyleSheet, Alert } from 'react-native';
|
||||
import { ThemedView } from '@/components/theme/Theme';
|
||||
import { useColorScheme } from '@/hooks/useColorScheme';
|
||||
import * as Notifications from 'expo-notifications';
|
||||
import Constants from 'expo-constants';
|
||||
import { saveUser, saveInitialData } from '@/components/services/SecureStore';
|
||||
import {
|
||||
getInitialDataByAppleId,
|
||||
createUser,
|
||||
updatePushToken
|
||||
} from '@/constants/APIs';
|
||||
import type { InitialData, User } from '@/constants/Types';
|
||||
|
||||
const SignInScreen({onSignIn}: {onSignIn: () => void}) => {
|
||||
const SignInScreen = ({onSignIn}: {onSignIn: () => void}) => {
|
||||
const scheme = useColorScheme() ?? 'dark';
|
||||
|
||||
const handleAppleSignIn = async () => {
|
||||
@ -30,17 +35,10 @@ const SignInScreen({onSignIn}: {onSignIn: () => void}) => {
|
||||
credential.fullName?.familyName, pushToken
|
||||
);
|
||||
|
||||
const initialData = await
|
||||
fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/users/getUserByAppleId` +
|
||||
`?appleId=${credential.user}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': process.env.NEXT_PUBLIC_API_KEY ?? '',
|
||||
},
|
||||
});
|
||||
console.log(initialData);
|
||||
if (initialData.status === 404 || !initialData.ok) {
|
||||
const initialDataResponse = await getInitialDataByAppleId(credential.user);
|
||||
|
||||
console.log(initialDataResponse);
|
||||
if (initialDataResponse.status === 404 || !initialDataResponse.ok) {
|
||||
if (!credential.user || !credential.email ||
|
||||
!credential.fullName?.givenName || !credential.fullName?.familyName ||
|
||||
!pushToken || credential.email.length === 0) {
|
||||
@ -55,62 +53,30 @@ const SignInScreen({onSignIn}: {onSignIn: () => void}) => {
|
||||
'save in the database.'
|
||||
);
|
||||
}
|
||||
|
||||
const userResponse = await
|
||||
fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/users/createUser`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': process.env.NEXT_PUBLIC_API_KEY ?? '',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
appleId: credential.user,
|
||||
email: credential.email,
|
||||
fullName:
|
||||
`${credential.fullName?.givenName} ${credential.fullName?.familyName}`,
|
||||
pushToken: pushToken,
|
||||
}),
|
||||
});
|
||||
if (!userResponse.ok) {
|
||||
const errorBody = await userResponse.text();
|
||||
console.error(
|
||||
'API error: No user returned: ',
|
||||
userResponse.status, errorBody
|
||||
);
|
||||
throw new Error(`Failed to create user: ${userResponse.status} ${errorBody}`);
|
||||
}
|
||||
const user: User = await userResponse.json() as User;
|
||||
const user: User = await createUser(
|
||||
credential.user,
|
||||
credential.email,
|
||||
credential.fullName?.givenName + credential.fullName?.familyName,
|
||||
pushToken.data
|
||||
) as User;
|
||||
await saveUser(user);
|
||||
} else if (initialData.ok) {
|
||||
const allData: InitialData = await initialData.json() as InitialData;
|
||||
} else if (initialDataResponse.ok) {
|
||||
const initialData: InitialData = await initialDataResponse.json() as InitialData;
|
||||
console.log('Existing user found! Saving data...');
|
||||
|
||||
if (allData.user.pushToken !== pushToken.data) {
|
||||
const updatePushTokenResponse =
|
||||
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/users/updatePushToken`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': process.env.NEXT_PUBLIC_API_KEY ?? '',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
userId: allData.user.id,
|
||||
pushToken: pushToken.data,
|
||||
}),
|
||||
});
|
||||
if (!updatePushTokenResponse.ok) {
|
||||
throw new Error(
|
||||
`Failed to update push token: ${updatePushTokenResponse.status}`
|
||||
);
|
||||
}
|
||||
if (initialData.user.pushToken !== pushToken.data) {
|
||||
const updatePushTokenResponse = await updatePushToken(
|
||||
initialData.user.id,
|
||||
pushToken.data
|
||||
);
|
||||
} else {
|
||||
console.log('Push token is up to date.');
|
||||
}
|
||||
allData.user.pushToken = pushToken.data;
|
||||
await saveInitialData(allData);
|
||||
initialData.user.pushToken = pushToken.data;
|
||||
await saveInitialData(initialData);
|
||||
}
|
||||
onSignIn();
|
||||
} catch (error) {
|
||||
} catch (error: unknown) {
|
||||
console.error('Error signing in:', error);
|
||||
if (error.code === 'ERR_REQUEST_CANCELLED') {
|
||||
Alert.alert('Sign in error', 'Sign in was cancelled.');
|
||||
|
Reference in New Issue
Block a user