Skip to content

Commit 1ccf1f1

Browse files
committed
fix: fallback when server is down
Fix DBP-26
1 parent 245e128 commit 1ccf1f1

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

app/api/auth/login/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import { NextRequest, NextResponse } from "next/server";
1515
* 4. Redirect user to authorization server
1616
*/
1717
export async function GET(request: NextRequest) {
18-
try {
19-
// Check if user is already authenticated
20-
await redirectIfAuthenticated();
18+
// Check if user is already authenticated
19+
await redirectIfAuthenticated();
2120

21+
try {
2222
// Generate PKCE parameters
2323
const codeVerifier = generateCodeVerifier();
2424
const state = generateState();

lib/auth.rsc.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ import { redirect } from "next/navigation";
22
import { getAuthStatus } from "./auth";
33

44
export async function redirectIfAuthenticated(): Promise<void> {
5-
const isAuthenticated = await getAuthStatus();
6-
if (isAuthenticated.role === "admin") {
5+
let role: string | undefined;
6+
7+
try {
8+
const isAuthenticated = await getAuthStatus();
9+
role = isAuthenticated.role;
10+
} catch (error) {
11+
console.error("Error validating auth:", error);
12+
}
13+
14+
if (role === "admin") {
715
redirect("/");
816
}
917
}

middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ export async function middleware(request: NextRequest) {
7878
return NextResponse.json(
7979
{
8080
error: "server_error",
81-
error_description: "Authentication validation failed",
81+
error_description: "Could not validate authentication",
8282
},
8383
{ status: 500 },
8484
);
8585
} else {
8686
const loginUrl = new URL("/login", request.url);
87-
loginUrl.searchParams.set("error", "auth_error");
87+
loginUrl.searchParams.set("error", "server_error");
8888
return NextResponse.redirect(loginUrl);
8989
}
9090
}

0 commit comments

Comments
 (0)