Skip to content

Commit

Permalink
feat(session): ✨ Updated refresh to include session and added refresh…
Browse files Browse the repository at this point in the history
… hook
  • Loading branch information
itpropro committed Sep 20, 2024
1 parent 322b01d commit bb8f159
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ The following hooks are available to extend the default behavior of the OIDC mod
- `clear` (Called before a user session is cleared)
- `refresh` (Called before a user session is refreshed)

:warning: Remember to also update the refresh hook if you modify the session, as claims and other fields would otherwise be wiped.

#### Example

```ts
Expand All @@ -333,9 +335,17 @@ export default defineNitroPlugin(() => {
// session.extended = {
// fromHooks: true
// }
console.log('Injecting "country" claim as test')
console.log('Injecting "status" claim as test')
if (!(Object.keys(session).length === 0)) {
const claimToAdd = { status: 'Fetch' }
session.claims = { ...session.claims, ...claimToAdd }
}
})

sessionHooks.hook('refresh', async (session) => {
console.log('Injecting "status" claim as test on refresh')
if (!(Object.keys(session).length === 0)) {
const claimToAdd = { country: 'Germany' }
const claimToAdd = { status: 'Refresh' }
session.claims = { ...session.claims, ...claimToAdd }
}
})
Expand Down
2 changes: 2 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export default defineNuxtConfig({
clientId: '',
clientSecret: '',
redirectUri: 'http://localhost:3000/auth/keycloak/callback',
exposeAccessToken: true,
userNameClaim: 'preferred_username',
},
},
session: {
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"@iconify-json/simple-icons": "^1.2.3",
"@nuxtjs/color-mode": "^3.5.1",
"@unocss/nuxt": "^0.62.4",
"nuxt-oidc-auth": "workspace:^"
"nuxt-oidc-auth": "latest"
}
}
12 changes: 10 additions & 2 deletions playground/server/plugins/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ export default defineNitroPlugin(() => {
// fromHooks: true
// }
// eslint-disable-next-line no-console
console.log('Injecting "country" claim as test')
console.log('Injecting "status" claim as test on fetch')
if (!(Object.keys(session).length === 0)) {
const claimToAdd = { country: 'Germany' }
const claimToAdd = { status: 'Fetch' }
session.claims = { ...session.claims, ...claimToAdd }
}
})

sessionHooks.hook('refresh', async (session) => {
console.log('Injecting "status" claim as test on refresh')
if (!(Object.keys(session).length === 0)) {
const claimToAdd = { status: 'Refresh' }
session.claims = { ...session.claims, ...claimToAdd }
}
})
Expand Down
8 changes: 6 additions & 2 deletions src/runtime/composables/oidcAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ export function useOidcAuth() {
* @returns {Promise<void>}
*/
async function refresh(): Promise<void> {
await $fetch('/api/_auth/refresh', { method: 'POST' })
await fetch()
useSessionState().value = (await useRequestFetch()('/api/_auth/refresh', {
headers: {
Accept: 'text/json',
},
method: 'POST',
}).catch(() => (undefined)) as UserSession)
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/server/api/refresh.post.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { eventHandler } from 'h3'
import { getUserSession, refreshUserSession } from '../utils/session'
import { getUserSession, refreshUserSession, sessionHooks } from '../utils/session'

export default eventHandler(async (event) => {
await getUserSession(event)
await refreshUserSession(event)
return { refreshed: true }
const session = await refreshUserSession(event)
await sessionHooks.callHookParallel('refresh', 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 @@ -89,7 +89,7 @@ export async function refreshUserSession(event: H3Event) {
await useStorage('oidc').setItem<PersistentSession>(session.id as string, updatedPersistentSession)
await session.update(defu(user, session.data))

return true
return session.data
}

// Deprecated, please use getUserSession
Expand Down

0 comments on commit bb8f159

Please sign in to comment.