Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(session): add generated session id #338

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/runtime/server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export const sessionHooks = createHooks<SessionHooks>()
* @returns The user session
*/
export async function getUserSession(event: UseSessionEvent) {
return (await _useSession(event)).data
const session = await _useSession(event)
return {
id: session.id,
...session.data,
}
}
/**
* Set a user session
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/types/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface SecureSessionData {
}

export interface UserSession {
/**
* Session ID
*/
id: string
/**
* User session data, available on client and server
*/
Expand Down
4 changes: 3 additions & 1 deletion test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('ssr', async () => {
it('returns an empty session', async () => {
// Get response to a server-rendered page with `$fetch`.
const session = await $fetch('/api/_auth/session')
expect(session).toStrictEqual({})
// Session should be an object with an `id` property
expect(session).toBeInstanceOf(Object)
expect(session).toHaveProperty('id')
})
})