Skip to content

Commit 9ffacf1

Browse files
committed
modified ConnectGithub.tsx and aith.github.js file to fix the redirct bug
1 parent 94f14fb commit 9ffacf1

3 files changed

Lines changed: 47 additions & 13 deletions

File tree

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect, useState } from 'react';
2+
// Added by Lorenc
3+
import { BASE, startGitHubOAuth } from '../lib/api';
24

35
/**
46
* Shows a "Connect GitHub" button if the user is not logged in.
@@ -11,33 +13,36 @@ export default function ConnectGithub() {
1113

1214
useEffect(() => {
1315
// Ask the backend if we have a session.
14-
fetch("/mcp/v1/status", { credentials: "include" })
15-
.then(r => r.json())
16-
.then(j => setSession(Boolean(j?.session)))
16+
// fetch("/mcp/v1/status", { credentials: "include" })
17+
fetch(`${BASE}/mcp/v1/status`, { credentials: 'include' })
18+
.then((r) => r.json())
19+
.then((j) => setSession(Boolean(j?.session)))
1720
.catch(() => setSession(false));
1821
}, []);
1922

2023
const startOAuth = () => {
2124
setLoading(true);
2225
// After GitHub completes, the server redirects back here.
23-
const redirectTo = encodeURIComponent(window.location.origin + "/connect");
24-
window.location.href = `/auth/github/start?redirect_to=${redirectTo}`;
26+
// const redirectTo = encodeURIComponent(window.location.origin + "/connect");
27+
// window.location.href = `/auth/github/start?redirect_to=${redirectTo}`;
28+
const redirectTo = window.location.origin + '/connect';
29+
startGitHubOAuth(redirectTo);
2530
};
2631

2732
if (session === null) return null; // or a small skeleton/spinner
2833

2934
return session ? (
3035
<span className="inline-flex items-center text-green-600 text-sm">
31-
<span className="w-2 h-2 rounded-full bg-green-600 mr-2" />
32-
✓ GitHub connected
36+
<span className="w-2 h-2 rounded-full bg-green-600 mr-2" />✓ GitHub
37+
connected
3338
</span>
3439
) : (
3540
<button
3641
onClick={startOAuth}
3742
disabled={loading}
3843
className="px-4 py-2 rounded-2xl bg-black text-white hover:opacity-90"
3944
>
40-
{loading ? "Redirecting…" : "Connect GitHub"}
45+
{loading ? 'Redirecting…' : 'Connect GitHub'}
4146
</button>
4247
);
4348
}

client/src/lib/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { usePipelineStore } from '../store/usePipelineStore';
22

3-
// This block of code was commented out from Lorenc. Connecting the GCP backed URL with the frontend
3+
// This block of code was commented out by Lorenc.
4+
// Connecting the GCP backed URL with the frontend
45

56
// export const BASE =
67
// import.meta.env.VITE_API_BASE ?? 'http://localhost:3000/api';

server/routes/auth.github.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const {
2323
const FRONTEND_URL =
2424
process.env.FRONTEND_URL || 'http://localhost:5173/connect';
2525

26+
const isProd = process.env.NODE_ENV === 'production';
27+
2628
if (!GITHUB_CLIENT_ID || !GITHUB_CLIENT_SECRET || !GITHUB_OAUTH_REDIRECT_URI) {
2729
console.warn('[WARN] Missing GitHub OAuth env vars');
2830
}
@@ -73,6 +75,21 @@ router.get('/callback', async (req, res) => {
7375
return res.status(400).send('Invalid OAuth state');
7476
}
7577

78+
// Log the redirect URI used (helps catch mismatches)
79+
// console.log(
80+
// '[OAuth callback] Using redirectUri:',
81+
// GITHUB_OAUTH_REDIRECT_URI
82+
// );
83+
84+
// Recover original redirect target (if any) from state
85+
const stateItem = consumeState(String(state));
86+
const redirectTarget =
87+
stateItem &&
88+
typeof stateItem.redirectTo === 'string' &&
89+
stateItem.redirectTo
90+
? stateItem.redirectTo
91+
: FRONTEND_URL;
92+
7693
// Log the redirect URI used (helps catch mismatches)
7794
console.log(
7895
'[OAuth callback] Using redirectUri:',
@@ -153,16 +170,27 @@ router.get('/callback', async (req, res) => {
153170
}
154171
const jwtToken = jwt.sign(jwtPayload, JWT_SECRET, { expiresIn: '10h' });
155172

173+
// res.clearCookie('oauth_state');
174+
// res.cookie('mcp_session', jwtToken, {
175+
// httpOnly: true,
176+
// sameSite: 'lax',
177+
// path: '/',
178+
// maxAge: 10 * 60 * 60 * 1000, // 10h
179+
// // secure: true, // enable on HTTPS
180+
// });
181+
182+
// return res.redirect(FRONTEND_URL);
183+
156184
res.clearCookie('oauth_state');
157185
res.cookie('mcp_session', jwtToken, {
158186
httpOnly: true,
159-
sameSite: 'lax',
187+
sameSite: isProd ? 'none' : 'lax',
188+
secure: isProd,
160189
path: '/',
161190
maxAge: 10 * 60 * 60 * 1000, // 10h
162-
// secure: true, // enable on HTTPS
163191
});
164192

165-
return res.redirect(FRONTEND_URL);
193+
return res.redirect(redirectTarget);
166194
} catch (e) {
167195
console.error('[OAuth callback] error:', e);
168196
return res.status(500).send(`OAuth failed: ${e.message}`);

0 commit comments

Comments
 (0)