Skip to content

Commit 1408969

Browse files
yyyortclaude
andcommitted
Fix ERR_TOO_MANY_REDIRECTS for rejected users accessing declined page
Fixed infinite redirect loop that occurred when users with rejected/failed application status tried to sign in. The middleware was performing unnecessary application status checks on the /auth/declined page itself, causing the page to redirect to itself infinitely. Changes: - Add AUTH_STATUS_ROUTES constant for routes that bypass application status checks - Allow authenticated users to access /auth/declined and /auth/verify without status validation - Remove unused error variables to fix TypeScript warnings This prevents the redirect loop by letting users access status-specific pages (/auth/declined, /auth/verify) immediately after authentication, without triggering additional status-based redirects. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 52e0fea commit 1408969

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎apps/codebility/middleware.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ const PUBLIC_ROUTE_PREFIXES = ["/profiles/", "/nda-signing/"] as const;
2929

3030
const AUTH_ROUTES = ["/auth/sign-in", "/auth/sign-up", "/auth/onboarding"] as const;
3131

32-
// Authentication status routes
32+
// Authentication status routes - these require auth but have special handling
3333
const EMAIL_VERIFICATION_ROUTE = "/auth/verify";
3434
const WAITING_APPROVAL_ROUTE = "/auth/waiting";
3535
const APPLICATION_DECLINED_ROUTE = "/auth/declined";
36-
const APPLICANT_ROUTE = "/applicant"
36+
const APPLICANT_ROUTE = "/applicant";
37+
38+
// Routes that authenticated users can always access regardless of application status
39+
const AUTH_STATUS_ROUTES = [APPLICATION_DECLINED_ROUTE, EMAIL_VERIFICATION_ROUTE] as const;
3740

3841
const routePermissionMap: Record<string, keyof RolePermissions> = {
3942
"/home/interns": "interns",
@@ -89,16 +92,14 @@ export async function middleware(req: NextRequest) {
8992
const supabase = await createClientServerComponent();
9093
const {
9194
data: { user },
92-
error,
9395
} = await supabase.auth.getUser();
9496

95-
// If error is related to missing refresh token, ignore it for auth routes
97+
// If user is already logged in, redirect to home
9698
if (user) {
97-
// Redirect to home if user is already logged in
9899
return redirectTo(req, "/home");
99100
}
100101

101-
// Allow access to auth pages if not logged in, regardless of error
102+
// Allow access to auth pages if not logged in
102103
return NextResponse.next();
103104
}
104105

@@ -120,10 +121,15 @@ export async function middleware(req: NextRequest) {
120121
return redirectToLogin(req);
121122
}
122123

124+
// Allow authenticated users to access auth status routes without further checks
125+
// This prevents redirect loops when users are being directed to these pages
126+
if (AUTH_STATUS_ROUTES.includes(pathname as any)) {
127+
return NextResponse.next();
128+
}
129+
123130
// Check if email is verified
124131
const {
125132
data: { user: authUser },
126-
error: verificationError,
127133
} = await supabase.auth.getUser();
128134

129135
if (

0 commit comments

Comments
 (0)