adonisjs / auth

Official Authentication package for AdonisJS
https://docs.adonisjs.com/guides/auth/introduction
MIT License
194 stars 65 forks source link

Error when using the Hash package to register the user #157

Closed leandrodavimg closed 3 years ago

leandrodavimg commented 3 years ago

I am using AdonisJs V5 to create an API.

I followed the following steps

When trying to register a user I get the following error.

image

Apparently I have an error in Hash, I discovered this because when changing the model line the user is saved in the database.

@beforeSave()
  public static async hashPassword(user: User) {
    if (user.$dirty.password) {
      // const test = await Hash.make(user.password)
      user.password = 'new_pass'
    }
  }

We do our best to reply to all the issues on time. If you will follow the given guidelines, the turn around time will be faster.

Package version

Node.js and npm version

Sample Code (to reproduce the issue)

my model

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

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

  @column()
  public email: 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) {
      // const test = await Hash.make(user.password)
      user.password = await Hash.make(user.password)
    }
  }
}

My Controller

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import User from 'App/Models/User'

export default class UsersController {
  public async store({ request }: HttpContextContract) {
    try {
      const { email, password } = request.all()
      const user = new User()
      user.email = email
      user.password = password
      await user.save()
      return user
    } catch (err) {
      return err
    }
  }

  public async index() {
    const user = await User.all()

    return user
  }
}

I did the process twice thinking it was an error in the first configuration and the same error was pointed out the second time.

sorry, it's a heavy gif but it describes my step by step Erro-adonisJs

leandrodavimg commented 3 years ago

ok, after researching a bit I found the solution in the community.

https://github.com/adonisjs/core/discussions/1603