3

我将 nextAuth 与我自己的用户数据库(mongoDb)一起使用,我希望该人能够使用他们的凭据登录。问题是我的用户对象没有保持状态,这似乎是 nextAuth 的人们阻止用户名和密码的设计选择,他们说:

If you use a custom credentials provider user accounts will not be 
persisted in a database by NextAuth.js (even if one is configured)

这似乎是一个巨大的障碍......我目前这样做是为了克服它,我想知道这里有什么缺点:

 async authorize(credentials) {

        // check if the user exists in the database
        let user = await db.collection('users').findOne({username:credentials.username})

        if (user && user.password == credentials.password) {
          // return the user object returned from the database
          // because next auth will not save this is state
          return {name: {user}}
        } else {
          return {name: null, email: null, image: null}
        }
      }

4

1 回答 1

0

缺点是您必须自己管理将密码保存在数据库中的安全方面。

我们通常推荐具有经过实战考验的系统的第三方提供商来保护您的密码安全。如果您对推出自己的解决方案感到满意,那么它没有任何问题。

作为警告说明⚠,仅从您上面的代码中,我已经看到了一个巨大的安全问题。您似乎将密码作为纯文本存储在数据库中。如果您的数据库被泄露,您的所有用户帐户将立即可供黑客使用。

于 2021-09-19T11:11:16.147 回答