add logic to fetch token from db

This commit is contained in:
CaptOrb 2025-08-28 12:01:34 +01:00
parent 3b883fe88a
commit 3a7e93611b
2 changed files with 18 additions and 9 deletions

View File

@ -8,7 +8,7 @@ import { connectDB } from "./config/db";
import { config, isProduction } from "./config/env";
import authRouter from "./routes/auth";
import { createForge } from "./services/forges";
import { findUserById } from "./types/user";
import { getAccessToken } from "./types/user";
const app = express();
app.use(express.json());
@ -44,16 +44,15 @@ api.register({
return res.status(401).json({ error: "Not authenticated" });
}
const user = await findUserById(userId);
if (!user) return res.status(401).json({ error: "User not found" });
const forge = createForge(forgeType);
if (!user.access_token) {
return res.status(401).json({ error: "Missing access token" });
const accessToken = await getAccessToken(userId);
if (!accessToken) {
return res
.status(401)
.json({ error: "Missing or expired access token" });
}
const repos = await forge.listRepositories(user.access_token);
const forge = createForge(forgeType);
const repos = await forge.listRepositories(accessToken);
return res.json(repos);
} catch (err) {

View File

@ -77,3 +77,13 @@ export const getOrCreateUser = async (
}
return user;
};
export const getAccessToken = async (
userId: number,
): Promise<string | null> => {
const result = await pool.query(
`SELECT access_token FROM users WHERE user_id = $1`,
[userId],
);
return result.rows[0]?.access_token ?? null;
};