Skip to content

Commit 5a4b90b

Browse files
authored
fix: add toast error for unknown errors on login (#630)
1 parent 4714e1c commit 5a4b90b

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

src/features/auth/page-login.tsx

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,19 @@ export default function PageLogin({
3535
mutationFn: async (
3636
provider: Parameters<typeof authClient.signIn.social>[0]['provider']
3737
) => {
38-
const response = await authClient.signIn.social({
39-
provider,
40-
callbackURL: search.redirect ?? '/',
41-
errorCallbackURL: '/login/error',
42-
});
43-
if (response.error) {
44-
throw new Error(response.error.message);
38+
try {
39+
const response = await authClient.signIn.social({
40+
provider,
41+
callbackURL: search.redirect ?? '/',
42+
errorCallbackURL: '/login/error',
43+
});
44+
if (response.error) {
45+
throw new Error(response.error.message);
46+
}
47+
return response.data;
48+
} catch {
49+
toast.error(t('auth:errorCode.UNKNOWN_ERROR'));
4550
}
46-
return response.data;
4751
},
4852
onError: (error) => {
4953
form.setError('email', { message: error.message });
@@ -63,30 +67,34 @@ export default function PageLogin({
6367
useMascot({ isError: !isValid && isSubmitted });
6468

6569
const submitHandler: SubmitHandler<FormFieldsLogin> = async ({ email }) => {
66-
const { error } = await authClient.emailOtp.sendVerificationOtp({
67-
email,
68-
type: 'sign-in',
69-
});
70+
try {
71+
const { error } = await authClient.emailOtp.sendVerificationOtp({
72+
email,
73+
type: 'sign-in',
74+
});
7075

71-
if (error) {
72-
toast.error(
73-
error.code
74-
? t(
75-
`auth:errorCode.${error.code as unknown as keyof typeof authClient.$ERROR_CODES}`
76-
)
77-
: error.message || t('auth:errorCode.UNKNOWN_ERROR')
78-
);
79-
return;
80-
}
76+
if (error) {
77+
toast.error(
78+
error.code
79+
? t(
80+
`auth:errorCode.${error.code as unknown as keyof typeof authClient.$ERROR_CODES}`
81+
)
82+
: error.message || t('auth:errorCode.UNKNOWN_ERROR')
83+
);
84+
return;
85+
}
8186

82-
router.navigate({
83-
replace: true,
84-
to: '/login/verify',
85-
search: {
86-
redirect: search.redirect,
87-
email,
88-
},
89-
});
87+
router.navigate({
88+
replace: true,
89+
to: '/login/verify',
90+
search: {
91+
redirect: search.redirect,
92+
email,
93+
},
94+
});
95+
} catch {
96+
toast.error(t('auth:errorCode.UNKNOWN_ERROR'));
97+
}
9098
};
9199

92100
return (

0 commit comments

Comments
 (0)