pirvate route / middleware
Answered
Snail Kite posted this in #help-forum
Snail KiteOP
hi there, i have my private route "listening", and i have done my middleware too. the problem is when i try to go "listening" after login, it redirect me to login page again until i refresh the app. anyone have solution. i'm using nextjs 14.0.4.
19 Replies
Snail KiteOP
// import { NextResponse } from 'next/server'
import { NextResponse, type NextRequest } from 'next/server'
// This function can be marked
export function middleware(request: NextRequest) {
// Only apply middleware logic to page requests
if (
request.nextUrl.pathname.startsWith('/_next')
request.nextUrl.pathname.includes('.')
request.nextUrl.pathname.startsWith('/api')
) {
// It's an asset or API request, not a page request, so do nothing
return
}
const tokenCookie = request.cookies.get('authState')
const privateRoutes = [
'/settings',
'/speaking',
'/speakingtopicpage',
'/listening',
]
console.log(tokenCookie?.value +"---"+request.nextUrl.pathname)
if (privateRoutes.find((route) => route === request.nextUrl.pathname)) {
if (!tokenCookie?.value) {
return NextResponse.redirect(new URL('/login', request.url))
}
} else if (
request.nextUrl.pathname == '/login'
request.nextUrl.pathname == '/signup'
) {
if (tokenCookie?.value) {
return NextResponse.redirect(new URL('/', request.url))
}
}
// if (tokenCookie?.value) {
// if (
// request.nextUrl.pathname == '/login'
// request.nextUrl.pathname == '/signup'
// ) {
// return NextResponse.redirect(new URL('/', request.url))
// }
// } else if (
// privateRoutes.find((route) => route === request.nextUrl.pathname)
// ) {
// return NextResponse.redirect(new URL('/login', request.url))
// }
}
import { NextResponse, type NextRequest } from 'next/server'
// This function can be marked
async if using await insideexport function middleware(request: NextRequest) {
// Only apply middleware logic to page requests
if (
request.nextUrl.pathname.startsWith('/_next')
request.nextUrl.pathname.includes('.')
request.nextUrl.pathname.startsWith('/api')
) {
// It's an asset or API request, not a page request, so do nothing
return
}
const tokenCookie = request.cookies.get('authState')
const privateRoutes = [
'/settings',
'/speaking',
'/speakingtopicpage',
'/listening',
]
console.log(tokenCookie?.value +"---"+request.nextUrl.pathname)
if (privateRoutes.find((route) => route === request.nextUrl.pathname)) {
if (!tokenCookie?.value) {
return NextResponse.redirect(new URL('/login', request.url))
}
} else if (
request.nextUrl.pathname == '/login'
request.nextUrl.pathname == '/signup'
) {
if (tokenCookie?.value) {
return NextResponse.redirect(new URL('/', request.url))
}
}
// if (tokenCookie?.value) {
// if (
// request.nextUrl.pathname == '/login'
// request.nextUrl.pathname == '/signup'
// ) {
// return NextResponse.redirect(new URL('/', request.url))
// }
// } else if (
// privateRoutes.find((route) => route === request.nextUrl.pathname)
// ) {
// return NextResponse.redirect(new URL('/login', request.url))
// }
}
Snail KiteOP
logic of middleware?
no, the code on how do you login the user?
Snail KiteOP
i have also applied to do revalidatePath("/") by makign a route inside api/revalidate/route.ts
@Snail Kite Click to see attachment
try add
router.refresh() before router.push('/')Answer
Snail KiteOP
but somehow it is not working
@Ray try add `router.refresh()` before `router.push('/')`
Snail KiteOP
ok let me try
aslo let me mention it is working all fine on local. it's only issue on production
yes try it
Snail KiteOP
it is working now. thanks to you 🙌🻠but is it right to use here? like it is not doing the refresh but still doesn't it degrade the quality? or is it the bug with next version or my app?
@Snail Kite it is working now. thanks to you 🙌🻠but is it right to use here? like it is not doing the refresh but still doesn't it degrade the quality? or is it the bug with next version or my app?
there is a router cache and the route got cached by prefetching which is only enable in production
so you have to refresh the route after setting the cookie
Snail KiteOP
so is there a way to remove it? because as long as i remember it wasn't issue in previous versions
btw, you should use server action and use http only cookie instead of setting the cookie on client side
by setting the cookie with server action also invalidate the router cache