-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add middleware logic to redirect to account page when logged in
- Loading branch information
Showing
2 changed files
with
20 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { auth } from '~/auth'; | ||
|
||
import { type MiddlewareFactory } from './compose-middlewares'; | ||
|
||
// The `auth` function doesn't have the correct type to support it as a MiddlewareFactory. | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
export const withAuth: MiddlewareFactory = auth as unknown as MiddlewareFactory; | ||
const AUTH_PATHS = ['/login/', '/login/forgot-password/', '/register/', '/change-password/']; | ||
|
||
export const withAuth: MiddlewareFactory = (next) => { | ||
return (request, event) => { | ||
// @ts-expect-error: The `auth` function doesn't have the correct type to support it as a MiddlewareFactory. | ||
const authWithCallback = auth(async (req) => { | ||
if (req.auth && AUTH_PATHS.includes(req.nextUrl.pathname)) { | ||
return NextResponse.redirect(new URL('/account/orders', req.nextUrl.origin)); | ||
} | ||
|
||
// Continue the middleware chain | ||
return next(req, event); | ||
}); | ||
|
||
// @ts-expect-error: The `auth` function doesn't have the correct type to support it as a MiddlewareFactory. | ||
return authWithCallback(request, event); | ||
}; | ||
}; |