Get authenticated user in validator

I want to get logged in user in validator for unique validation rules.

Validators/EditProfile.Js

class EditProfile {
  get validateAll () {
    return true
  }

  get rules () {
    const user = await this.ctx.auth.authenticator('session').getUser()
    console.log(user)
    return {
      email: `required|email|unique:users,email,id,${user.id}`,
      firstName: 'required|max:50|alpha',
      lastName: 'required|max:50|alpha'
    }
  }
}

Here rules() method is not async and we can’t make it async so I am stuck to get authenticated user.

How can I get logged in user in rules() method?

2 Likes

const { id } = this.ctx.auth.user;

1 Like

hey, @klawdyo Thanks for the reply.

But still it seems to be some problem. const { id } = this.ctx.auth.user; returns the TypeError: Cannot destructure property id of ‘undefined’ or ‘null’. error.

Are you authenticated?

It’s working with this:

const { id } = this.ctx.auth.authenticator('session').user

Thanks for help @klawdyo

1 Like