KevinK
Convex Community2y ago
10 replies
Kevin

[Convex Auth] Middleware on Nextjs

Following https://labs.convex.dev/auth/authz/nextjs

After using signIn from useAuthActions by Email OTP (from resend),

const { signIn } = useAuthActions()

await signIn("resend-otp", { email , code})


on client side,
 const { isAuthenticated, isLoading } = useConvexAuth()

I see isAuthenticated = true

however,

on middleware,
import {
  convexAuthNextjsMiddleware,
  createRouteMatcher,
  isAuthenticatedNextjs,
  nextjsMiddlewareRedirect,
} from "@convex-dev/auth/nextjs/server";
 
const isSignInPage = createRouteMatcher(["/signin"]);
const isProtectedRoute = createRouteMatcher(["/product(.*)"]);
 
export default convexAuthNextjsMiddleware((request) => {
  if (isSignInPage(request) && isAuthenticatedNextjs()) {
    return nextjsMiddlewareRedirect(request, "/product");
  }
  if (isProtectedRoute(request) && !isAuthenticatedNextjs()) {
    return nextjsMiddlewareRedirect(request, "/signin");
  }
});
 
export const config = {
  // The following matcher runs middleware on all routes
  // except static assets.
  matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};


isAuthenticatedNextjs() is always false

can you guys let me know how to debug this? There is no error shown from console or terminal?
Authentication library for your Convex backend
Was this page helpful?