【问题标题】:NextAuth Credentials returns null after successfully fetch dataNextAuth Credentials 在成功获取数据后返回 null
【发布时间】:2021-08-17 08:05:36
【问题描述】:

目前我无法在我的系统中实现next-auth 凭据提供程序

我正在使用axios到不同的本地主机验证凭据(电子邮件和密码)。

  • Nextjs 系统运行在http://localhost:3000
  • 节点服务器运行在http://localhost:5400

这些是我目前面临的问题:-

  1. 成功登录后,我被重定向http://localhost:5400(空白屏幕)。即使我从系统中logout 也是如此。它仍然将我重定向到相同的网址。
  2. 即使在成功登录之后,user 也会返回null。我从session 获得的唯一数据是expires

我现在的代码:-

  • 演示按钮调用signIn & signOut:-
// in homepage (pages/home.js)
import { signIn, signOut, useSession } from 'next-auth/client'

export default Homepage = () => {
  const [ session ] = useSession()  
  console.log(session) 
  // returns 'null' (when not login yet)
  // returns { expires: "2021-06-28T08:59:47.504Z", user: { name: null, email: null, image: null, _proto_: Object }, _proto_: Object } after successful login  

  return (
    <>
       {!session ? 
        <>
          Sign-in to continue
          <button onClick={() => {
            signIn('credentials', 
            { 
              email: 'mohdfathi_mhdnoor@yahoo.com', 
              password: 'qwer1234', 
              callbackUrl: `${window.location.origin}/about` 
            })
            }}>SignIn</button>
        </> 
      :
        <>
          {session.user.email}
          <button onClick={() => signOut()}>SignOut</button>
        </>
      
      }
    </>
}
  • [...nextauth].js:-
import axios from 'axios'
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
import { config } from '../../../src/utils/Header'

export default NextAuth({
  providers: [
    Providers.Credentials({
      name: 'Portfolio V4 Credentials',
      credentials: {
        email: { label: "Email", type: "email", placeholder: "Please enter an email" },
        password: { label: "Password", type: "password", placeholder: "Please enter a password" }
      },
      async authorize(credentials, req) {
        try {
          const res = await axios.post(
            process.env.REACT_APP_SERVER_ENDPOINT_HOST + "/api/auth/callback/credentials", 
            { uid: '', email: credentials.email, password: credentials.password },
            config
          )
          const user = await res
          
          if (user.data.success) {
            console.log(user.data) // this logouts { success: true, data: { uid: '5f8fc26c6a103b243428bec1', sato: 1622295126589 } }
            return user.data
          }
        } catch (e) {
          console.log('Error: ')
          console.log(e)
          return null
        }
      }
    })
  ]
})

我确实将user 的内容更改为我从server 获得的内容。但它根本没有反映上面session I console.log 中的内容。还是console.log:-

{
  expires: "2021-06-28T08:59:47.504Z",
  user: {
    email: null,
    image: null,
    name: null,
    _proto_: Object
  },
  _proto_: Object
}

【问题讨论】:

    标签: null next.js return-value next-auth


    【解决方案1】:

    Next-Auth | When user object have too many items then session request is without data

    查看该链接下的代码。你需要的是回调函数。

    【讨论】:

    • 我本想为您的答案投票,结果点击了两次。如果我忘记回复评论,我会投票。
    猜你喜欢
    • 2023-03-09
    • 2016-03-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 2013-03-10
    • 2018-02-26
    • 1970-01-01
    相关资源
    最近更新 更多