gpt4 book ai didi

password-hash - Adonis JS - 哈希密码

转载 作者:行者123 更新时间:2023-12-04 20:33:22 24 4
gpt4 key购买 nike

我经历过

  • http://adonisjs.com/docs/3.1/database-hooks#_hooks_events
  • http://adonisjs.com/docs/3.1/encryption-and-hashing#_hashing_values
  • https://adonisjs.svbtle.com/basic-authentication-with-adonisjs#using-hash-provider_3
  • 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() }}

    Controller :用户 Controller
    '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

    钩子(Hook):用户
    '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
    }

    谢谢!

    最佳答案

    您应该使用 Model自己创建记录。为什么使用 Database提供者?

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

    Controller

    * 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)
    }

    同样在您的钩子(Hook)中,您无权访问 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
    }

    在此处查看钩子(Hook)的文档 http://adonisjs.com/docs/3.1/database-hooks#_basic_example

    Adonis v4 内部模型的 Hook
    class User extends Model {
    static boot () {
    super.boot()

    this.addHook('beforeCreate', async (userInstance) => {
    userInstance.password = await Hash.make(userInstance.password)
    })
    }
    }

    在此处查看钩子(Hook)的文档 https://adonisjs.com/docs/4.1/database-hooks#_defining_hooks

    关于password-hash - Adonis JS - 哈希密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733742/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com