javascript - Next Auth middleware and protected folder - Stack Overflow

I am building a NextJS app and I have a series of routers at pagesdashboard* that I want to protect.

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744770983a4592757.html

相关推荐

  • javascript - Next Auth middleware and protected folder - Stack Overflow

    I am building a NextJS app and I have a series of routers at pagesdashboard* that I want to protect.

    20小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信