Skip to content

Commit

Permalink
rm: unwanted url excludes for auth check
Browse files Browse the repository at this point in the history
  • Loading branch information
debendraoli committed Nov 26, 2024
1 parent 8dd66f8 commit dde8e01
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,33 @@ import { NextRequestWithAuth, withAuth } from 'next-auth/middleware'
import { NextMiddlewareResult } from 'next/dist/server/web/types'
import { NextRequest, NextResponse, type NextFetchEvent } from 'next/server'

export default async function middleware(request: NextRequest, event: NextFetchEvent) {
const acceptLanguage = request.headers.get('accept-language') ?? ''
const headers = { 'accept-language': acceptLanguage === '*' ? 'en' : acceptLanguage }
const languages = new Negotiator({ headers }).languages()
const locales = getLocales()
const excludedPaths = ['/login']

const locale = match(languages, locales, defaultLocale)
const response = NextResponse.next()
if (!request.cookies.get('locale')) {
response.cookies.set('locale', locale)
}
export default async function middleware(req: NextRequest, event: NextFetchEvent) {
const acceptLanguage = req.headers.get('accept-language') ?? ''
const headers = { 'accept-language': acceptLanguage === '*' ? 'en' : acceptLanguage }
const languages = new Negotiator({ headers }).languages()
const locales = getLocales()

const excludedPaths = [
'/login',
'/api/auth/providers',
'/api/auth/csrf',
'/api/auth/callback/credentials',
]
const locale = match(languages, locales, defaultLocale)
const response = NextResponse.next()
if (!req.cookies.get('locale')) {
response.cookies.set('locale', locale)
}

if (!excludedPaths.includes(request.nextUrl.pathname)) {
const res: NextMiddlewareResult = await withAuth(
// Response with local cookies
() => response,
{
// Matches the pages config in `[...nextauth]`
pages: {
signIn: '/login',
},
},
)(request as NextRequestWithAuth, event)
return res
}
if (!excludedPaths.includes(req.nextUrl.pathname)) {
const res: NextMiddlewareResult = await withAuth(
// Response with local cookies
() => response,
{
// Matches the pages config in `[...nextauth]`
pages: {
signIn: '/login',
},
}
)(req as NextRequestWithAuth, event)
return res
}

return response
return response
}

0 comments on commit dde8e01

Please sign in to comment.