Skip to content

Commit 736b193

Browse files
committed
Fix CodeRabbit AI suggestions: GitHub ID type, OAuth scope, error handling
1 parent 7121440 commit 736b193

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

backend/models/User.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ const UserSchema = new mongoose.Schema({
1818
},
1919
// GitHub OAuth fields
2020
githubId: {
21-
type: Number,
21+
type: String,
2222
unique: true,
2323
sparse: true,
2424
},
2525
githubUsername: {
2626
type: String,
27+
unique: true,
28+
sparse: true,
2729
},
2830
avatarUrl: {
2931
type: String,

backend/routes/auth.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ router.post("/github/callback", async (req, res) => {
4343
'Accept': 'application/json',
4444
'Content-Type': 'application/json',
4545
},
46+
signal: AbortSignal.timeout(10000), // 10 second timeout
4647
body: JSON.stringify({
4748
client_id: process.env.GITHUB_CLIENT_ID,
4849
client_secret: process.env.GITHUB_CLIENT_SECRET,
@@ -51,10 +52,15 @@ router.post("/github/callback", async (req, res) => {
5152
}),
5253
});
5354

55+
if (!tokenResponse.ok) {
56+
return res.status(400).json({ message: 'Failed to authenticate with GitHub' });
57+
}
58+
5459
const tokenData = await tokenResponse.json();
5560

5661
if (tokenData.error) {
57-
return res.status(400).json({ message: 'Failed to get access token from GitHub' });
62+
console.error('GitHub token error:', tokenData.error);
63+
return res.status(400).json({ message: 'Failed to authenticate with GitHub' });
5864
}
5965

6066
const accessToken = tokenData.access_token;
@@ -65,6 +71,7 @@ router.post("/github/callback", async (req, res) => {
6571
'Authorization': `Bearer ${accessToken}`,
6672
'Accept': 'application/vnd.github.v3+json',
6773
},
74+
signal: AbortSignal.timeout(10000), // 10 second timeout
6875
});
6976

7077
const userData = await userResponse.json();
@@ -79,6 +86,7 @@ router.post("/github/callback", async (req, res) => {
7986
'Authorization': `Bearer ${accessToken}`,
8087
'Accept': 'application/vnd.github.v3+json',
8188
},
89+
signal: AbortSignal.timeout(10000), // 10 second timeout
8290
});
8391

8492
const emailsData = await emailsResponse.json();
@@ -92,7 +100,7 @@ router.post("/github/callback", async (req, res) => {
92100
user = new User({
93101
username: userData.login,
94102
email: primaryEmail,
95-
githubId: userData.id,
103+
githubId: userData.id.toString(), // Convert to string
96104
githubUsername: userData.login,
97105
avatarUrl: userData.avatar_url,
98106
// Set a random password since GitHub users don't have passwords
@@ -101,7 +109,7 @@ router.post("/github/callback", async (req, res) => {
101109
await user.save();
102110
} else {
103111
// Update existing user with GitHub info
104-
user.githubId = userData.id;
112+
user.githubId = userData.id.toString(); // Convert to string
105113
user.githubUsername = userData.login;
106114
user.avatarUrl = userData.avatar_url;
107115
await user.save();

src/pages/GitHubCallback/GitHubCallback.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ThemeContext } from "../../context/ThemeContext";
44
import type { ThemeContextType } from "../../context/ThemeContext";
55
import { FaGithub, FaCheckCircle, FaExclamationCircle } from "react-icons/fa";
66

7-
const backendUrl = import.meta.env.VITE_BACKEND_URL;
7+
const backendUrl = import.meta.env.VITE_BACKEND_URL || 'http://localhost:5000';
88

99
const GitHubCallback: React.FC = () => {
1010
const [status, setStatus] = useState<'loading' | 'success' | 'error'>('loading');
@@ -49,7 +49,7 @@ const GitHubCallback: React.FC = () => {
4949
if (response.ok) {
5050
setStatus('success');
5151
setMessage('Successfully authenticated with GitHub!');
52-
setTimeout(() => navigate('/home'), 2000);
52+
setTimeout(() => navigate('/'), 2000);
5353
} else {
5454
throw new Error(data.message || 'Authentication failed');
5555
}

src/pages/Login/Login.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, ChangeEvent, FormEvent, useContext } from "react";
22
import axios from "axios";
3-
import { useNavigate } from "react-router-dom";
3+
import { useNavigate, Link } from "react-router-dom";
44
import { ThemeContext } from "../../context/ThemeContext";
55
import type { ThemeContextType } from "../../context/ThemeContext";
66
import { FaGithub, FaEye, FaEyeSlash, FaArrowRight } from "react-icons/fa";
@@ -52,15 +52,18 @@ const Login: React.FC = () => {
5252
setMessage("");
5353

5454
try {
55-
// GitHub OAuth URL - you'll need to configure this with your GitHub OAuth app
56-
const githubClientId = import.meta.env.VITE_GITHUB_CLIENT_ID || 'your-github-client-id';
55+
const githubClientId = import.meta.env.VITE_GITHUB_CLIENT_ID;
56+
57+
if (!githubClientId) {
58+
throw new Error('GitHub Client ID not configured');
59+
}
60+
5761
const redirectUri = `${window.location.origin}/auth/github/callback`;
58-
const githubAuthUrl = `https://github.com/login/oauth/authorize?client_id=${githubClientId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=user:email,repo`;
62+
const githubAuthUrl = `https://github.com/login/oauth/authorize?client_id=${githubClientId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=user:email`;
5963

6064
window.location.href = githubAuthUrl;
6165
} catch (error: any) {
62-
setMessage("GitHub authentication failed. Please try again.");
63-
} finally {
66+
setMessage(error.message || "GitHub authentication failed. Please try again.");
6467
setIsGitHubLoading(false);
6568
}
6669
};
@@ -212,12 +215,12 @@ const Login: React.FC = () => {
212215
<div className="text-center mt-8 pb-8">
213216
<p className={`${mode === "dark" ? "text-slate-500" : "text-gray-600"} text-sm`}>
214217
Don't have an account?{" "}
215-
<a
216-
href="/signup"
218+
<Link
219+
to="/signup"
217220
className="text-purple-400 hover:text-purple-300 transition-colors duration-300 font-medium hover:underline"
218221
>
219222
Sign up here
220-
</a>
223+
</Link>
221224
</p>
222225
</div>
223226
</div>

0 commit comments

Comments
 (0)