Skip to content

Commit

Permalink
chore(auth): dynamic provider server route
Browse files Browse the repository at this point in the history
  • Loading branch information
smarroufin committed Feb 2, 2024
1 parent 975a0fb commit b6a9715
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
22 changes: 22 additions & 0 deletions _nuxthub/server/api/auth/[...provider].get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type OAuthHandler = keyof typeof oauth

export default eventHandler(async event => {
const { provider } = await getValidatedRouterParams(event, z.object({
provider: z.string().min(1)
}).parse)

const config = getConfig()
const oauthConfig = config.oauth?.[provider]
const handlerName = `${provider}EventHandler`
if (!Object.hasOwn(oauth, handlerName)) {
throw createError({ statusCode: 400, message: 'Could not resolve this provider.' })
}

return oauth[handlerName as OAuthHandler]({
config: oauthConfig as any,
async onSuccess(event, { user }) {
await setUserSession(event, { user })
return sendRedirect(event, '/todos')
}
})(event)
})
2 changes: 1 addition & 1 deletion _nuxthub/server/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function _fetchConfig() {
return _config
}

export function useConfig() {
export function getConfig() {
if (!_config) {
throw createError('Please run _fetchConfig() in order to use useConfig()')
}
Expand Down
6 changes: 0 additions & 6 deletions server/api/auth/github.get.ts

This file was deleted.

0 comments on commit b6a9715

Please sign in to comment.