Skip to content

Commit

Permalink
fix: normalise errors when user not accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Jan 21, 2025
1 parent 9282ecb commit c98ea5d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/runtime/server/lib/oauth/facebook.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { H3Event } from 'h3'
import { eventHandler, getQuery, sendRedirect } from 'h3'
import { eventHandler, getQuery, sendRedirect, createError } from 'h3'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
import { useRuntimeConfig, createError } from '#imports'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthFacebookConfig {
Expand Down Expand Up @@ -125,7 +125,13 @@ export function defineOAuthFacebookEventHandler({
)

if (!user) {
throw new Error('Facebook login failed: no user found')
const error = createError({
statusCode: 500,
message: 'Could not get Facebook user',
data: tokens,
})
if (!onError) throw error
return onError(event, error)
}

return onSuccess(event, {
Expand Down
12 changes: 9 additions & 3 deletions src/runtime/server/lib/oauth/github.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { H3Event } from 'h3'
import { eventHandler, getQuery, sendRedirect } from 'h3'
import { eventHandler, getQuery, sendRedirect, createError } from 'h3'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
import { useRuntimeConfig, createError } from '#imports'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthGitHubConfig {
Expand Down Expand Up @@ -139,7 +139,13 @@ export function defineOAuthGitHubEventHandler({ config, onSuccess, onError }: OA
const primaryEmail = emails.find((email: any) => email.primary)
// Still no email
if (!primaryEmail) {
throw new Error('GitHub login failed: no user email found')
const error = createError({
statusCode: 500,
message: 'Could not get GitHub user email',
data: tokens,
})
if (!onError) throw error
return onError(event, error)
}
user.email = primaryEmail.email
}
Expand Down
12 changes: 9 additions & 3 deletions src/runtime/server/lib/oauth/instagram.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { H3Event } from 'h3'
import { eventHandler, getQuery, sendRedirect } from 'h3'
import { eventHandler, getQuery, sendRedirect, createError } from 'h3'
import { withQuery } from 'ufo'
import { defu } from 'defu'
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
import { useRuntimeConfig, createError } from '#imports'
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthInstagramConfig {
Expand Down Expand Up @@ -136,7 +136,13 @@ export function defineOAuthInstagramEventHandler({
)

if (!user) {
throw new Error('Instagram login failed: no user found')
const error = createError({
statusCode: 500,
message: 'Could not get Instagram user',
data: tokens,
})
if (!onError) throw error
return onError(event, error)
}

return onSuccess(event, {
Expand Down

0 comments on commit c98ea5d

Please sign in to comment.