Skip to content

Commit 5675aaf

Browse files
authored
refactor: handle missing configuration error
1 parent 4ad11a4 commit 5675aaf

File tree

22 files changed

+193
-160
lines changed

22 files changed

+193
-160
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"ofetch": "^1.3.4",
3939
"ohash": "^1.1.3",
4040
"pathe": "^1.1.2",
41+
"scule": "^1.3.0",
4142
"uncrypto": "^0.1.3"
4243
},
4344
"devDependencies": {

pnpm-lock.yaml

Lines changed: 135 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { H3Event } 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'
56
import { useRuntimeConfig } from '#imports'
67
import type { OAuthConfig } from '#auth-utils'
78

@@ -72,12 +73,7 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }: OAuthConf
7273
const { code } = getQuery(event)
7374

7475
if (!config.clientId || !config.clientSecret || !config.domain) {
75-
const error = createError({
76-
statusCode: 500,
77-
message: 'Missing NUXT_OAUTH_AUTH0_CLIENT_ID or NUXT_OAUTH_AUTH0_CLIENT_SECRET or NUXT_OAUTH_AUTH0_DOMAIN env variables.',
78-
})
79-
if (!onError) throw error
80-
return onError(event, error)
76+
return handleMissingConfiguration(event, 'auth0', ['clientId', 'clientSecret', 'domain'], onError)
8177
}
8278
const authorizationURL = `https://${config.domain}/authorize`
8379
const tokenURL = `https://${config.domain}/oauth/token`

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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'
67
import { useRuntimeConfig } from '#imports'
78
import type { OAuthConfig } from '#auth-utils'
89

@@ -75,12 +76,8 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }: OA
7576
}
7677

7778
if (!config.clientId || !config.clientSecret) {
78-
const error = createError({
79-
statusCode: 500,
80-
message: 'Missing NUXT_OAUTH_BATTLEDOTNET_CLIENT_ID or NUXT_OAUTH_BATTLEDOTNET_CLIENT_SECRET env variables.',
81-
})
82-
if (!onError) throw error
83-
return onError(event, error)
79+
return handleMissingConfiguration(event, 'battledotnet', ['clientId', 'clientSecret'], onError,
80+
)
8481
}
8582

8683
const redirectURL = config.redirectURL || getRequestURL(event).href

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { H3Event } 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'
56
import { useRuntimeConfig } from '#imports'
67
import type { OAuthConfig } from '#auth-utils'
78

@@ -56,12 +57,7 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }: OAuthCo
5657
const { code } = getQuery(event)
5758

5859
if (!config.clientId || !config.clientSecret || !config.userPoolId || !config.region) {
59-
const error = createError({
60-
statusCode: 500,
61-
message: 'Missing NUXT_OAUTH_COGNITO_CLIENT_ID, NUXT_OAUTH_COGNITO_CLIENT_SECRET, NUXT_OAUTH_COGNITO_USER_POOL_ID, or NUXT_OAUTH_COGNITO_REGION env variables.',
62-
})
63-
if (!onError) throw error
64-
return onError(event, error)
60+
return handleMissingConfiguration(event, 'cognito', ['clientId', 'clientSecret', 'userPoolId', 'region'], onError)
6561
}
6662

6763
const urlBase = config?.domain || `${config.userPoolId}.auth.${config.region}.amazoncognito.com`

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { H3Event } from 'h3'
22
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
33
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
44
import { defu } from 'defu'
5+
import { handleMissingConfiguration } from '../utils'
56
import { useRuntimeConfig } from '#imports'
67
import type { OAuthConfig } from '#auth-utils'
78

@@ -69,12 +70,7 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }: OAuthCo
6970
const { code } = getQuery(event)
7071

7172
if (!config.clientId || !config.clientSecret) {
72-
const error = createError({
73-
statusCode: 500,
74-
message: 'Missing NUXT_OAUTH_DISCORD_CLIENT_ID or NUXT_OAUTH_DISCORD_CLIENT_SECRET env variables.',
75-
})
76-
if (!onError) throw error
77-
return onError(event, error)
73+
return handleMissingConfiguration(event, 'discord', ['clientId', 'clientSecret'], onError)
7874
}
7975

8076
const redirectURL = config.redirectURL || getRequestURL(event).href

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

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

@@ -86,13 +87,7 @@ export function oauthFacebookEventHandler({
8687
}
8788

8889
if (!config.clientId) {
89-
const error = createError({
90-
statusCode: 500,
91-
message:
92-
'Missing NUXT_OAUTH_FACEBOOK_CLIENT_ID or NUXT_OAUTH_FACEBOOK_CLIENT_SECRET env variables.',
93-
})
94-
if (!onError) throw error
95-
return onError(event, error)
90+
return handleMissingConfiguration(event, 'facebook', ['clientId'], onError)
9691
}
9792

9893
const redirectURL = config.redirectURL || getRequestURL(event).href

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +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'
56
import { useRuntimeConfig } from '#imports'
67
import type { OAuthConfig } from '#auth-utils'
78

@@ -76,12 +77,7 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }: OAuthCon
7677
}
7778

7879
if (!config.clientId || !config.clientSecret) {
79-
const error = createError({
80-
statusCode: 500,
81-
message: 'Missing NUXT_OAUTH_GITHUB_CLIENT_ID or NUXT_OAUTH_GITHUB_CLIENT_SECRET env variables.',
82-
})
83-
if (!onError) throw error
84-
return onError(event, error)
80+
return handleMissingConfiguration(event, 'github', ['clientId', 'clientSecret'], onError)
8581
}
8682

8783
if (!query.code) {

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

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

@@ -79,12 +80,7 @@ export function oauthGoogleEventHandler({
7980
const { code } = getQuery(event)
8081

8182
if (!config.clientId) {
82-
const error = createError({
83-
statusCode: 500,
84-
message: 'Missing NUXT_OAUTH_GOOGLE_CLIENT_ID env variables.',
85-
})
86-
if (!onError) throw error
87-
return onError(event, error)
83+
return handleMissingConfiguration(event, 'google', ['clientId'], onError)
8884
}
8985

9086
const redirectURL = config.redirectURL || getRequestURL(event).href

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

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

@@ -80,13 +81,7 @@ export function oauthKeycloakEventHandler({
8081
|| !config.serverUrl
8182
|| !config.realm
8283
) {
83-
const error = createError({
84-
statusCode: 500,
85-
message:
86-
'Missing NUXT_OAUTH_KEYCLOAK_CLIENT_ID or NUXT_OAUTH_KEYCLOAK_CLIENT_SECRET or NUXT_OAUTH_KEYCLOAK_SERVER_URL or NUXT_OAUTH_KEYCLOAK_REALM env variables.',
87-
})
88-
if (!onError) throw error
89-
return onError(event, error)
84+
return handleMissingConfiguration(event, 'keycloak', ['clientId', 'clientSecret', 'serverUrl', 'realm'], onError)
9085
}
9186

9287
const realmURL = `${config.serverUrl}/realms/${config.realm}`

0 commit comments

Comments
 (0)