in app/lib/server/auth.ts [21:97]
export function extractCredentialsFromCookie(
cookieHeader: string | null
): ApiCredentials {
if (!cookieHeader) {
return {};
}
try {
// Parse cookies
const cookies = parseCookies(cookieHeader);
// Get the encoded cookie name
const encodedCookieName = encodeURIComponent(serverConfig.COOKIE_NAME);
const authCookie = cookies[encodedCookieName];
if (!authCookie) {
return {};
}
// console.log("Auth cookie found. Attempting to decode...");
// Decode the URL encoded cookie value first
const decodedCookie = decodeURIComponent(authCookie);
// Then decode the base64 and parse the JSON
const cookieData = JSON.parse(
Buffer.from(decodedCookie, "base64").toString()
);
const credentials: ApiCredentials = {};
if (cookieData.enc) {
// Decode the credentials inside the cookie
const creds = JSON.parse(
Buffer.from(cookieData.enc, "base64").toString()
);
credentials.huggingfaceToken = creds.hf;
credentials.githubToken = creds.gh;
// OpenAI is no longer stored in cookies
}
// Extract user info if available
if (cookieData.hfUserInfo) {
try {
const userInfo = JSON.parse(
Buffer.from(cookieData.hfUserInfo, "base64").toString()
);
credentials.hfUserInfo = userInfo;
// console.log(
// "Successfully extracted HF user info from cookie:",
// userInfo.username
// );
} catch (err) {
console.error("Error parsing HF user info:", err);
}
}
// Extract GitHub user info if available
if (cookieData.githubUserInfo) {
try {
const githubUserInfo = JSON.parse(
Buffer.from(cookieData.githubUserInfo, "base64").toString()
);
credentials.githubUserInfo = githubUserInfo;
} catch (err) {
console.error("Error parsing GitHub user info:", err);
}
}
// console.log("Successfully extracted API credentials from cookie");
return credentials;
} catch (error) {
console.error("Failed to parse auth cookie:", error);
return {};
}
}