Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(auth): only redirect to verify-email when the email is not verified (#22693)

Mocha336a71ad7011
diff --git a/src/features/Auth/SignIn/useSignIn.test.ts b/src/features/Auth/SignIn/useSignIn.test.ts
index d893e74e26a..534745677c1 100644
--- a/src/features/Auth/SignIn/useSignIn.test.ts
+++ b/src/features/Auth/SignIn/useSignIn.test.ts
@@ -312,10 +312,12 @@ describe('useSignIn', () => {
expect(mockMessageError).not.toHaveBeenCalled();
});
- it('should redirect to verify-email on 403', async () => {
+ it('should redirect to verify-email when the email is not verified', async () => {
mockSignInEmail.mockImplementation(async (_data: any, opts: any) => {
- opts.onError({ error: { status: 403 } });
- return { error: { message: 'Email not verified', status: 403 } };
+ opts.onError({ error: { code: 'EMAIL_NOT_VERIFIED', status: 403 } });
+ return {
+ error: { code: 'EMAIL_NOT_VERIFIED', message: 'Email not verified', status: 403 },
+ };
});
mockFetch.mockResolvedValueOnce({
@@ -336,6 +338,32 @@ describe('useSignIn', () => {
expect(mockNavigate).toHaveBeenCalledWith(
expect.stringContaining('/verify-email?email=user%40example.com'),
);
+ expect(mockForm.setErrors).not.toHaveBeenCalled();
+ });
+
+ it('should surface a non-verification 403 inline instead of redirecting', async () => {
+ mockSignInEmail.mockImplementation(async (_data: any, opts: any) => {
+ opts.onError({ error: { code: 'BANNED_USER', status: 403 } });
+ return { error: { code: 'BANNED_USER', message: 'You have been banned', status: 403 } };
+ });
+
+ mockFetch.mockResolvedValueOnce({
+ json: async () => ({ exists: true, hasPassword: true }),
+ ok: true,
+ });
+
+ const { result } = renderHook(() => useSignIn());
+
+ await act(async () => {
+ await result.current.handleCheckUser({ email: '[email protected]' });
+ });
+
+ await act(async () => {
+ await result.current.handleSignIn({ password: 'password' });
+ });
+
+ expect(mockNavigate).not.toHaveBeenCalled();
+ expect(mockForm.setErrors).toHaveBeenCalledWith({ password: 'You have been banned' });
});
});
diff --git a/src/features/Auth/SignIn/useSignIn.ts b/src/features/Auth/SignIn/useSignIn.ts
index e1caaa0a217..1524903135f 100644
--- a/src/features/Auth/SignIn/useSignIn.ts
+++ b/src/features/Auth/SignIn/useSignIn.ts
@@ -22,6 +22,13 @@ import { EMAIL_REGEX, USERNAME_REGEX } from './SignInEmailStep';
const LAST_AUTH_PROVIDER_KEY = 'lobehub:auth:last-provider:v1';
+// Better Auth returns FORBIDDEN for several sign-in rejections (banned user,
+// access denied). Only the dedicated EMAIL_NOT_VERIFIED code means the account
+// is valid but needs to verify its address, so only that one goes to
+// /verify-email; every other 403 stays on the password field as an error.
+const isEmailNotVerifiedError = (error?: unknown) =>
+ (error as { code?: string } | null | undefined)?.code === 'EMAIL_NOT_VERIFIED';
+
type Step = 'email' | 'password' | 'emailSent';
type SentEmailType = 'magicLink' | 'resetPassword';
@@ -238,7 +245,7 @@ export const useSignIn = () => {
{
onError: (ctx) => {
console.error('Sign in error:', ctx.error);
- if (ctx.error.status === 403) {
+ if (isEmailNotVerifiedError(ctx.error)) {
navigate(
`/verify-email?email=${encodeURIComponent(email)}&callbackUrl=${encodeURIComponent(callbackUrl)}`,
);
@@ -251,7 +258,7 @@ export const useSignIn = () => {
},
);
- if (result.error && result.error.status !== 403) {
+ if (result.error && !isEmailNotVerifiedError(result.error)) {
// Wrong password is the most common sign-in failure. Keep the error
// pinned inline on the field (persistent, with retry context) rather
// than a toast that vanishes in 3s (ux Read §1.1 / Same-Page Error).