how to get auth.user in model #3642
Answered
by
Julien-R44
hariprasath-yadav
asked this question in
Help
-
How to get authenticated user details like id in Hooks functions (ex: @beforeSave()) under database model |
Beta Was this translation helpful? Give feedback.
Answered by
Julien-R44
Mar 24, 2022
Replies: 1 comment
-
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: 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.
} |
Beta Was this translation helpful? Give feedback.
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
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 :