Skip to content

Commit

Permalink
fix: add middleware logic to redirect to account page when logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemoya committed Feb 4, 2025
1 parent 35753c0 commit 8099817
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
19 changes: 0 additions & 19 deletions core/app/[locale]/(default)/(auth)/layout.tsx

This file was deleted.

23 changes: 20 additions & 3 deletions core/middlewares/with-auth.ts
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);
};
};

0 comments on commit 8099817

Please sign in to comment.