-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1152 from ritukumari89/main
Github Login
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const githubLoginButton = document.getElementById("github-login-button"); | ||
|
||
githubLoginButton.addEventListener("click", () => { | ||
const clientID = "9b422344fa31d0dfaa43"; | ||
const redirectURI = "http://127.0.0.1:5500/GithubProfile.html"; | ||
const scope = "read:user"; | ||
|
||
const githubAuthURL = `https://github.com/login/oauth/authorize?client_id=${clientID}&redirect_uri=${redirectURI}&scope=${scope}`; | ||
|
||
window.location.replace(githubAuthURL); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Github</title> | ||
</head> | ||
<body> | ||
<button id="github-login-button">Login with GitHub</button> | ||
<script src="./Github.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Login Page</title> | ||
</head> | ||
<body> | ||
<h1>Welcome to Github</h1> | ||
<button id="logout-button" onclick="logout()">Logout</button> | ||
<script> | ||
const clientID = "9b422344fa31d0dfaa43"; | ||
const clientSecret = "96cb462ea558914a876859a7a959bd8f76c5fe5b"; | ||
|
||
async function exchangeCodeForToken(code) { | ||
const response = await fetch("https://github.com/login/oauth/access_token", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
client_id: clientID, | ||
client_secret: clientSecret, | ||
code: code | ||
}) | ||
}); | ||
|
||
const { access_token } = await response.json(); | ||
return access_token; | ||
} | ||
|
||
async function getUserInfo(accessToken) { | ||
const response = await fetch("https://api.github.com/user", { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}` | ||
} | ||
}); | ||
|
||
return response.json(); | ||
} | ||
|
||
// async function displayUserInfo() { | ||
// const accessToken = sessionStorage.getItem("access_token"); | ||
// if (!accessToken) { | ||
// // Redirect to the login page if accessToken is not available | ||
// window.location.href = "GithubLogin.html"; | ||
// return; | ||
// } | ||
|
||
// const userInfo = await getUserInfo(accessToken); | ||
|
||
// // Get the user name from the userInfo object | ||
// const userName = userInfo.name; | ||
|
||
// // Add the user name to the link | ||
// const link = document.getElementById("user-link"); | ||
// link.textContent = userName; | ||
// link.href = userInfo.html_url; | ||
|
||
// console.log("Successfully logged in with GitHub:", userInfo); | ||
// } | ||
|
||
|
||
function logout() { | ||
|
||
// Clear session data | ||
sessionStorage.clear(); | ||
|
||
console.log("Successfully logged out."); | ||
|
||
// Redirect to the login page | ||
window.location.href = "GithubLogin.html"; | ||
} | ||
</script> | ||
</body> | ||
</html> |