Skip to content

Commit

Permalink
fix: fixed login bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis3797 committed Jul 4, 2024
1 parent 10d683d commit d0c6926
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions src/modules/auth/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,44 +85,38 @@ const AuthProvider: React.FC<AuthProviderProps> = ({ children, config }) => {
};

const fetchUserData = async (userData: IdTokenPayload) => {
try {
const userResponse = await fetch(
`${process.env.EXPO_PUBLIC_API_URL}/api/user/${userData.sub}`,
{
headers: {
Authorization: 'Bearer ' + (await AsyncStorage.getItem('accessToken')),
},
}
).then((res) => res.json() as unknown as GetUserResponse);
const access_token = await AsyncStorage.getItem('accessToken');
const userResponse = await fetch(
`${process.env.EXPO_PUBLIC_API_URL}/api/user/${userData.sub}`,
{
headers: {
Authorization: 'Bearer ' + access_token,
},
}
).then((res) => res.json() as unknown as GetUserResponse);

if (userResponse.success && userResponse.data) {
return userResponse.data.user;
} else {
const createdUserData = await createUserMutation.mutateAsync({
id: userData.sub,
username: userData.preferred_username,
email: userData.email,
});

console.log(userResponse);
if (createdUserData.success && createdUserData.data) {
return createdUserData.data.user;
}

if (userResponse.success && userResponse.data) {
return userResponse.data.user;
} else {
const createdUserData = await createUserMutation.mutateAsync({
id: userData.sub,
username: userData.preferred_username,
email: userData.email,
if (createdUserData.error[0]) {
Toast.show({
type: 'error',
text1: createdUserData.error[0].name,
text2: createdUserData.error[0].msg,
});

if (createdUserData.success && createdUserData.data) {
return createdUserData.data.user;
}

if (createdUserData.error[0]) {
Toast.show({
type: 'error',
text1: createdUserData.error[0].name,
text2: createdUserData.error[0].msg,
});
}
}
return null;
} catch (error) {
console.error('Error fetching user data', error);
return null;
}
return null;
};

const signIn = useCallback(async () => {
Expand Down

0 comments on commit d0c6926

Please sign in to comment.