Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Feb 4, 2025
1 parent ba4afab commit d2d32fd
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/runtime/server/lib/oauth/gitea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ export interface OAuthGiteaConfig {

/**
* URL of your Gitea instance
* @default 'http://localhost:3000'
* @default 'https://gitea.com'
*/
baseURL?: string
}

export function defineOAuthGiteaEventHandler({
config,
onSuccess,
onError
onError,
}: OAuthConfig<OAuthGiteaConfig>) {
return eventHandler(async (event: H3Event) => {
const runtimeConfig = useRuntimeConfig(event).oauth?.gitea
const baseURL = config?.baseURL ?? runtimeConfig.baseURL ?? 'http://localhost:3000'
const baseURL = config?.baseURL ?? runtimeConfig.baseURL ?? 'https://gitea.com'
config = defu(config, runtimeConfig, {
authorizationURL: `${baseURL}/login/oauth/authorize`,
tokenURL: `${baseURL}/login/oauth/access_token`,
authorizationParams: {}
authorizationParams: {},
}) as OAuthGiteaConfig

const query = getQuery<{ code?: string, error?: string }>(event)
Expand All @@ -86,7 +86,7 @@ export function defineOAuthGiteaEventHandler({
const error = createError({
statusCode: 401,
message: `Gitea login failed: ${query.error || 'Unknown error'}`,
data: query
data: query,
})
if (!onError) throw error
return onError(event, error)
Expand All @@ -97,7 +97,7 @@ export function defineOAuthGiteaEventHandler({
event,
'gitea',
['clientId', 'clientSecret'],
onError
onError,
)
}

Expand All @@ -119,8 +119,8 @@ export function defineOAuthGiteaEventHandler({
client_id: config.clientId,
redirect_uri: redirectURL,
scope: config.scope.join(' '),
...config.authorizationParams
})
...config.authorizationParams,
}),
)
}

Expand All @@ -130,8 +130,8 @@ export function defineOAuthGiteaEventHandler({
client_id: config.clientId,
client_secret: config.clientSecret,
redirect_uri: redirectURL,
code: query.code
}
code: query.code,
},
})

if (tokens.error) {
Expand All @@ -140,15 +140,16 @@ export function defineOAuthGiteaEventHandler({

const accessToken = tokens.access_token

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const user: any = await $fetch(`${baseURL}/api/v1/user`, {
headers: {
Authorization: `token ${accessToken}`
}
Authorization: `token ${accessToken}`,
},
})

return onSuccess(event, {
user,
tokens
tokens,
})
})
}

0 comments on commit d2d32fd

Please sign in to comment.