mirror of
https://github.com/andwati/dokploy.git
synced 2026-10-18 19:22:56 +00:00
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { findGitlabById, updateGitlab } from "@/server/api/services/gitlab";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
const { code, gitlabId } = req.query;
|
|
|
|
if (!code || Array.isArray(code)) {
|
|
return res.status(400).json({ error: "Missing or invalid code" });
|
|
}
|
|
|
|
const gitlab = await findGitlabById(gitlabId as string);
|
|
|
|
const response = await fetch("https://gitlab.com/oauth/token", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
body: new URLSearchParams({
|
|
client_id: gitlab.applicationId as string,
|
|
client_secret: gitlab.secret as string,
|
|
code: code as string,
|
|
grant_type: "authorization_code",
|
|
redirect_uri: `${gitlab.redirectUri}?gitlabId=${gitlabId}`,
|
|
}),
|
|
});
|
|
|
|
const result = await response.json();
|
|
|
|
if (!result.access_token || !result.refresh_token) {
|
|
return res.status(400).json({ error: "Missing or invalid code" });
|
|
}
|
|
|
|
const expiresAt = Math.floor(Date.now() / 1000) + result.expires_in;
|
|
await updateGitlab(gitlab.gitlabId, {
|
|
accessToken: result.access_token,
|
|
refreshToken: result.refresh_token,
|
|
expiresAt,
|
|
});
|
|
|
|
return res.redirect(307, "/dashboard/settings/git-providers");
|
|
}
|