2024-10-09 16:55:16 -05:00
|
|
|
import * as SecureStore from 'expo-secure-store';
|
|
|
|
|
2024-10-12 21:55:48 -05:00
|
|
|
type UserData = {
|
|
|
|
id: number;
|
|
|
|
appleId: string | null;
|
|
|
|
appleEmail: string | null;
|
|
|
|
fullName: string;
|
|
|
|
pfpURL: string | null;
|
|
|
|
pushToken: string;
|
|
|
|
createdAt: Date;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const saveUserData = async (userData: UserData) => {
|
2024-10-09 16:55:16 -05:00
|
|
|
try {
|
|
|
|
await SecureStore.setItemAsync('userData', JSON.stringify(userData));
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error saving user data:', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getUserData = async () => {
|
|
|
|
try {
|
|
|
|
const userData = await SecureStore.getItemAsync('userData');
|
|
|
|
return userData ? JSON.parse(userData) : null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error getting user data:', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2024-10-11 13:46:46 -05:00
|
|
|
|
|
|
|
export const updateUserData = async (updatedFields: Partial<any>) => {
|
|
|
|
try {
|
2024-10-12 21:55:48 -05:00
|
|
|
const currentUserData: UserData = await getUserData() as UserData;
|
2024-10-11 13:46:46 -05:00
|
|
|
if (currentUserData) {
|
2024-10-12 21:55:48 -05:00
|
|
|
const updatedUserData: UserData = { ...currentUserData, ...updatedFields };
|
2024-10-11 13:46:46 -05:00
|
|
|
await saveUserData(updatedUserData);
|
2024-10-12 21:55:48 -05:00
|
|
|
return updatedUserData as UserData;
|
2024-10-11 13:46:46 -05:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error updating user data:', error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|