最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Next Auth middleware and protected folder - Stack Overflow

programmeradmin0浏览0评论

I am building a NextJS app and I have a series of routers at pages/dashboard/* that I want to protect. I am using Prisma but for the moment what I want to do is to being able to access the routes only if the user is logged in. I am using Google provider and everything is setup correctly. This is my middleware.ts

export { default } from "next-auth/middleware"

export const config = { matcher: ["/dashboard"] }

I am building a NextJS app and I have a series of routers at pages/dashboard/* that I want to protect. I am using Prisma but for the moment what I want to do is to being able to access the routes only if the user is logged in. I am using Google provider and everything is setup correctly. This is my middleware.ts

export { default } from "next-auth/middleware"

export const config = { matcher: ["/dashboard"] }

Is very simple. This is my [...nextAuth] file

import NextAuth, { NextAuthOptions } from "next-auth" import GoogleProvider from "next-auth/providers/google" import { PrismaAdapter } from "@next-auth/prisma-adapter" import { prisma } from "@/lib/db/prisma"

export const authOptions: NextAuthOptions = {
  adapter: PrismaAdapter(prisma),
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
      authorization: {
        params: {
          prompt: "consent",
          access_type: "offline",
          response_type: "code",
        },
      },
    }),
  ],
}

export default NextAuth(authOptions)

I have the secrets in a .env.local also the NEXTAUTH_SECRET created locally. The problem I am facing is that whenever the user goes to dashboard it gets redirected to the login, once you login the page is still not accessible and keeps asking for login. I noticed that the useSession returns me the data in the browser, but apparently the token or session in the node side is null. I have Next 13.2.4 and Next Auth ^4.20.1 I can't figure out why is not working, I tried all the solutions proposed in other questions but none of them works and I am unable to see the dashboard. Any help? Thanks

Share Improve this question asked Mar 14, 2023 at 10:30 Kaiser SozeKaiser Soze 1,4983 gold badges17 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Found the solution, for those who might be in the same situation: I added the strategy to the next auth options:

 session: {
    strategy: "jwt",
  },

and now it works.

Try adding NEXTAUTH_SECRET on authOptions.

 export const authOptions: NextAuthOptions = {
  adapter: PrismaAdapter(prisma),
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
      authorization: {
        params: {
          prompt: "consent",
          access_type: "offline",
          response_type: "code",
        },
      },
    }),
  ],
session: {
    strategy: 'jwt'
},
secret: process.env.NEXTAUTH_SECRET

}

export default NextAuth(authOptions)
发布评论

评论列表(0)

  1. 暂无评论