Skip to content

Commit b6a9715

Browse files
committed
chore(auth): dynamic provider server route
1 parent 975a0fb commit b6a9715

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
type OAuthHandler = keyof typeof oauth
2+
3+
export default eventHandler(async event => {
4+
const { provider } = await getValidatedRouterParams(event, z.object({
5+
provider: z.string().min(1)
6+
}).parse)
7+
8+
const config = getConfig()
9+
const oauthConfig = config.oauth?.[provider]
10+
const handlerName = `${provider}EventHandler`
11+
if (!Object.hasOwn(oauth, handlerName)) {
12+
throw createError({ statusCode: 400, message: 'Could not resolve this provider.' })
13+
}
14+
15+
return oauth[handlerName as OAuthHandler]({
16+
config: oauthConfig as any,
17+
async onSuccess(event, { user }) {
18+
await setUserSession(event, { user })
19+
return sendRedirect(event, '/todos')
20+
}
21+
})(event)
22+
})

_nuxthub/server/utils/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function _fetchConfig() {
2424
return _config
2525
}
2626

27-
export function useConfig() {
27+
export function getConfig() {
2828
if (!_config) {
2929
throw createError('Please run _fetchConfig() in order to use useConfig()')
3030
}

server/api/auth/github.get.ts

-6
This file was deleted.

0 commit comments

Comments
 (0)