Some of the conditions are omitted in middleware

some of conditions are not checked by the middleware as when a user is authenticated and navigates to /register he should be redirected back to /home but it does not do that only /login condition works but others do not work using codes below
import { verify } from 'jsonwebtoken';
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { COOKIE_NAME } from './app/constants/cookie';
import { cookies } from 'next/headers';

export async function middleware(req: NextRequest) {

const res = NextResponse.next()

const token = req.cookies.get(COOKIE_NAME);


if (!token) {
if (req.nextUrl.pathname.startsWith('/home')) {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/login'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
else{
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
}

const user = JSON.parse(token.value)

if (user.role === "USER") {
if (req.nextUrl.pathname.startsWith('/adupload')) {
return NextResponse.rewrite(new URL('/home', req.url))
}

if (req.nextUrl.pathname.startsWith('/login')) {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/home'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}

if (req.nextUrl.pathname.startsWith('/register')) {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/home'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
}
return res
}

export const config = {
matcher: [
'/home/:path*',

],
}
import { verify } from 'jsonwebtoken';
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { COOKIE_NAME } from './app/constants/cookie';
import { cookies } from 'next/headers';

export async function middleware(req: NextRequest) {

const res = NextResponse.next()

const token = req.cookies.get(COOKIE_NAME);


if (!token) {
if (req.nextUrl.pathname.startsWith('/home')) {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/login'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
else{
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
}

const user = JSON.parse(token.value)

if (user.role === "USER") {
if (req.nextUrl.pathname.startsWith('/adupload')) {
return NextResponse.rewrite(new URL('/home', req.url))
}

if (req.nextUrl.pathname.startsWith('/login')) {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/home'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}

if (req.nextUrl.pathname.startsWith('/register')) {
const redirectUrl = req.nextUrl.clone()
redirectUrl.pathname = '/home'
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
}
return res
}

export const config = {
matcher: [
'/home/:path*',

],
}
2 Replies
JulieCezar
JulieCezar15mo ago
It can't get to /register because of your matcher
export const config = {
matcher: [
'/home/:path*',

],
}
export const config = {
matcher: [
'/home/:path*',

],
}
this means the middleware will only activate on routes that start with /home, e.g. - /home/register - /home - /home/user/idk-whatever/123 but not on /register
Revaycolizer
Revaycolizer15mo ago
Fixed already
Want results from more Discord servers?
Add your server