Auth not working properly in Nextjs
hey guys what could be the reason that convex auth is not working properly on my nextjs app?
setup everything as on the tutorial but after login, i dont get redirected and even if i manually go to protected route, i still get redirected back to /login page, and isAuthenticated is always loggin false
setup everything as on the tutorial but after login, i dont get redirected and even if i manually go to protected route, i still get redirected back to /login page, and isAuthenticated is always loggin false
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
nextjsMiddlewareRedirect,
} from '@convex-dev/auth/nextjs/server'
const PUBLIC_ROUTES = ['/login', '/signup', '/forgot-password', '/public', '/about']
const PROTECTED_ROUTES = ['/product(.*)', '/dashboard(.*)', '/user(.*)', '/']
const isPublicRoute = createRouteMatcher(PUBLIC_ROUTES)
const isProtectedRoute = createRouteMatcher(PROTECTED_ROUTES)
export default convexAuthNextjsMiddleware(async (request, { convexAuth }) => {
const isAuthenticated = await convexAuth.isAuthenticated()
console.log('isAuthenticated', isAuthenticated)
const url = new URL(request.url)
try {
if (url.pathname === '/login' && isAuthenticated) {
return nextjsMiddlewareRedirect(request, '/')
}
if (isProtectedRoute(request) && !isAuthenticated) {
return Response.redirect(new URL(`/login?returnTo=${url.pathname}`, request.url))
}
if (isPublicRoute(request)) {
return
}
return
} catch (error) {
console.error('Middleware error:', error)
return nextjsMiddlewareRedirect(request, '/error')
}
})
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|public).*)'],
}import {
convexAuthNextjsMiddleware,
createRouteMatcher,
nextjsMiddlewareRedirect,
} from '@convex-dev/auth/nextjs/server'
const PUBLIC_ROUTES = ['/login', '/signup', '/forgot-password', '/public', '/about']
const PROTECTED_ROUTES = ['/product(.*)', '/dashboard(.*)', '/user(.*)', '/']
const isPublicRoute = createRouteMatcher(PUBLIC_ROUTES)
const isProtectedRoute = createRouteMatcher(PROTECTED_ROUTES)
export default convexAuthNextjsMiddleware(async (request, { convexAuth }) => {
const isAuthenticated = await convexAuth.isAuthenticated()
console.log('isAuthenticated', isAuthenticated)
const url = new URL(request.url)
try {
if (url.pathname === '/login' && isAuthenticated) {
return nextjsMiddlewareRedirect(request, '/')
}
if (isProtectedRoute(request) && !isAuthenticated) {
return Response.redirect(new URL(`/login?returnTo=${url.pathname}`, request.url))
}
if (isPublicRoute(request)) {
return
}
return
} catch (error) {
console.error('Middleware error:', error)
return nextjsMiddlewareRedirect(request, '/error')
}
})
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|public).*)'],
}