feat(#964): Add Hooks provider#1062
Conversation
commit: |
commit: |
docs: improve documentation
| ## `createRequest(data, authState, nuxt)` | ||
|
|
||
| Prepare data for the fetch call. | ||
|
|
||
| Must return either an object conforming to: | ||
|
|
||
| ```ts | ||
| interface CreateRequestResult { | ||
| // Path to the endpoint | ||
| path: string | ||
| // Request: body, headers, etc. | ||
| request: NitroFetchOptions | ||
| } | ||
| ``` |
There was a problem hiding this comment.
Why did you decide to abstract the returns of the hooks provider? While I think it is a good step forward compared to the current local-provider I think it could still limit you if you e.g. want to switch from ofetch to axios under the hood.
There was a problem hiding this comment.
I don't think it would ever make sense for us to switch the underlying fetch library to something outside Nuxt ecosystem. At the moment ofetch is the default library of Nuxt, therefore it is being used
| ### `authState` argument | ||
|
|
||
| This argument gives you access to the state of the module, allowing to read or modify session data or tokens. |
There was a problem hiding this comment.
Do we have a schema for this or this is e.g. based off the session object you can provide via the nuxt config?
There was a problem hiding this comment.
Yes, we do! I updated the documentation for it to point to useAuthState:
|
|
||
| This argument gives you access to the state of the module, allowing to read or modify session data or tokens. | ||
|
|
||
| ### `nuxt` argument |
There was a problem hiding this comment.
Should we call this nuxtApp like Nuxt also does in the docs you linked below?
| Handle the response and optionally instruct the module how to update state. | ||
|
|
||
| May return: | ||
| * `false` — stop further processing (module will not update auth state). |
There was a problem hiding this comment.
Does this false act the same way as the return false from the createRequest hook?
There was a problem hiding this comment.
Yep, exactly the same - the meaning of false is to stop execution without an error
| * `ResponseAccept` object — instruct the module what to set in `authState` (see below). | ||
| * Throw an `Error` to propagate a failure. | ||
|
|
||
| The `response` argument is the [`ofetch` raw response](https://github.com/unjs/ofetch?tab=readme-ov-file#-access-to-raw-response) that the module uses as well. `response._data` usually contains parsed body. |
There was a problem hiding this comment.
What else can response._data contain if not the parsed body? E.g. do we do error handling here or just return whatever was in the body?
There was a problem hiding this comment.
We do error handling for the request:
nuxt-auth/src/runtime/composables/hooks/useAuth.ts
Lines 78 to 92 in c416c1e
But thinking a bit more, we are silently consuming the error which is not very good for this usecase:
nuxt-auth/src/runtime/utils/fetch.ts
Lines 75 to 88 in c416c1e
It was done like so to avoid leaking exact error details (which might not be caught by consuming code and passed to the app side -> viewable by the frontend). I will consider if this could be improved, there's an external PR for better error handling:
#1069
| onResponse(response) { | ||
| // Backend returns { access: 'xxx', refresh: 'yyy', user: {...} } | ||
| const body = response._data | ||
| // Default to `undefined` to not reset the tokens and session (but you may want to reset it) | ||
| return { | ||
| token: body?.access ?? undefined, | ||
| refreshToken: body?.refresh ?? undefined, | ||
| session: body?.user ?? undefined, | ||
| } | ||
| }, |
There was a problem hiding this comment.
Are these hooks promises? E.g. if I wanted to do another DB or API call here, would the hooks provider support this?
There was a problem hiding this comment.
Yes, the hooks are awaitable:
| } | ||
| }, | ||
|
|
||
| onResponse(response) { |
There was a problem hiding this comment.
If I return false from the getSession.createRequest hook is this response:
falsenull/undefinedonResponseis never called
There was a problem hiding this comment.
The onResponse is never called:
nuxt-auth/src/runtime/composables/hooks/useAuth.ts
Lines 73 to 76 in c416c1e
|
|
||
| This argument gives you access to the state of the module, allowing to read or modify session data or tokens. | ||
|
|
||
| ### `nuxt` argument |
There was a problem hiding this comment.
Do we want to add a guide on how you can use this to e.g. access runtimeConfig variables? I think this could be a pretty common workflow.
🔗 Linked issue
Closes #964
❓ Type of change
📚 Description
📝 Checklist