Skip to content

how to get auth.user in model #3642

Discussion options

You must be logged in to vote

Hello 👋

As far as I know, since authentication depends on the context of the HTTP request, the only way to do this is to use local storage async. So basically, you have to enable local storage async:
https://docs.adonisjs.com/guides/async-local-storage#usage

And then in your hook you can do :

@beforeSave()
public static async myHook(user: User) {
    const auth = HttpContext.get()!.auth
   // And here you do what you want with the `auth` object.
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Julien-R44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants