【问题标题】:Adonis JS - Hashing PasswordAdonis JS - 哈希密码
【发布时间】:2017-04-05 15:34:52
【问题描述】:

我经历过

  1. http://adonisjs.com/docs/3.1/database-hooks#_hooks_events
  2. http://adonisjs.com/docs/3.1/encryption-and-hashing#_hashing_values
  3. https://adonisjs.svbtle.com/basic-authentication-with-adonisjs#using-hash-provider_3
  4. https://auth0.com/blog/creating-your-first-app-with-adonisj-and-adding-authentication/

还有更多。

这应该很简单,但我不知道为什么我无法弄清楚。我想在“登录”时使用 Adonis 的身份验证工具。为此,我需要在保存之前对密码进行哈希处理。我被困在这里了。

查看

<h1>Sign up</h1>
{{ form.open({url: '/addNew', action: 'UserController.addNewUser'}) }}

{{ csrfField }}

<div class="field">
{{ form.label('username', 'Choose a username') }}
{{ form.text('username') }}
</div>

<div class="field">
{{ form.label('email', 'Enter email address') }}
{{ form.text('email') }}
</div>

<div class="field">
{{ form.label('password', 'Choose a strong password') }}
{{ form.password('password') }}
</div>

<div class="button">
{{ form.submit('Sign Up') }}
</div>

{{ form.close() }}

控制器:用户控制器

'use strict'

const Database = use('Database')
const User = use('App/Model/User')
const user = new User()

class UserController {

* index (request, response) {
 const users = yield Database.select('*').from('users')
 response.json(users)
}

* addNewUser (request, response){

 user.name = request.input('username')
 user.email = request.input('email')
 user.password = request.input('password')
 user.entry = "Lorem Ipsum";

//Insert into database
  const userId = yield Database
 .insert({name: user.name, email: user.email, password: user.password, entry: user.entry})
 .into('users')

 response.json(userId)
 }
}
module.exports = UserController

模型:用户

'use strict'

const Lucid = use('Lucid')


class User extends Lucid {

static boot () {
   super.boot()
   this.addHook('beforeCreate', 'User.encryptPassword')
  }
}


module.exports = User

挂钩:用户

'use strict'

const Hash = use('Hash')
const User = exports = module.exports = {}

User.encryptPassword = function * (next) {
  this.password = yield Hash.make(request.input('password'))
  yield next
}

谢谢!

【问题讨论】:

    标签: password-hash adonis.js


    【解决方案1】:

    您应该使用Model 本身来创建记录。为什么要为此使用Database 提供程序?

    文档中没有说要新建一个模型,然后使用数据库提供程序进行调用。所以应该是

    控制器

    * addNewUser (request, response) {
      const user = new User()
      user.name = request.input('username')
      user.email = request.input('email')
      user.password = request.input('password')
      user.entry = "Lorem Ipsum";
      yield user.save()
      response.json(user.id)
    }
    

    同样在你的钩子里,你不能访问request 对象。我相信您没有费心阅读文档。

    挂钩

    'use strict'
    
    const Hash = use('Hash')
    const User = exports = module.exports = {}
    
    User.encryptPassword = function * (next) {
       this.password = yield Hash.make(this.password)
       yield next
    }
    

    http://adonisjs.com/docs/3.1/database-hooks#_basic_example查看文档中的钩子

    Hook for Adonis v4 inside model

    class User extends Model {
      static boot () {
        super.boot()
    
        this.addHook('beforeCreate', async (userInstance) => {
          userInstance.password = await Hash.make(userInstance.password)
        })
      }
    }
    

    https://adonisjs.com/docs/4.1/database-hooks#_defining_hooks查看文档中的钩子

    【讨论】:

    • 很好的答案!正如你所说,我没有费心阅读文档,只是在编造东西!
    【解决方案2】:

    有一种方法可以在不使用 Hook 或 Model 的情况下做到这一点。只需在控制器本身中散列密码即可。但我想用 Hook 来做。无论如何,这是代码:

    控制器:UserController -> addNewUser()

    user.name = request.input('username')
    user.email = request.input('email')
    const pswd = request.input('password')
    user.password = yield Hash.make(pswd)
    

    注意:我不确定 Hash.make 究竟做了哪种加密。但是 Adonis 的encryption 工具无法验证密码。还有一件事,哈希密码总是以 $2a$10$ 开头。请各位大侠帮忙!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2023-04-08
    • 2015-11-30
    • 2018-01-31
    • 2020-04-19
    • 2017-11-30
    相关资源
    最近更新 更多