Skip to content

Commit 5b060b8

Browse files
fix(oauth): add generic OAuthConfig type (#18)
* fix(oauth): add generic OAuhtConfig type * fix(oauth): use generic OAuthConfig type * fix(oauth/battlenet): use oauthconfig type * chore: revert some formatting changes * refactor: expose `OAuthConfig` type from `#auth-utils` --------- Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent 13daa78 commit 5b060b8

File tree

13 files changed

+39
-61
lines changed

13 files changed

+39
-61
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@ await clearUserSession(event)
108108
const session = await requireUserSession(event)
109109
```
110110

111-
You can define the type for your user session by creating a type declaration file (for example, `auth.d.ts`) in your project:
111+
You can define the type for your user session by creating a type declaration file (for example, `auth.d.ts`) in your project to augment the `UserSession` type:
112112

113113
```ts
114114
declare module '#auth-utils' {
115115
interface UserSession {
116116
// define the type here
117117
}
118118
}
119+
export {}
119120
```
120121

121122
### OAuth Event Handlers

playground/auth.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ declare module '#auth-utils' {
1212
loggedInAt: number
1313
}
1414
}
15+
16+
export {}

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default defineNuxtModule<ModuleOptions>({
2222
console.log(`NUXT_SESSION_PASSWORD=${randomPassword}`)
2323
}
2424

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

2727
// App
2828
addImportsDir(resolver.resolve('./runtime/composables'))

src/runtime/server/lib/oauth/auth0.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { H3Event, H3Error } from 'h3'
1+
import type { H3Event } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parsePath } from 'ufo'
44
import { ofetch } from 'ofetch'
55
import { defu } from 'defu'
66
import { useRuntimeConfig } from '#imports'
7+
import type { OAuthConfig } from '#auth-utils'
78

89
export interface OAuthAuth0Config {
910
/**
@@ -40,13 +41,7 @@ export interface OAuthAuth0Config {
4041
emailRequired?: boolean
4142
}
4243

43-
interface OAuthConfig {
44-
config?: OAuthAuth0Config
45-
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
46-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
47-
}
48-
49-
export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig) {
44+
export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthAuth0Config>) {
5045
return eventHandler(async (event: H3Event) => {
5146
// @ts-ignore
5247
config = defu(config, useRuntimeConfig(event).oauth?.auth0) as OAuthAuth0Config

src/runtime/server/lib/oauth/battledotnet.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import type { H3Event, H3Error } from 'h3'
1+
import type { H3Event } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { ofetch } from 'ofetch'
44
import { withQuery, parsePath } from 'ufo'
55
import { defu } from 'defu'
66
import { useRuntimeConfig } from '#imports'
77
import { randomUUID } from 'crypto'
8+
import type { OAuthConfig } from '#auth-utils'
89

910
export interface OAuthBattledotnetConfig {
1011
/**
@@ -43,13 +44,7 @@ export interface OAuthBattledotnetConfig {
4344
tokenURL?: string
4445
}
4546

46-
interface OAuthConfig {
47-
config?: OAuthBattledotnetConfig
48-
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
49-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
50-
}
51-
52-
export function battledotnetEventHandler({ config, onSuccess, onError }: OAuthConfig) {
47+
export function battledotnetEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthBattledotnetConfig>) {
5348
return eventHandler(async (event: H3Event) => {
5449

5550
// @ts-ignore

src/runtime/server/lib/oauth/discord.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { H3Event, H3Error } from 'h3'
1+
import type { H3Event } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
44
import { ofetch } from 'ofetch'
55
import { defu } from 'defu'
66
import { useRuntimeConfig } from '#imports'
7+
import type { OAuthConfig } from '#auth-utils'
78

89
export interface OAuthDiscordConfig {
910
/**
@@ -46,13 +47,7 @@ export interface OAuthDiscordConfig {
4647
tokenURL?: string
4748
}
4849

49-
interface OAuthConfig {
50-
config?: OAuthDiscordConfig
51-
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
52-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
53-
}
54-
55-
export function discordEventHandler({ config, onSuccess, onError }: OAuthConfig) {
50+
export function discordEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthDiscordConfig>) {
5651
return eventHandler(async (event: H3Event) => {
5752
// @ts-ignore
5853
config = defu(config, useRuntimeConfig(event).oauth?.discord, {

src/runtime/server/lib/oauth/github.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { H3Event, H3Error } from 'h3'
1+
import type { H3Event } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { ofetch } from 'ofetch'
44
import { withQuery } from 'ufo'
55
import { defu } from 'defu'
66
import { useRuntimeConfig } from '#imports'
7+
import type { OAuthConfig } from '#auth-utils'
78

89
export interface OAuthGitHubConfig {
910
/**
@@ -42,13 +43,7 @@ export interface OAuthGitHubConfig {
4243
tokenURL?: string
4344
}
4445

45-
interface OAuthConfig {
46-
config?: OAuthGitHubConfig
47-
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
48-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
49-
}
50-
51-
export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig) {
46+
export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthGitHubConfig>) {
5247
return eventHandler(async (event: H3Event) => {
5348
// @ts-ignore
5449
config = defu(config, useRuntimeConfig(event).oauth?.github, {

src/runtime/server/lib/oauth/google.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { H3Event, H3Error } from 'h3'
1+
import type { H3Event } from 'h3'
22
import {
33
eventHandler,
44
createError,
@@ -10,6 +10,7 @@ import { withQuery, parsePath } from 'ufo'
1010
import { ofetch } from 'ofetch'
1111
import { defu } from 'defu'
1212
import { useRuntimeConfig } from '#imports'
13+
import type { OAuthConfig } from '#auth-utils'
1314

1415
export interface OAuthGoogleConfig {
1516
/**
@@ -51,20 +52,11 @@ export interface OAuthGoogleConfig {
5152
redirectUrl: '/auth/google';
5253
}
5354

54-
interface OAuthConfig {
55-
config?: OAuthGoogleConfig;
56-
onSuccess: (
57-
event: H3Event,
58-
result: { user: any; tokens: any }
59-
) => Promise<void> | void;
60-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void;
61-
}
62-
6355
export function googleEventHandler({
6456
config,
6557
onSuccess,
6658
onError,
67-
}: OAuthConfig) {
59+
}: OAuthConfig<OAuthGoogleConfig>) {
6860
return eventHandler(async (event: H3Event) => {
6961
// @ts-ignore
7062
config = defu(config, useRuntimeConfig(event).oauth?.google, {

src/runtime/server/lib/oauth/spotify.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { H3Event, H3Error } from 'h3'
1+
import type { H3Event } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parsePath } from 'ufo'
44
import { ofetch } from 'ofetch'
55
import { defu } from 'defu'
66
import { useRuntimeConfig } from '#imports'
7+
import type { OAuthConfig } from '#auth-utils'
78

89
export interface OAuthSpotifyConfig {
910
/**
@@ -42,13 +43,7 @@ export interface OAuthSpotifyConfig {
4243
tokenURL?: string
4344
}
4445

45-
interface OAuthConfig {
46-
config?: OAuthSpotifyConfig
47-
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
48-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
49-
}
50-
51-
export function spotifyEventHandler({ config, onSuccess, onError }: OAuthConfig) {
46+
export function spotifyEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthSpotifyConfig>) {
5247
return eventHandler(async (event: H3Event) => {
5348
// @ts-ignore
5449
config = defu(config, useRuntimeConfig(event).oauth?.spotify, {

src/runtime/server/lib/oauth/twitch.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { H3Event, H3Error } from 'h3'
1+
import type { H3Event } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parsePath } from 'ufo'
44
import { ofetch } from 'ofetch'
55
import { defu } from 'defu'
66
import { useRuntimeConfig } from '#imports'
7+
import type { OAuthConfig } from '#auth-utils'
78

89
export interface OAuthTwitchConfig {
910
/**
@@ -45,13 +46,7 @@ export interface OAuthTwitchConfig {
4546
tokenURL?: string
4647
}
4748

48-
interface OAuthConfig {
49-
config?: OAuthTwitchConfig
50-
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
51-
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
52-
}
53-
54-
export function twitchEventHandler({ config, onSuccess, onError }: OAuthConfig) {
49+
export function twitchEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthTwitchConfig>) {
5550
return eventHandler(async (event: H3Event) => {
5651
// @ts-ignore
5752
config = defu(config, useRuntimeConfig(event).oauth?.twitch, {

0 commit comments

Comments
 (0)