【问题标题】:Next-auth session not created using email verification未使用电子邮件验证创建下一个身份验证会话
【发布时间】:2021-08-02 17:47:49
【问题描述】:

我正在使用 nextjs next-auth 创建一个网络应用程序,但遇到了问题。当用户单击登录按钮时,他们将被定向到电子邮件表单,这很好,然后当您输入电子邮件时,电子邮件已成功发送,但是当您从电子邮件单击登录时,您将被重定向到主页但仍未登录。控制台我收到此错误:

[next-auth][error][session_error] 
https://next-auth.js.org/errors#session_error TypeError: Cannot read property 'name' of undefined
    at Object.session (/home/dennis/shopsms/node_modules/next-auth/dist/server/routes/session.js:87:24)
[next-auth][error][session_error] 
https://next-auth.js.org/errors#session_error TypeError: Cannot read property 'name' of undefined
    at Object.session (/home/dennis/shopsms/node_modules/next-auth/dist/server/routes/session.js:87:24)
[next-auth][error][session_error] 
https://next-auth.js.org/errors#session_error TypeError: Cannot read property 'name' of undefined
    at Object.session (/home/dennis/shopsms/node_modules/next-auth/dist/server/routes/session.js:87:24)

这是我的 nextauth.js

// pages/api/auth/[...nextauth].js
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers'

const options = {
  site: process.env.NEXTAUTH_URL,
  providers: [
Providers.Email({
      name:'userauth',
      server: {
        port: 465,
        host: 'smtp.gmail.com',
        secure: true,
        auth: {
          user: process.env.EMAIL_USERNAME,
          pass: process.env.EMAIL_PASSWORD,
        },
        tls: {
          rejectUnauthorized: false,
        },
      },
       session: {
    jwt: true, 
    // Seconds - How long until an idle session expires and is no longer valid.
    maxAge: 30 * 24 * 60 * 60, // 30 days
  },

      from: process.env.EMAIL_FROM,
      
    })
  ],
    callbacks: {
    session: async (session, user) => {
      const { getUserByEmail } = await adapter.Default({}).getAdapter()
      const { id } = await getUserByEmail(session.user.email)
      session.user.id = id
      return session
    }
  },
  database: process.env.DATABASE_URL
}

export default (req, res) => NextAuth(req, res, options)

可能出了什么问题?

【问题讨论】:

    标签: sqlite email session next.js next-auth


    【解决方案1】:

    在您需要检查用户是否登录的页面上

    import { useSession } from 'next-auth/client'
      const [ session, loading ] = useSession()
    console.log(session)
    

    或服务器端

    import { getSession } from 'next-auth/client'
    
    export default async (req, res) => {
      const session = await getSession({ req })
      /* ... */
      res.end()
    }
    

    你得到了什么?

    【讨论】:

    • 我不确定。我什至尝试了 url localhot:3000/api/auth/session 并得到了 {}
    【解决方案2】:

    我认为您需要像这样指定返回的对象 return {name: session.user.name, email: session.user.email}

    【讨论】:

    • cosole.log(session) 你会看到预期返回的必填字段
    • 我把它放在哪里?
    • 我在控制台中为空
    • 在需要检查用户是否登录的页面上 import { useSession } from 'next-auth/client' const [ session, loading ] = useSession() console.log(session)
    猜你喜欢
    • 2018-02-10
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2023-03-18
    • 2012-08-10
    相关资源
    最近更新 更多