Skip to content

Commit

Permalink
fix: improve GH error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Feb 17, 2024
1 parent 53c5ead commit ac9486e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/runtime/server/lib/oauth/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@ export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig<O
tokenURL: 'https://github.com/login/oauth/access_token',
authorizationParams: {}
}) as OAuthGitHubConfig
const { code } = getQuery(event)
const query = getQuery(event)

if (query.error) {
const error = createError({
statusCode: 401,
message: `GitHub login failed: ${query.error || 'Unknown error'}`,
data: query,
})
if (!onError) throw error
return onError(event, error)
}

if (!config.clientId || !config.clientSecret) {
const error = createError({
Expand All @@ -69,7 +79,7 @@ export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig<O
return onError(event, error)
}

if (!code) {
if (!query.code) {
config.scope = config.scope || []
if (config.emailRequired && !config.scope.includes('user:email')) {
config.scope.push('user:email')
Expand All @@ -94,7 +104,7 @@ export function githubEventHandler({ config, onSuccess, onError }: OAuthConfig<O
body: {
client_id: config.clientId,
client_secret: config.clientSecret,
code
code: query.code
}
}
)
Expand Down

0 comments on commit ac9486e

Please sign in to comment.