67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { COOKIE_NAME, ONE_YEAR_MS } from "@shared/const";
|
|
import type { Express, Request, Response } from "express";
|
|
import * as db from "../db";
|
|
import { getSessionCookieOptions } from "./cookies";
|
|
import { sdk } from "./sdk";
|
|
|
|
function getQueryParam(req: Request, key: string): string | undefined {
|
|
const value = req.query[key];
|
|
return typeof value === "string" ? value : undefined;
|
|
}
|
|
|
|
export function registerOAuthRoutes(app: Express) {
|
|
app.get("/api/oauth/callback", async (req: Request, res: Response) => {
|
|
const code = getQueryParam(req, "code");
|
|
const state = getQueryParam(req, "state");
|
|
|
|
if (!code || !state) {
|
|
res.status(400).json({ error: "code and state are required" });
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const tokenResponse = await sdk.exchangeCodeForToken(code, state);
|
|
const userInfo = await sdk.getUserInfo(tokenResponse.accessToken);
|
|
|
|
if (!userInfo.openId) {
|
|
res.status(400).json({ error: "openId missing from user info" });
|
|
return;
|
|
}
|
|
|
|
await db.upsertUser({
|
|
openId: userInfo.openId,
|
|
name: userInfo.name || null,
|
|
email: userInfo.email ?? null,
|
|
loginMethod: userInfo.loginMethod ?? userInfo.platform ?? null,
|
|
lastSignedIn: new Date(),
|
|
});
|
|
|
|
const sessionToken = await sdk.createSessionToken(userInfo.openId, {
|
|
name: userInfo.name || "",
|
|
expiresInMs: ONE_YEAR_MS,
|
|
});
|
|
|
|
const cookieOptions = getSessionCookieOptions(req);
|
|
res.cookie(COOKIE_NAME, sessionToken, { ...cookieOptions, maxAge: ONE_YEAR_MS });
|
|
|
|
// Décoder le state pour obtenir l'URL de redirection
|
|
let redirectUrl = "/";
|
|
try {
|
|
redirectUrl = Buffer.from(state, 'base64').toString('utf-8');
|
|
// Si c'est une URL complète, extraire seulement le path
|
|
if (redirectUrl.startsWith('http')) {
|
|
const url = new URL(redirectUrl);
|
|
redirectUrl = url.pathname;
|
|
}
|
|
} catch (error) {
|
|
console.warn("[OAuth] Failed to decode state, redirecting to /", error);
|
|
}
|
|
|
|
res.redirect(302, redirectUrl);
|
|
} catch (error) {
|
|
console.error("[OAuth] Callback failed", error);
|
|
res.status(500).json({ error: "OAuth callback failed" });
|
|
}
|
|
});
|
|
}
|