misc: moved authenticate to preValidation

This commit is contained in:
Sheen Capadngan
2024-06-21 22:22:34 +08:00
parent 193d6dad54
commit 52fcf53d0e
+11 -5
View File
@@ -60,7 +60,8 @@ export const registerOidcRouter = async (server: FastifyZodProvider) => {
callbackPort: z.string().trim().optional()
})
},
handler: async (req, res) => {
preValidation: [
async (req, res) => {
const { orgSlug, callbackPort } = req.query;
// ensure fresh session state per login attempt
@@ -73,31 +74,36 @@ export const registerOidcRouter = async (server: FastifyZodProvider) => {
}
const oidcStrategy = await server.services.oidc.getOrgAuthStrategy(orgSlug, callbackPort);
(
return (
passport.authenticate(oidcStrategy as Strategy, {
scope: "profile email openid"
}) as any
)(req, res);
}
],
handler: () => {}
});
// callback route after login from IDP
server.route({
url: "/callback",
method: "GET",
handler: async (req, res) => {
preValidation: [
async (req, res) => {
const oidcOrgSlug = req.session.get<any>("oidcOrgSlug");
const callbackPort = req.session.get<any>("callbackPort");
const oidcStrategy = await server.services.oidc.getOrgAuthStrategy(oidcOrgSlug, callbackPort);
await (
return (
passport.authenticate(oidcStrategy as Strategy, {
failureRedirect: "/api/v1/sso/oidc/login/error",
session: false,
failureMessage: true
}) as any
)(req, res);
}
],
handler: async (req, res) => {
await req.session.destroy();
if (req.passportUser.isUserCompleted) {