Skip to content

Commit c98ea5d

Browse files
committed
fix: normalise errors when user not accessible
1 parent 9282ecb commit c98ea5d

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

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

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

99
export interface OAuthFacebookConfig {
@@ -125,7 +125,13 @@ export function defineOAuthFacebookEventHandler({
125125
)
126126

127127
if (!user) {
128-
throw new Error('Facebook login failed: no user found')
128+
const error = createError({
129+
statusCode: 500,
130+
message: 'Could not get Facebook user',
131+
data: tokens,
132+
})
133+
if (!onError) throw error
134+
return onError(event, error)
129135
}
130136

131137
return onSuccess(event, {

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

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

99
export interface OAuthGitHubConfig {
@@ -139,7 +139,13 @@ export function defineOAuthGitHubEventHandler({ config, onSuccess, onError }: OA
139139
const primaryEmail = emails.find((email: any) => email.primary)
140140
// Still no email
141141
if (!primaryEmail) {
142-
throw new Error('GitHub login failed: no user email found')
142+
const error = createError({
143+
statusCode: 500,
144+
message: 'Could not get GitHub user email',
145+
data: tokens,
146+
})
147+
if (!onError) throw error
148+
return onError(event, error)
143149
}
144150
user.email = primaryEmail.email
145151
}

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

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

99
export interface OAuthInstagramConfig {
@@ -136,7 +136,13 @@ export function defineOAuthInstagramEventHandler({
136136
)
137137

138138
if (!user) {
139-
throw new Error('Instagram login failed: no user found')
139+
const error = createError({
140+
statusCode: 500,
141+
message: 'Could not get Instagram user',
142+
data: tokens,
143+
})
144+
if (!onError) throw error
145+
return onError(event, error)
140146
}
141147

142148
return onSuccess(event, {

0 commit comments

Comments
 (0)