MCPcopy Create free account
hub / github.com/BingyanStudio/github-analyzer / handleGithubAuth

Function handleGithubAuth

api/src/services/auth.ts:17–86  ·  view source on GitHub ↗
(req: BunRequest)

Source from the content-addressed store, hash-verified

15}
16
17export async function handleGithubAuth(req: BunRequest) {
18 console.log("Handling GitHub authentication...");
19 const params = new URLSearchParams(req.url.split("?")[1]);
20 const { code, state } = Object.fromEntries(params.entries());
21
22 // Get the auth result which includes tokens
23 const authResult = (await appOctokit.auth({
24 type: "oauth-user",
25 code: code,
26 state: state,
27 })) as Record<string, any>;
28
29 // Create user Octokit instance with the tokens
30 const userOctokit = new Octokit({
31 authStrategy: createOAuthUserAuth,
32 auth: {
33 clientId: process.env.GITHUB_CLIENT_ID,
34 clientSecret: process.env.GITHUB_CLIENT_SECRET,
35 token: authResult.token,
36 refreshToken: authResult.refreshToken,
37 expiresAt: authResult.expiresAt,
38 tokenType: authResult.tokenType,
39 },
40 });
41
42 // Get user information
43 const user = await userOctokit.rest.users.getAuthenticated();
44 const username = user.data.login;
45 console.log("Authenticated user:", username);
46
47 // Create session object
48 const sessionData: UserSession = {
49 login: username,
50 accessToken: authResult.token,
51 refreshToken: authResult.refreshToken || null,
52 expiresAt: authResult.expiresAt || null,
53 tokenType: authResult.tokenType || "bearer",
54 createdAt: new Date(),
55 };
56
57 // Generate session ID and store in Redis
58 const sessionId = crypto.randomUUID();
59 await sessionStore.saveSession(sessionId, sessionData);
60
61 // Calculate expiry time for cookie (optional, can be session cookie)
62 const cookieExpiry = authResult.expiresAt
63 ? new Date(authResult.expiresAt).toUTCString()
64 : undefined;
65
66 // Return a response with a session cookie
67 const headers = new Headers();
68 const cookieOptions = [`session=${sessionId}`, "Path=/", "SameSite=Strict"];
69
70 if (cookieExpiry) {
71 cookieOptions.push(`Expires=${cookieExpiry}`);
72 }
73
74 headers.append("Set-Cookie", cookieOptions.join("; "));

Callers

nothing calls this directly

Calls 1

saveSessionMethod · 0.80

Tested by

no test coverage detected