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

fix(oauth): add generic OAuthConfig type #18

Merged
merged 6 commits into from
Nov 20, 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ 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:
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:

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

### OAuth Event Handlers
Expand Down
2 changes: 2 additions & 0 deletions playground/auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ declare module '#auth-utils' {
loggedInAt: number
}
}

export {}
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default defineNuxtModule<ModuleOptions>({
console.log(`NUXT_SESSION_PASSWORD=${randomPassword}`)
}

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

// App
addImportsDir(resolver.resolve('./runtime/composables'))
Expand Down
11 changes: 3 additions & 8 deletions src/runtime/server/lib/oauth/auth0.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { H3Event, H3Error } from 'h3'
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthAuth0Config {
/**
Expand Down Expand Up @@ -40,13 +41,7 @@ export interface OAuthAuth0Config {
emailRequired?: boolean
}

interface OAuthConfig {
config?: OAuthAuth0Config
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
}

export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig) {
export function auth0EventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthAuth0Config>) {
return eventHandler(async (event: H3Event) => {
// @ts-ignore
config = defu(config, useRuntimeConfig(event).oauth?.auth0) as OAuthAuth0Config
Expand Down
11 changes: 3 additions & 8 deletions src/runtime/server/lib/oauth/battledotnet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { H3Event, H3Error } from 'h3'
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { ofetch } from 'ofetch'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import { randomUUID } from 'crypto'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthBattledotnetConfig {
/**
Expand Down Expand Up @@ -43,13 +44,7 @@ export interface OAuthBattledotnetConfig {
tokenURL?: string
}

interface OAuthConfig {
config?: OAuthBattledotnetConfig
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
}

export function battledotnetEventHandler({ config, onSuccess, onError }: OAuthConfig) {
export function battledotnetEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthBattledotnetConfig>) {
return eventHandler(async (event: H3Event) => {

// @ts-ignore
Expand Down
11 changes: 3 additions & 8 deletions src/runtime/server/lib/oauth/discord.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { H3Event, H3Error } from 'h3'
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthDiscordConfig {
/**
Expand Down Expand Up @@ -46,13 +47,7 @@ export interface OAuthDiscordConfig {
tokenURL?: string
}

interface OAuthConfig {
config?: OAuthDiscordConfig
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
}

export function discordEventHandler({ config, onSuccess, onError }: OAuthConfig) {
export function discordEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthDiscordConfig>) {
return eventHandler(async (event: H3Event) => {
// @ts-ignore
config = defu(config, useRuntimeConfig(event).oauth?.discord, {
Expand Down
11 changes: 3 additions & 8 deletions src/runtime/server/lib/oauth/github.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { H3Event, H3Error } from 'h3'
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { ofetch } from 'ofetch'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthGitHubConfig {
/**
Expand Down Expand Up @@ -42,13 +43,7 @@ export interface OAuthGitHubConfig {
tokenURL?: string
}

interface OAuthConfig {
config?: OAuthGitHubConfig
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
}

export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig) {
export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthGitHubConfig>) {
return eventHandler(async (event: H3Event) => {
// @ts-ignore
config = defu(config, useRuntimeConfig(event).oauth?.github, {
Expand Down
14 changes: 3 additions & 11 deletions src/runtime/server/lib/oauth/google.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { H3Event, H3Error } from 'h3'
import type { H3Event } from 'h3'
import {
eventHandler,
createError,
Expand All @@ -10,6 +10,7 @@ import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthGoogleConfig {
/**
Expand Down Expand Up @@ -51,20 +52,11 @@ export interface OAuthGoogleConfig {
redirectUrl: '/auth/google';
}

interface OAuthConfig {
config?: OAuthGoogleConfig;
onSuccess: (
event: H3Event,
result: { user: any; tokens: any }
) => Promise<void> | void;
onError?: (event: H3Event, error: H3Error) => Promise<void> | void;
}

export function googleEventHandler({
config,
onSuccess,
onError,
}: OAuthConfig) {
}: OAuthConfig<OAuthGoogleConfig>) {
return eventHandler(async (event: H3Event) => {
// @ts-ignore
config = defu(config, useRuntimeConfig(event).oauth?.google, {
Expand Down
11 changes: 3 additions & 8 deletions src/runtime/server/lib/oauth/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { H3Event, H3Error } from 'h3'
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthSpotifyConfig {
/**
Expand Down Expand Up @@ -42,13 +43,7 @@ export interface OAuthSpotifyConfig {
tokenURL?: string
}

interface OAuthConfig {
config?: OAuthSpotifyConfig
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
}

export function spotifyEventHandler({ config, onSuccess, onError }: OAuthConfig) {
export function spotifyEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthSpotifyConfig>) {
return eventHandler(async (event: H3Event) => {
// @ts-ignore
config = defu(config, useRuntimeConfig(event).oauth?.spotify, {
Expand Down
11 changes: 3 additions & 8 deletions src/runtime/server/lib/oauth/twitch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { H3Event, H3Error } from 'h3'
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { ofetch } from 'ofetch'
import { defu } from 'defu'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthTwitchConfig {
/**
Expand Down Expand Up @@ -45,13 +46,7 @@ export interface OAuthTwitchConfig {
tokenURL?: string
}

interface OAuthConfig {
config?: OAuthTwitchConfig
onSuccess: (event: H3Event, result: { user: any, tokens: any }) => Promise<void> | void
onError?: (event: H3Event, error: H3Error) => Promise<void> | void
}

export function twitchEventHandler({ config, onSuccess, onError }: OAuthConfig) {
export function twitchEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthTwitchConfig>) {
return eventHandler(async (event: H3Event) => {
// @ts-ignore
config = defu(config, useRuntimeConfig(event).oauth?.twitch, {
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { UserSession } from './session'
export type { OAuthConfig } from './oauth-config'
11 changes: 11 additions & 0 deletions src/runtime/types/oauth-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { H3Event, H3Error } from 'h3'
import type { UserSession } from '#auth-utils'

export interface OAuthConfig<TConfig, TUser = UserSession, TTokens = any> {
config?: TConfig;
onSuccess: (
event: H3Event,
result: { user: TUser; tokens: TTokens }
) => Promise<void> | void;
onError?: (event: H3Event, error: H3Error) => Promise<void> | void;
}
File renamed without changes.