Skip to content

Commit

Permalink
refactor: handle access token error response
Browse files Browse the repository at this point in the history
* refactor: handle access token  error response

* chore: add missing import

* fix lint

---------

Co-authored-by: Sébastien Chopin <seb@nuxt.com>
  • Loading branch information
Barbapapazes and atinux committed Aug 28, 2024
1 parent 7e81c27 commit a1b3fbb
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 150 deletions.
13 changes: 4 additions & 9 deletions src/runtime/server/lib/oauth/auth0.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleMissingConfiguration, handleAccessTokenErrorResponse } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -120,14 +120,9 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }: OAuthConf
).catch((error) => {
return { error }
})

if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Auth0 login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'auth0', tokens, onError)
}

const tokenType = tokens.token_type
Expand Down
10 changes: 2 additions & 8 deletions src/runtime/server/lib/oauth/battledotnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -133,13 +133,7 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }: OA
})

if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Battle.net login failed: ${tokens.error || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'battle.net', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
12 changes: 3 additions & 9 deletions src/runtime/server/lib/oauth/cognito.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -97,13 +97,7 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }: OAuthCo
})

if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Cognito login failed: ${tokens.error_description || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'cognito', tokens, onError)
}

const tokenType = tokens.token_type
Expand Down
14 changes: 4 additions & 10 deletions src/runtime/server/lib/oauth/discord.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -118,15 +118,9 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }: OAuthCo
).catch((error) => {
return { error }
})
if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Discord login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
data: tokens,
})

if (!onError) throw error
return onError(event, error)
if (tokens.error) {
return handleAccessTokenErrorResponse(event, 'discord', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
11 changes: 3 additions & 8 deletions src/runtime/server/lib/oauth/facebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'h3'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -115,14 +115,9 @@ export function oauthFacebookEventHandler({
code: query.code,
},
})

if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Facebook login failed: ${tokens.error || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'facebook', tokens, onError)
}

const accessToken = tokens.access_token
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
Expand Up @@ -2,7 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -111,14 +111,9 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }: OAuthCon
},
},
)

if (tokens.error) {
const error = createError({
statusCode: 401,
message: `GitHub login failed: ${tokens.error || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'github', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
13 changes: 2 additions & 11 deletions src/runtime/server/lib/oauth/google.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import type { H3Event } from 'h3'
import {
eventHandler,
createError,
getQuery,
getRequestURL,
sendRedirect,
} from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -117,15 +116,7 @@ export function oauthGoogleEventHandler({
return { error }
})
if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Google login failed: ${
tokens.error?.data?.error_description || 'Unknown error'
}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'google', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
12 changes: 2 additions & 10 deletions src/runtime/server/lib/oauth/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -130,15 +130,7 @@ export function oauthKeycloakEventHandler({
})

if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Keycloak login failed: ${
tokens.error?.data?.error_description || 'Unknown error'
}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'keycloak', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
12 changes: 3 additions & 9 deletions src/runtime/server/lib/oauth/linkedin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { H3Event, H3Error } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'

export interface OAuthLinkedInConfig {
Expand Down Expand Up @@ -115,13 +115,7 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }: OAuthC
return { error }
})
if (tokens.error) {
const error = createError({
statusCode: 401,
message: `LinkedIn login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'linkedin', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
10 changes: 2 additions & 8 deletions src/runtime/server/lib/oauth/microsoft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { H3Event, H3Error } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'

export interface OAuthMicrosoftConfig {
Expand Down Expand Up @@ -117,13 +117,7 @@ export function oauthMicrosoftEventHandler({ config, onSuccess, onError }: OAuth
return { error }
})
if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Microsoft login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'microsoft', tokens, onError)
}

const tokenType = tokens.token_type
Expand Down
10 changes: 2 additions & 8 deletions src/runtime/server/lib/oauth/paypal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -132,13 +132,7 @@ export function oauthPaypalEventHandler({ config, onSuccess, onError }: OAuthCon
})

if (tokens.error) {
const error = createError({
statusCode: 401,
message: `PayPal login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'paypal', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
12 changes: 3 additions & 9 deletions src/runtime/server/lib/oauth/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -108,13 +108,7 @@ export function oauthSpotifyEventHandler({ config, onSuccess, onError }: OAuthCo
return { error }
})
if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Spotify login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'spotify', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
10 changes: 2 additions & 8 deletions src/runtime/server/lib/oauth/twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -111,13 +111,7 @@ export function oauthTwitchEventHandler({ config, onSuccess, onError }: OAuthCon
return { error }
})
if (tokens.error) {
const error = createError({
statusCode: 401,
message: `Twitch login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'twitch', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
12 changes: 2 additions & 10 deletions src/runtime/server/lib/oauth/x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -124,15 +124,7 @@ export function oauthXEventHandler({
return { error }
})
if (tokens.error) {
const error = createError({
statusCode: 401,
message: `X login failed: ${
tokens.error?.data?.error_description || 'Unknown error'
}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'x', tokens, onError)
}

const accessToken = tokens.access_token
Expand Down
12 changes: 3 additions & 9 deletions src/runtime/server/lib/oauth/xsuaa.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { H3Event } from 'h3'
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
import { withQuery, parsePath } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration } from '../utils'
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

Expand Down Expand Up @@ -83,13 +83,7 @@ export function oauthXSUAAEventHandler({ config, onSuccess, onError }: OAuthConf
return { error }
})
if (tokens.error) {
const error = createError({
statusCode: 401,
message: `XSUAA login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
data: tokens,
})
if (!onError) throw error
return onError(event, error)
return handleAccessTokenErrorResponse(event, 'auth0', tokens, onError)
}

const tokenType = tokens.token_type
Expand Down
Loading

0 comments on commit a1b3fbb

Please sign in to comment.