Skip to content

Commit

Permalink
chore: rename session from verify to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Dec 13, 2023
1 parent a74e7f4 commit 10694e9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,15 @@ We leverage hooks to let you extend the session data with your own data or to lo
```ts
// server/plugins/session.ts
export default defineNitroPlugin(() => {
sessionHooks.hook('verify', async (session, event) => {
// Called when the session is fetched during SSR for the Vue composable (/api/_auth/session)
// Or when we call useUserSession().fetch()
sessionHooks.hook('fetch', async (session, event) => {
// extend User Session by calling your database
// or
// throw createError({ ... }) if session is invalid for example
})

// Called when we call useServerSession().clear() or clearUserSession(event)
sessionHooks.hook('clear', async (session, event) => {
// Log that user logged out
})
Expand Down
5 changes: 3 additions & 2 deletions playground/server/plugins/session.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export default defineNitroPlugin(() => {
sessionHooks.hook('verify', async (session) => {
sessionHooks.hook('fetch', async (session) => {
// Extend User Session
// Or throw createError({ ... }) if session is invalid
session.extended = {
fromHooks: true
}
})

sessionHooks.hook('clear', async () => {
sessionHooks.hook('clear', async (session) => {

Check warning on line 10 in playground/server/plugins/session.ts

View workflow job for this annotation

GitHub Actions / lint

'session' is defined but never used
// Log that user logged out
console.log('User logged out')
})
})

2 changes: 1 addition & 1 deletion src/runtime/server/api/session.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { requireUserSession, sessionHooks } from '../utils/session'
export default eventHandler(async (event) => {
const session = await requireUserSession(event)

await sessionHooks.callHookParallel('verify', session, event)
await sessionHooks.callHookParallel('fetch', session, event)

return session
})
2 changes: 1 addition & 1 deletion src/runtime/server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface SessionHooks {
* - Add extra properties to the session
* - Throw an error if the session could not be verified (with a database for example)
*/
'verify': (session: UserSession, event: H3Event) => void | Promise<void>
'fetch': (session: UserSession, event: H3Event) => void | Promise<void>
/**
* Called before clearing the session
*/
Expand Down

0 comments on commit 10694e9

Please sign in to comment.