Skip to content

Commit 50d82b9

Browse files
committed
added choose between github oauth or without
1 parent 7e17c64 commit 50d82b9

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

nextstep-backend/src/controllers/github_controller.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ export const handleGitHubOAuth = async (req: Request, res: Response) => {
4646

4747
export const fetchGitHubRepos = async (req: Request, res: Response) => {
4848
const { username } = req.params;
49+
const { accessToken } = req.query; // Optional access token for authenticated requests
4950

5051
try {
5152
const apiUrl = `https://api.github.com/users/${username}/repos`;
52-
const response = await axios.get(apiUrl) as {data: any, status: number};
53+
54+
const headers = accessToken
55+
? { Authorization: `Bearer ${accessToken}` }
56+
: undefined;
57+
58+
const response = await axios.get(apiUrl, { headers }) as { data: any, status: number };
5359

5460
if (response.status !== 200) {
5561
return res.status(400).json({ error: `Error fetching repos: ${response.status}` });

nextstep-frontend/src/handlers/githubAuth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ export const fetchGitHubRepos = async (username: string) => {
1818
export const connectToGitHub = async (username: string) => {
1919
if (!username) {
2020
throw new Error('Username is required to connect to GitHub.');
21-
}
21+
}
2222
return await fetchGitHubRepos(username);
2323
};
2424

2525
// Function to initiate GitHub OAuth login (redirect only)
2626
export const initiateGitHubOAuth = () => {
2727
const clientId = import.meta.env.VITE_GITHUB_CLIENT_ID; // Use environment variable
2828
const redirectUri = import.meta.env.VITE_GITHUB_REDIRECT_URI; // Use environment variable
29-
const scope = 'read:repo'; // Only read access to repositories
29+
const scope = 'public_repo'; // Only read access to repositories
3030
const authUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}`;
3131
window.location.href = authUrl; // Redirect the user to GitHub's authorization page
3232
};

nextstep-frontend/src/pages/MainDashboard.tsx

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ const MainDashboard: React.FC = () => {
4343
const [newSkill, setNewSkill] = useState('');
4444
const [selectedRole, setSelectedRole] = useState(() => localStorage.getItem('selectedRole') || '');
4545
const [repos, setRepos] = useState<{ id: number; name: string; html_url: string }[]>([]);
46+
const [useOAuth, setUseOAuth] = useState(true); // Toggle for GitHub connection method
47+
const [showAuthOptions, setShowAuthOptions] = useState(false); // Toggle for showing auth options
4648

4749
useEffect(() => {
4850
localStorage.setItem('aboutMe', aboutMe);
@@ -67,10 +69,35 @@ const MainDashboard: React.FC = () => {
6769
};
6870

6971
const handleGitHubConnect = async () => {
72+
if (!showAuthOptions) {
73+
setShowAuthOptions(true); // Show auth options first
74+
return;
75+
}
76+
7077
try {
71-
initiateGitHubOAuth();
78+
if (useOAuth) {
79+
initiateGitHubOAuth();
80+
} else {
81+
const username = prompt('Enter GitHub username:');
82+
if (!username) {
83+
alert('GitHub username is required for No Auth connection.');
84+
return;
85+
}
86+
const fetchedRepos = await connectToGitHub(username);
87+
setRepos(fetchedRepos);
88+
89+
// Fetch languages and add them as skills
90+
const languagesSet = new Set(skills);
91+
for (const repo of fetchedRepos) {
92+
const repoLanguages = await fetchRepoLanguages(repo.html_url);
93+
Object.keys(repoLanguages).forEach((lang) => languagesSet.add(lang));
94+
}
95+
setSkills(Array.from(languagesSet));
96+
}
7297
} catch (error) {
73-
console.error('Error initiating GitHub OAuth:', error);
98+
console.error('Error connecting to GitHub:', error);
99+
} finally {
100+
setShowAuthOptions(false); // Close auth options after proceeding
74101
}
75102
};
76103

@@ -203,14 +230,26 @@ const MainDashboard: React.FC = () => {
203230
>
204231
Connect to LinkedIn
205232
</Button>
233+
{showAuthOptions && (
234+
<FormControl fullWidth sx={{ mb: 2 }}>
235+
<InputLabel>GitHub Connection Method</InputLabel>
236+
<Select
237+
value={useOAuth ? 'oauth' : 'no-auth'}
238+
onChange={(e) => setUseOAuth(e.target.value === 'oauth')}
239+
>
240+
<MenuItem value="oauth">OAuth</MenuItem>
241+
<MenuItem value="no-auth">No Auth</MenuItem>
242+
</Select>
243+
</FormControl>
244+
)}
206245
<Button
207246
variant="contained"
208247
color="secondary"
209248
startIcon={<GitHub />}
210249
fullWidth
211250
onClick={handleGitHubConnect}
212251
>
213-
Connect to GitHub
252+
{showAuthOptions ? 'Proceed' : 'Connect to GitHub'}
214253
</Button>
215254
{
216255
repos.length > 0 && <Typography variant="h6" sx={{ mt: 3 }}>
@@ -226,7 +265,7 @@ const MainDashboard: React.FC = () => {
226265
</li>
227266
))}
228267
</ul>
229-
</Paper>
268+
</Paper>
230269
</Grid>
231270
</Grid>
232271
</Container>

0 commit comments

Comments
 (0)