mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-15 01:01:20 +10:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import sessionHandler from "~/server/internal/session";
|
|
import authManager from "~/server/internal/auth";
|
|
|
|
defineRouteMeta({
|
|
openAPI: {
|
|
tags: ["Auth"],
|
|
description: "OIDC Signin callback",
|
|
parameters: [],
|
|
},
|
|
});
|
|
|
|
export default defineEventHandler(async (h3) => {
|
|
const enabledAuthManagers = authManager.getAuthProviders();
|
|
if (!enabledAuthManagers.OpenID) return sendRedirect(h3, "/auth/signin");
|
|
|
|
const manager = enabledAuthManagers.OpenID;
|
|
|
|
const query = getQuery(h3);
|
|
const code = query.code?.toString();
|
|
if (!code)
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: "No code in query params.",
|
|
});
|
|
|
|
const state = query.state?.toString();
|
|
if (!state)
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: "No state in query params.",
|
|
});
|
|
|
|
const result = await manager.authorize(code, state);
|
|
|
|
if (typeof result === "string")
|
|
throw createError({
|
|
statusCode: 403,
|
|
message: `Failed to sign in: "${result}". Please try again.`,
|
|
});
|
|
|
|
await sessionHandler.signin(h3, result.user.id, true);
|
|
|
|
if (result.options.redirect) {
|
|
return sendRedirect(h3, result.options.redirect);
|
|
}
|
|
|
|
return sendRedirect(h3, "/");
|
|
});
|