Skip to content

Commit

Permalink
Merge branch 'feature/admin'
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed Sep 19, 2024
2 parents ee36315 + 815cd1c commit ff87351
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
13 changes: 8 additions & 5 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const token = request.cookies.get(SESSION_COOKIE)?.value;

// update session
if (token) {
const sessionUpdated = await updateSession(request);
if (!sessionUpdated) {
return handleExpiredSession(request, SESSION_COOKIE, SESSION_EXPIRED_URL);
}
}

// check if it's a public route
if (PUBLIC_ROUTES.includes(path)) {
return handlePublicRoute(request, token);
}

// update session for non-public routes
if (token && !(await updateSession(request))) {
return handleExpiredSession(request, SESSION_COOKIE, SESSION_EXPIRED_URL);
}

// handle private routes
if (PRIVATE_ROUTES.includes(`/${path.split("/")[1]}`)) {
return handlePrivateRoute(request, token);
Expand Down
15 changes: 11 additions & 4 deletions middlewares/handleApiRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { decrypt } from "@sessions/sessionUtils";
import { JWTExpired, JWTInvalid } from "jose/errors";

export default async function handleApiRoute(request: NextRequest) {
const path = request.nextUrl.pathname;

if (
path === "/api/login" ||
path === "/api/signup" ||
path === "/api/demouser"
) {
// skipping middleware
return NextResponse.next();
}

const authHeader = request.headers.get("Authorization");

if (!authHeader || !authHeader.startsWith("Bearer ")) {
Expand Down Expand Up @@ -52,7 +63,3 @@ export default async function handleApiRoute(request: NextRequest) {
}
}
}

export const config = {
matcher: ["/((?!api/login|api/signup|api/demouser).*)"],
};

0 comments on commit ff87351

Please sign in to comment.