From c98ea5ddf5f0c2be2548523301bae4647db9caf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Tue, 21 Jan 2025 15:59:21 +0100 Subject: [PATCH] fix: normalise errors when user not accessible --- src/runtime/server/lib/oauth/facebook.ts | 12 +++++++++--- src/runtime/server/lib/oauth/github.ts | 12 +++++++++--- src/runtime/server/lib/oauth/instagram.ts | 12 +++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/runtime/server/lib/oauth/facebook.ts b/src/runtime/server/lib/oauth/facebook.ts index 7433e3a2..2234844b 100644 --- a/src/runtime/server/lib/oauth/facebook.ts +++ b/src/runtime/server/lib/oauth/facebook.ts @@ -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 { @@ -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, { diff --git a/src/runtime/server/lib/oauth/github.ts b/src/runtime/server/lib/oauth/github.ts index 7589291e..5f84f178 100644 --- a/src/runtime/server/lib/oauth/github.ts +++ b/src/runtime/server/lib/oauth/github.ts @@ -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 { @@ -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 } diff --git a/src/runtime/server/lib/oauth/instagram.ts b/src/runtime/server/lib/oauth/instagram.ts index 21b3cffb..72000008 100644 --- a/src/runtime/server/lib/oauth/instagram.ts +++ b/src/runtime/server/lib/oauth/instagram.ts @@ -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 { @@ -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, {