Skip to content

Commit a1b3fbb

Browse files
Barbapapazesatinux
andauthored
refactor: handle access token error response
* refactor: handle access token error response * chore: add missing import * fix lint --------- Co-authored-by: Sébastien Chopin <seb@nuxt.com>
1 parent 7e81c27 commit a1b3fbb

File tree

19 files changed

+68
-150
lines changed

19 files changed

+68
-150
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { H3Event } from 'h3'
2-
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
2+
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parsePath } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration } from '../utils'
5+
import { handleMissingConfiguration, handleAccessTokenErrorResponse } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77
import type { OAuthConfig } from '#auth-utils'
88

@@ -120,14 +120,9 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }: OAuthConf
120120
).catch((error) => {
121121
return { error }
122122
})
123+
123124
if (tokens.error) {
124-
const error = createError({
125-
statusCode: 401,
126-
message: `Auth0 login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
127-
data: tokens,
128-
})
129-
if (!onError) throw error
130-
return onError(event, error)
125+
return handleAccessTokenErrorResponse(event, 'auth0', tokens, onError)
131126
}
132127

133128
const tokenType = tokens.token_type

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { H3Event } from 'h3'
33
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
44
import { withQuery, parsePath } from 'ufo'
55
import { defu } from 'defu'
6-
import { handleMissingConfiguration } from '../utils'
6+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
77
import { useRuntimeConfig } from '#imports'
88
import type { OAuthConfig } from '#auth-utils'
99

@@ -133,13 +133,7 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }: OA
133133
})
134134

135135
if (tokens.error) {
136-
const error = createError({
137-
statusCode: 401,
138-
message: `Battle.net login failed: ${tokens.error || 'Unknown error'}`,
139-
data: tokens,
140-
})
141-
if (!onError) throw error
142-
return onError(event, error)
136+
return handleAccessTokenErrorResponse(event, 'battle.net', tokens, onError)
143137
}
144138

145139
const accessToken = tokens.access_token

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { H3Event } from 'h3'
2-
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
2+
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parsePath } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration } from '../utils'
5+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77
import type { OAuthConfig } from '#auth-utils'
88

@@ -97,13 +97,7 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }: OAuthCo
9797
})
9898

9999
if (tokens.error) {
100-
const error = createError({
101-
statusCode: 401,
102-
message: `Cognito login failed: ${tokens.error_description || 'Unknown error'}`,
103-
data: tokens,
104-
})
105-
if (!onError) throw error
106-
return onError(event, error)
100+
return handleAccessTokenErrorResponse(event, 'cognito', tokens, onError)
107101
}
108102

109103
const tokenType = tokens.token_type

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { H3Event } from 'h3'
2-
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
2+
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration } from '../utils'
5+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77
import type { OAuthConfig } from '#auth-utils'
88

@@ -118,15 +118,9 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }: OAuthCo
118118
).catch((error) => {
119119
return { error }
120120
})
121-
if (tokens.error) {
122-
const error = createError({
123-
statusCode: 401,
124-
message: `Discord login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
125-
data: tokens,
126-
})
127121

128-
if (!onError) throw error
129-
return onError(event, error)
122+
if (tokens.error) {
123+
return handleAccessTokenErrorResponse(event, 'discord', tokens, onError)
130124
}
131125

132126
const accessToken = tokens.access_token

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from 'h3'
99
import { withQuery } from 'ufo'
1010
import { defu } from 'defu'
11-
import { handleMissingConfiguration } from '../utils'
11+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
1212
import { useRuntimeConfig } from '#imports'
1313
import type { OAuthConfig } from '#auth-utils'
1414

@@ -115,14 +115,9 @@ export function oauthFacebookEventHandler({
115115
code: query.code,
116116
},
117117
})
118+
118119
if (tokens.error) {
119-
const error = createError({
120-
statusCode: 401,
121-
message: `Facebook login failed: ${tokens.error || 'Unknown error'}`,
122-
data: tokens,
123-
})
124-
if (!onError) throw error
125-
return onError(event, error)
120+
return handleAccessTokenErrorResponse(event, 'facebook', tokens, onError)
126121
}
127122

128123
const accessToken = tokens.access_token

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { H3Event } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration } from '../utils'
5+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77
import type { OAuthConfig } from '#auth-utils'
88

@@ -111,14 +111,9 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }: OAuthCon
111111
},
112112
},
113113
)
114+
114115
if (tokens.error) {
115-
const error = createError({
116-
statusCode: 401,
117-
message: `GitHub login failed: ${tokens.error || 'Unknown error'}`,
118-
data: tokens,
119-
})
120-
if (!onError) throw error
121-
return onError(event, error)
116+
return handleAccessTokenErrorResponse(event, 'github', tokens, onError)
122117
}
123118

124119
const accessToken = tokens.access_token

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import type { H3Event } from 'h3'
22
import {
33
eventHandler,
4-
createError,
54
getQuery,
65
getRequestURL,
76
sendRedirect,
87
} from 'h3'
98
import { withQuery, parsePath } from 'ufo'
109
import { defu } from 'defu'
11-
import { handleMissingConfiguration } from '../utils'
10+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
1211
import { useRuntimeConfig } from '#imports'
1312
import type { OAuthConfig } from '#auth-utils'
1413

@@ -117,15 +116,7 @@ export function oauthGoogleEventHandler({
117116
return { error }
118117
})
119118
if (tokens.error) {
120-
const error = createError({
121-
statusCode: 401,
122-
message: `Google login failed: ${
123-
tokens.error?.data?.error_description || 'Unknown error'
124-
}`,
125-
data: tokens,
126-
})
127-
if (!onError) throw error
128-
return onError(event, error)
119+
return handleAccessTokenErrorResponse(event, 'google', tokens, onError)
129120
}
130121

131122
const accessToken = tokens.access_token

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from 'h3'
99
import { withQuery, parsePath } from 'ufo'
1010
import { defu } from 'defu'
11-
import { handleMissingConfiguration } from '../utils'
11+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
1212
import { useRuntimeConfig } from '#imports'
1313
import type { OAuthConfig } from '#auth-utils'
1414

@@ -130,15 +130,7 @@ export function oauthKeycloakEventHandler({
130130
})
131131

132132
if (tokens.error) {
133-
const error = createError({
134-
statusCode: 401,
135-
message: `Keycloak login failed: ${
136-
tokens.error?.data?.error_description || 'Unknown error'
137-
}`,
138-
data: tokens,
139-
})
140-
if (!onError) throw error
141-
return onError(event, error)
133+
return handleAccessTokenErrorResponse(event, 'keycloak', tokens, onError)
142134
}
143135

144136
const accessToken = tokens.access_token

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { H3Event, H3Error } from 'h3'
2-
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
2+
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration } from '../utils'
5+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77

88
export interface OAuthLinkedInConfig {
@@ -115,13 +115,7 @@ export function oauthLinkedInEventHandler({ config, onSuccess, onError }: OAuthC
115115
return { error }
116116
})
117117
if (tokens.error) {
118-
const error = createError({
119-
statusCode: 401,
120-
message: `LinkedIn login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
121-
data: tokens,
122-
})
123-
if (!onError) throw error
124-
return onError(event, error)
118+
return handleAccessTokenErrorResponse(event, 'linkedin', tokens, onError)
125119
}
126120

127121
const accessToken = tokens.access_token

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { H3Event, H3Error } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parsePath } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration } from '../utils'
5+
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77

88
export interface OAuthMicrosoftConfig {
@@ -117,13 +117,7 @@ export function oauthMicrosoftEventHandler({ config, onSuccess, onError }: OAuth
117117
return { error }
118118
})
119119
if (tokens.error) {
120-
const error = createError({
121-
statusCode: 401,
122-
message: `Microsoft login failed: ${tokens.error?.data?.error_description || 'Unknown error'}`,
123-
data: tokens,
124-
})
125-
if (!onError) throw error
126-
return onError(event, error)
120+
return handleAccessTokenErrorResponse(event, 'microsoft', tokens, onError)
127121
}
128122

129123
const tokenType = tokens.token_type

0 commit comments

Comments
 (0)