Skip to content

Commit e1a48c6

Browse files
committed
docs: update usage with nitro
1 parent 57345df commit e1a48c6

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

README.md

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,22 @@ For the Nitro server, create a new plugin in `server/plugins/authorization-resol
6767

6868
```ts
6969
export default defineNitroPlugin((nitroApp) => {
70-
nitroApp.$authorization = {
71-
resolveServerUser: (event) => {
72-
// Your logic to retrieve the user from the server
73-
},
74-
}
70+
nitroApp.hooks.hook('request', async (event) => {
71+
event.context.$authorization = {
72+
resolveServerUser: () => {
73+
// Your logic to retrieve the user from the server
74+
},
75+
}
76+
})
7577
})
7678
```
7779

78-
This resolver receive the event. You can use it to retrieve the user from the session or the request. It should return the user object or `null` if the user is not authenticated. It can by async.
79-
80-
Generally, you use a plugin to fetch the user when the app starts and then store it. Resolver functions should only return the stored user and not fetch it again (otherwise, you could have severe performance issues).
81-
82-
TypeScript should complain about a missing '$authorization' property on the `nitroApp` object. You can fix this by adding a declaration in `server/nitro.d.ts`:
83-
84-
```ts
85-
import type { H3Event } from 'h3'
86-
87-
declare module 'nitropack' {
88-
interface NitroApp {
89-
$authorization: {
90-
resolveServerUser: (event: H3Event) => object | null | Promise<object | null>
91-
}
92-
}
93-
}
80+
> [!NOTE]
81+
> Read more about the [`event.context`](https://h3.unjs.io/guide/event#eventcontext)
9482
95-
export {}
96-
```
83+
This resolver is setup within the hook `request` and receive the event. You can use it to retrieve the user from the session or the request. It should return the user object or `null` if the user is not authenticated. It can by async.
9784

98-
You can replace `object` with the type of your user object.
85+
Generally, you use a plugin to fetch the user when the app starts and then store it. Resolver functions should only return the stored user and not fetch it again (otherwise, you could have severe performance issues).
9986

10087
#### Example with `nuxt-auth-utils`
10188

@@ -124,12 +111,14 @@ Nitro plugin:
124111

125112
```ts
126113
export default defineNitroPlugin((nitroApp) => {
127-
nitroApp.$authorization = {
128-
resolveServerUser: async (event) => {
129-
const session = await getUserSession(event)
130-
return session.user ?? null
131-
},
132-
}
114+
nitroApp.hooks.hook('request', async (event) => {
115+
event.context.$authorization = {
116+
resolveServerUser: async () => {
117+
const session = await getUserSession(event)
118+
return session.user ?? null
119+
},
120+
}
121+
})
133122
})
134123
```
135124

0 commit comments

Comments
 (0)