Skip to content

Commit f94894e

Browse files
VIA-172 AJ/AS Changed the redirected page in middleware when session is not found
1 parent 083893d commit f94894e

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/middleware.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import { auth } from "@project/auth";
6+
import { SESSION_LOGOUT_ROUTE } from "@src/app/session-logout/constants";
67
import { middleware } from "@src/middleware";
78
import { NextRequest } from "next/server";
89

@@ -26,7 +27,7 @@ describe("middleware", () => {
2627
jest.clearAllMocks();
2728
});
2829

29-
it("redirects users without active session to sso-failure page", async () => {
30+
it("redirects users without active session to session-logout page", async () => {
3031
const testUrl = "https://testurl/abc";
3132
const mockRequest = getMockRequest(testUrl);
3233

@@ -36,7 +37,7 @@ describe("middleware", () => {
3637

3738
expect(result.status).toBe(307);
3839
expect(result.headers.get("Location")).toEqual(
39-
`${mockRequest.nextUrl.origin}/sso-failure?error=No%20active%20session%20found`,
40+
`${mockRequest.nextUrl.origin}${SESSION_LOGOUT_ROUTE}`,
4041
);
4142
});
4243

src/middleware.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { auth } from "@project/auth";
2+
import { SESSION_LOGOUT_ROUTE } from "@src/app/session-logout/constants";
23
import { Session } from "next-auth";
34
import { NextRequest, NextResponse } from "next/server";
45
import { logger } from "@src/utils/logger";
@@ -9,12 +10,10 @@ const log: Logger = logger.child({ name: "middleware" });
910
export async function middleware(request: NextRequest) {
1011
log.info(`Inspecting ${request.nextUrl}`);
1112
const session: Session | null = await auth();
12-
if (!session?.user?.birthdate) {
13-
return NextResponse.redirect(
14-
new URL(`/sso-failure?error=No active session found`, request.url),
15-
);
16-
}
1713
log.info(session, "Session Object");
14+
if (!session?.user) {
15+
return NextResponse.redirect(new URL(SESSION_LOGOUT_ROUTE, request.url));
16+
}
1817
return NextResponse.next();
1918
}
2019

0 commit comments

Comments
 (0)