fix: revert getSessionCookie in middleware
This commit is contained in:
parent
3664d0920b
commit
2df67a1075
@ -1,7 +1,8 @@
|
||||
import { getSessionCookie } from 'better-auth/cookies';
|
||||
import { betterFetch } from '@better-fetch/fetch';
|
||||
import createMiddleware from 'next-intl/middleware';
|
||||
import { type NextRequest, NextResponse } from 'next/server';
|
||||
import { LOCALES, routing } from './i18n/routing';
|
||||
import type { Session } from './lib/auth-types';
|
||||
import {
|
||||
DEFAULT_LOGIN_REDIRECT,
|
||||
protectedRoutes,
|
||||
@ -20,13 +21,22 @@ const intlMiddleware = createMiddleware(routing);
|
||||
* In Next.js middleware, it's recommended to only check for the existence of a session cookie
|
||||
* to handle redirection. To avoid blocking requests by making API or database calls.
|
||||
*/
|
||||
export default async function middleware(request: NextRequest) {
|
||||
const { nextUrl } = request;
|
||||
export default async function middleware(req: NextRequest) {
|
||||
const { nextUrl, headers } = req;
|
||||
console.log('>> middleware start, pathname', nextUrl.pathname);
|
||||
|
||||
const sessionCookie = getSessionCookie(request);
|
||||
// console.log('middleware, sessionCookie', sessionCookie);
|
||||
const isLoggedIn = !!sessionCookie;
|
||||
// do not use getSession() here, it will cause error related to edge runtime
|
||||
// const session = await getSession();
|
||||
const { data: session } = await betterFetch<Session>(
|
||||
'/api/auth/get-session',
|
||||
{
|
||||
baseURL: req.nextUrl.origin,
|
||||
headers: {
|
||||
cookie: req.headers.get('cookie') || '', // Forward the cookies from the request
|
||||
},
|
||||
}
|
||||
);
|
||||
const isLoggedIn = !!session;
|
||||
// console.log('middleware, isLoggedIn', isLoggedIn);
|
||||
|
||||
// Get the pathname of the request (e.g. /zh/dashboard to /dashboard)
|
||||
@ -71,7 +81,7 @@ export default async function middleware(request: NextRequest) {
|
||||
|
||||
// Apply intlMiddleware for all routes
|
||||
console.log('<< middleware end, applying intlMiddleware');
|
||||
return intlMiddleware(request);
|
||||
return intlMiddleware(req);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user