Skip to content

Commit 5210bfc

Browse files
authored
Implemented oauth with github and related changes (#1338)
1 parent 0cf91c0 commit 5210bfc

File tree

6 files changed

+195
-153
lines changed

6 files changed

+195
-153
lines changed

login.html

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ <h2 class="title">Sign in</h2>
3030
<p class="social-text">Or Sign in with social platforms</p>
3131
<div class="social-media">
3232
<button type="button" class="github-login" id="gh-lg-btn">
33-
<img src="https://upload.wikimedia.org/wikipedia/commons/9/91/Octicons-mark-github.svg" alt="GitHub" width="40" height="40">
33+
<img src="https://upload.wikimedia.org/wikipedia/commons/9/91/Octicons-mark-github.svg" alt="GitHub" width="40" height="40" style="margin-left: 5px;">
3434
</button>
3535
</div>
3636
</form>
37-
37+
3838
<form action="#" class="sign-up-form">
3939
<h2 class="title">Sign up</h2>
4040
<div class="input-field">
@@ -50,18 +50,18 @@ <h2 class="title">Sign up</h2>
5050
<input type="email" placeholder="Email" />
5151
</div>
5252
<div class="input-field">
53-
<i class="fas fa-lock toggle-password" id="toggle-password-signup"></i>
54-
<input type="password" id="password-input-signup" placeholder="Password" minlength="8" />
53+
<i class="fas fa-lock toggle-password" id="toggle-password"></i>
54+
<input type="password" id="password-input" placeholder="Password" minlength="8" />
5555
</div>
5656
<div class="remember-me">
57-
<input type="checkbox" id="remember-me-signup" />
58-
<label for="remember-me-signup">Remember Me</label>
57+
<input type="checkbox" id="remember-me" />
58+
<label for="remember-me">Remember Me</label>
5959
</div>
6060
<input type="submit" class="btn" value="Sign up" />
6161
<p class="social-text">Or Sign up with social platforms</p>
6262
<div class="social-media">
6363
<button type="button" class="github-login" id="gh-sg-btn">
64-
<img src="https://upload.wikimedia.org/wikipedia/commons/9/91/Octicons-mark-github.svg" alt="GitHub" width="40" height="40">
64+
<img src="https://upload.wikimedia.org/wikipedia/commons/9/91/Octicons-mark-github.svg" alt="GitHub" width="40" height="40" style="margin-left: 5px;">
6565
</button>
6666
</div>
6767
</form>
@@ -71,24 +71,21 @@ <h2 class="title">Sign up</h2>
7171
<div class="panels-container">
7272
<a href="index.html" class="homeBtn">
7373
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 20" width="30" height="30">
74-
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" fill="#000000"/>
74+
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
7575
</svg>
7676
</a>
7777
<div class="panel left-panel">
7878
<div class="content">
7979
<h3>New here ?</h3>
8080
<p>
81-
Discover new experiences and profiles with awesome-github-profiles<br>
82-
Get access to exclusive content and features.<br>
83-
Create your account.
81+
Discover new experiences and profiles with awesome-github-profiles <br>Get access to exclusive content and features. <br> Create your account.
8482
</p>
8583
<button class="btn transparent" id="sign-up-btn">
8684
Sign up
8785
</button>
8886
</div>
8987
<img src="img/log.svg" class="image" alt="" />
9088
</div>
91-
9289
<div class="panel right-panel">
9390
<div class="content">
9491
<h3>One of us ?</h3>

login.js

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,76 +20,87 @@ document.addEventListener("DOMContentLoaded", () => {
2020
});
2121

2222
// GitHub Authentication Setup
23-
const GITHUB_CLIENT_ID = "your_github_client_id";
23+
const GITHUB_CLIENT_ID = "Ov23liz64bw3989HVcKK";
2424
const BACKEND_URL = "http://localhost:3000";
2525

26-
// Handle GitHub OAuth
26+
// GitHub auth handler
2727
const handleGitHubAuth = () => {
28-
const params = window.location.search;
29-
const urlParams = new URLSearchParams(params);
30-
const code = urlParams.get("code");
31-
28+
const params = new URLSearchParams(window.location.search);
29+
const code = params.get("code");
30+
3231
if (code) {
33-
localStorage.setItem("code", code);
34-
getGhUser();
32+
console.log("OAuth code received:", code);
33+
getGhUser(code);
3534
}
3635
};
3736

38-
// Get GitHub User Data
39-
const getGhUser = () => {
40-
let code = localStorage.getItem("code");
41-
if (!code || code === "null") return;
42-
37+
// Get GitHub user data
38+
const getGhUser = (code) => {
39+
console.log("Starting GitHub user fetch with code:", code);
40+
4341
fetch(`${BACKEND_URL}/api/auth/github?code=${code}`)
44-
.then((res) => res.json())
45-
.then((response) => {
46-
let resData = response.data;
47-
let token = new URLSearchParams(resData).get("access_token");
42+
.then(res => {
43+
console.log("Auth response status:", res.status);
44+
if (!res.ok) {
45+
throw new Error(`HTTP error! status: ${res.status}`);
46+
}
47+
return res.json();
48+
})
49+
.then(response => {
50+
console.log("Auth response data:", response);
51+
if (!response.data || !response.data.access_token) {
52+
throw new Error('No access token received');
53+
}
54+
55+
const token = response.data.access_token;
56+
console.log("Received access token, fetching user data");
4857

4958
return fetch(`${BACKEND_URL}/api/auth/github/getUser`, {
5059
headers: {
51-
Authorization: "Bearer " + token,
52-
},
60+
'Authorization': `Bearer ${token}`
61+
}
5362
});
5463
})
55-
.then((res) => res.json())
56-
.then((response) => {
64+
.then(res => {
65+
console.log("User data response status:", res.status);
66+
if (!res.ok) {
67+
throw new Error(`HTTP error! status: ${res.status}`);
68+
}
69+
return res.json();
70+
})
71+
.then(response => {
72+
console.log("User data response:", response);
73+
if (!response.user) {
74+
throw new Error('No user data received');
75+
}
76+
5777
const { name, email } = response.user;
58-
localStorage.setItem(
59-
"user-info",
60-
JSON.stringify({
61-
name: name,
62-
email: email,
63-
})
64-
);
65-
localStorage.removeItem("code");
78+
localStorage.setItem('user-info', JSON.stringify({ name, email }));
79+
80+
// Clean up URL
81+
const cleanUrl = window.location.href.split('?')[0];
82+
window.history.replaceState({}, '', cleanUrl);
83+
84+
// Redirect to home page
6685
window.location.href = "/";
6786
})
68-
.catch((error) => {
69-
console.error("GitHub auth error:", error);
87+
.catch(error => {
88+
console.error("Authentication error:", error);
89+
alert("Failed to authenticate with GitHub. Please try again.");
7090
});
7191
};
7292

73-
// Handle initial auth check
74-
if (localStorage.getItem("user-info")) {
75-
window.location.href = "/";
76-
} else if (localStorage.getItem("code")) {
77-
getGhUser();
78-
}
79-
8093
// GitHub button click handlers
81-
github_signIn.onclick = () => {
82-
window.location.assign(
83-
`https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}`
84-
);
85-
};
86-
87-
github_login.onclick = () => {
88-
window.location.assign(
89-
`https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}`
90-
);
94+
const initiateGitHubAuth = () => {
95+
const authUrl = new URL('https://github.com/login/oauth/authorize');
96+
authUrl.searchParams.append('client_id', GITHUB_CLIENT_ID);
97+
authUrl.searchParams.append('scope', 'user');
98+
window.location.href = authUrl.toString();
9199
};
92100

101+
github_signIn.onclick = initiateGitHubAuth;
102+
github_login.onclick = initiateGitHubAuth;
103+
93104
// Email validation function
94105
const isValidEmail = (email) => {
95106
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
@@ -125,7 +136,7 @@ document.addEventListener("DOMContentLoaded", () => {
125136
localStorage.removeItem("rememberedUsername");
126137
}
127138
alert("Login successful!");
128-
window.location.href = "index.html";
139+
window.location.href = "/";
129140
} else {
130141
alert("Invalid username or password");
131142
}
@@ -161,7 +172,7 @@ document.addEventListener("DOMContentLoaded", () => {
161172
localStorage.setItem("isLoggedIn", "true");
162173

163174
alert("Signup successful!");
164-
window.location.href = "index.html";
175+
window.location.href = "/";
165176
});
166177

167178
// Password visibility toggle
@@ -188,4 +199,9 @@ document.addEventListener("DOMContentLoaded", () => {
188199
usernameInput.value = localStorage.getItem("rememberedUsername");
189200
rememberMeCheckbox.checked = true;
190201
}
202+
203+
// Handle GitHub auth on page load
204+
if (window.location.search.includes("code")) {
205+
handleGitHubAuth();
206+
}
191207
});

node_modules/.package-lock.json

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 8 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"start": "nodemon server.js"
44
},
55
"dependencies": {
6-
"axios": "^1.7.7",
6+
"axios": "^1.7.9",
77
"cors": "^2.8.5",
8-
"dotenv": "^16.4.5",
8+
"dotenv": "^16.4.7",
99
"express": "^4.21.2",
1010
"puppeteer": "^22.15.0"
1111
},

0 commit comments

Comments
 (0)