You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+19-30Lines changed: 19 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,35 +67,22 @@ For the Nitro server, create a new plugin in `server/plugins/authorization-resol
67
67
68
68
```ts
69
69
exportdefaultdefineNitroPlugin((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
+
})
75
77
})
76
78
```
77
79
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`:
> Read more about the [`event.context`](https://h3.unjs.io/guide/event#eventcontext)
94
82
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.
97
84
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).
0 commit comments