diff --git a/7_auth/7_1_express_js/src/controllers/GitHubController.ts b/7_auth/7_1_express_js/src/controllers/GitHubController.ts index a0a18da..8e052ae 100644 --- a/7_auth/7_1_express_js/src/controllers/GitHubController.ts +++ b/7_auth/7_1_express_js/src/controllers/GitHubController.ts @@ -12,6 +12,8 @@ const OAUTH_URL = "https://github.com/login/oauth/authorize"; const OAUTH_ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"; const REDIS_HOST = process.env.REDIS_HOST!; +const FRONTEND_HOST = process.env.FRONTEND_HOST!; +const FRONTEND_PORT = process.env.FRONTEND_PORT!; // docker run -d -p 6379:6379 --name redis redis // Create a new Redis client @@ -69,12 +71,12 @@ export class GithubController { const userResponse = await axios.get(`${GITHUB_API_URL}/user`, { headers: { Authorization: `Bearer ${access_token}` }, }); - const userInfo = userResponse.data; - // Store the access token in Redis (or a session store) - await redis.set(`user:${userInfo.login}:access_token`, access_token, 'EX', 3600); + const userInfo = userResponse.data; + await redis.set(`user:${userInfo.login}:access_token`, access_token); res.cookie('is_authenticated', 'true', { secure: true, maxAge: 3600_000 }); // 1 hour + res.redirect(`${FRONTEND_HOST}:${FRONTEND_PORT}/auth/callback`); } catch (err: unknown) { if (err instanceof Error) { res.status(500).send({ error: "GitHub OAuth2 callback failed", details: err.message }); diff --git a/7_auth/client/Context.jsx b/7_auth/client/Context.jsx index e30db53..479e620 100644 --- a/7_auth/client/Context.jsx +++ b/7_auth/client/Context.jsx @@ -16,7 +16,11 @@ export const AuthProvider = ({ children }) => { if (isAuthenticatedCookie === 'true' && !isAuthenticated) { setIsAuthenticated(true); - window.location.reload(); + + // Redirect after successful authentication + if (history.location.pathname === '/auth/callback') { + history.push('/'); // Or whatever protected page you want to navigate to + } } }, []); diff --git a/7_auth/docker/docker-compose.yml b/7_auth/docker/docker-compose.yml index 8c99cce..06d6228 100644 --- a/7_auth/docker/docker-compose.yml +++ b/7_auth/docker/docker-compose.yml @@ -6,6 +6,8 @@ services: container_name: express-backend environment: REDIS_HOST: redis + FRONTEND_HOST: vite-react-client + FRONTEND_PORT: 4173 ports: - "3000:3000" depends_on: