Skip to content

Commit

Permalink
chore:
Browse files Browse the repository at this point in the history
  • Loading branch information
이은상 committed Dec 26, 2024
1 parent 8f9ca0e commit c015d87
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
8 changes: 5 additions & 3 deletions 7_auth/7_1_express_js/src/controllers/GitHubController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 });
Expand Down
6 changes: 5 additions & 1 deletion 7_auth/client/Context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}, []);

Expand Down
2 changes: 2 additions & 0 deletions 7_auth/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c015d87

Please sign in to comment.