简体   繁体   English

如何在adonisjs中使用Hash.make(user.password)

[英]How to use Hash.make(user.password) in adonisjs

Trying to encrypt a user password using Hash.make(user.password) from import Hash from '@ioc:Adonis/Core/Hash'尝试使用Hash.make(user.password) from import Hash from '@ioc:Adonis/Core/Hash'加密用户密码

The problem happens when the code tries to execute the Hash.make present inside @beforesave it gives me a referenceError.当代码尝试执行存在于@beforesave 中的Hash.make时,就会出现问题,它给了我一个referenceError。

message:'Hash is not defined'
stack:'ReferenceError: Hash is not defined\n    at eval (eval at hashPassword (/usr/src/app/Models/User.ts:35:9), <anonymous>:2:28)

Here is the user Model.这里是用户 Model。


import { DateTime } from 'luxon'
import Hash from '@ioc:Adonis/Core/Hash'
import { column, beforeSave, BaseModel } from '@ioc:Adonis/Lucid/Orm'
import Logger from '@ioc:Adonis/Core/Logger'

export default class User extends BaseModel {
  @column({ isPrimary: true })
  public id: number

  @column()
  public email: string

  @column({ columnName: 'first_name' })
  public firstName: string

  @column({ columnName: 'last_name' })
  public lastName: string

  @column({ serializeAs: null })
  public password: string

  @column()
  public rememberMeToken?: string

  @column.dateTime({ autoCreate: true })
  public createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  public updatedAt: DateTime

  @beforeSave()
  public static async hashPassword(user: User) {
    if (user.$dirty.password) {
      try {
        user.password = await Hash.make(user.password)
      }
      catch (error) {
        Logger.error(error)
      }
    }
  }
}


When debugging steps goes into the right path belonging to the Hash dependency, but it fails inside the hash driver I think当调试步骤进入属于 Hash 依赖项的正确路径时,我认为它在 hash 驱动程序中失败

Since I noticed it only happening inside docker, the problem seems to be with the default driver inside docker.因为我注意到它只发生在 docker 内部,所以问题似乎出在 docker 内部的默认驱动程序上。

I solved this by using this:我通过使用这个解决了这个问题:

await Hash.use('bcrypt').make(user.password)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM