import { setSessionCookie } from "../sessionCookies.js";
import { validateSession } from "../userAuth.js";
function isDevLoginEnabled() {
    return process.env.CROSS_SEED_DEV_LOGIN === "true";
}
function normalizeRedirectTo(redirectTo) {
    if (typeof redirectTo !== "string" ||
        !redirectTo.startsWith("/") ||
        redirectTo.startsWith("//")) {
        return "/";
    }
    return redirectTo;
}
export async function devLoginPlugin(app, { basePath }) {
    if (!isDevLoginEnabled())
        return;
    app.get("/api/dev-login/:sessionId", async (request, reply) => {
        const sessionId = request.params.sessionId;
        const user = await validateSession(sessionId);
        if (!user) {
            return reply
                .code(404)
                .send({ error: "Dev login session not found" });
        }
        setSessionCookie(reply, sessionId);
        return reply.redirect(`${basePath}${normalizeRedirectTo(request.query.redirectTo)}`);
    });
}
//# sourceMappingURL=devLogin.js.map