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: allow users to define custom session factory + types #2

Merged
merged 5 commits into from
Nov 7, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ jobs:
- name: Run test suite
run: pnpm test

# - name: Test types
# run: pnpm test:types
- name: Test types
run: pnpm test:types

# - name: Test playground types
# run: pnpm test:types:playground
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ await clearUserSession(event)
const session = await requireUserSession(event)
```

You can define the type for your user session by creating a type declaration file (for example, `auth.d.ts`) in your project:

```ts
declare module '#auth-utils' {
interface UserSession {
// define the type here
}
}
```

### OAuth Event Handlers

All helpers are exposed from the `oauth` global variable and can be used in your server routes or API routes.
Expand Down
9 changes: 9 additions & 0 deletions playground/auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module '#auth-utils' {
interface UserSession {
user: {
spotify?: any
github?: any
}
loggedInAt: number
}
}
3 changes: 2 additions & 1 deletion playground/server/routes/auth/github.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export default oauth.githubEventHandler({
await setUserSession(event, {
user: {
github: user,
}
},
loggedInAt: Date.now()
})

return sendRedirect(event, '/')
Expand Down
3 changes: 2 additions & 1 deletion playground/server/routes/auth/spotify.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export default oauth.spotifyEventHandler({
await setUserSession(event, {
user: {
spotify: user,
}
},
loggedInAt: Date.now()
})

return sendRedirect(event, '/')
Expand Down
2 changes: 2 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default defineNuxtModule<ModuleOptions>({
}
}

nuxt.options.alias['#auth-utils'] = resolver.resolve('./runtime/types/auth-utils-session')

// App
addImportsDir(resolver.resolve('./runtime/composables'))
addPlugin(resolver.resolve('./runtime/plugins/session.server'))
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/composables/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useState, computed, useRequestFetch } from '#imports'
// import { UserSession } from '../server/utils/session'
interface UserSession {}
import type { UserSession } from '#auth-utils'

const useSessionState = () => useState<UserSession>('nuxt-session', () => ({}))

Expand Down
6 changes: 1 addition & 5 deletions src/runtime/server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import type { H3Event } from 'h3'
import { useSession, createError } from 'h3'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'

export interface UserSession {
user?: any
[key: string]: any
}
import type { UserSession } from '#auth-utils'

export async function getUserSession (event: H3Event) {
return (await _useSession(event)).data as UserSession
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/types/auth-utils-session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface UserSession {
user?: {}
}