This commit is contained in:
Unknown 2024-05-20 09:38:08 -07:00
parent 7168a981bd
commit 373116f6ce
2 changed files with 117 additions and 122 deletions

View file

@ -19,9 +19,10 @@
},
body: new URLSearchParams({
client_id: "c91a3691-c800-4aed-9e85-1a2c896327de",
scope: "Files.ReadWrite.All",
scope: "openid offline_access Files.ReadWrite.All",
code: code,
redirect_uri: "http://localhost:5500/Pages/upload.html",
redirect_uri:
"http://localhost:5500/Pages/Callback.html",
grant_type: "authorization_code",
client_secret: "ddba6e4e-62b1-4a13-b165-08b00ef6723f",
}),

View file

@ -17,8 +17,120 @@
if (accessToken) {
// Store access token in local storage
localStorage.setItem("accessToken", accessToken);
updateAccessTokenDisplay();
}
});
// Function to update the access token display
function updateAccessTokenDisplay() {
// Retrieve access token from local storage
const accessToken = localStorage.getItem("accessToken");
// Update the access token display
document.getElementById("accessTokenDisplay").innerText =
accessToken || "Access token is not set.";
}
// Function to handle sign-in with Microsoft
function signInWithMicrosoft() {
window.open(
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?" +
"client_id=c91a3691-c800-4aed-9e85-1a2c896327de" + // Replace with your client ID
"&response_type=code" +
"&redirect_uri=http://localhost:5500/Pages/Callback.html" + // Replace with your redirect URI
"&scope=openid%20offline_access%20Files.ReadWrite.All",
"_blank"
);
}
// Function to handle logout
function logout() {
// Clear access token from local storage
localStorage.removeItem("accessToken");
// Open the logout endpoint in a new window
window.open(
"https://login.microsoftonline.com/common/oauth2/v2.0/logout" +
"?post_logout_redirect_uri=http://localhost:5500/Pages/logout.html",
"_blank"
);
}
// Function to upload file
function uploadFile() {
const fileInput = document.getElementById("fileInput");
const file = fileInput.files[0];
if (file) {
const formData = new FormData();
formData.append("file", file);
// Retrieve access token from local storage
const accessToken = localStorage.getItem("accessToken");
if (!accessToken) {
alert("Access token not found. Please sign in again.");
return;
}
fetch(
"https://graph.microsoft.com/v1.0/me/drive/root:/Photos/" +
file.name +
":/content",
{
method: "PUT",
body: formData,
headers: {
Authorization: "Bearer " + accessToken,
},
}
)
.then((response) => {
if (response.ok) {
alert("File uploaded successfully!");
} else {
alert("Error uploading file!");
}
})
.catch((error) => console.error("Error:", error));
} else {
alert("Please select a file to upload.");
}
}
// Function to preview the selected image
function previewImage() {
const fileInput = document.getElementById("fileInput");
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
document.getElementById("previewImage").src =
e.target.result;
document.getElementById("previewImage").style.display =
"block";
};
reader.readAsDataURL(file);
}
}
window.addEventListener("DOMContentLoaded", (event) => {
fetch("/Pages/navbar.html")
.then((response) => response.text())
.then((data) => {
document.getElementById("navbar").innerHTML = data;
});
fetch("/Pages/socials.html")
.then((response) => response.text())
.then((data) => {
document.getElementById("socials").innerHTML = data;
});
// Update access token display
updateAccessTokenDisplay();
// Listen for changes in local storage (e.g., when access token is updated)
window.addEventListener("storage", function (event) {
if (event.key === "accessToken") {
updateAccessTokenDisplay();
}
});
});
</script>
</head>
<body>
@ -53,130 +165,12 @@
<!-- Logout button -->
<button class="button-style" onclick="logout()">Log out</button>
<!-- Access Token Display -->
<h1>Access Token:</h1>
<div id="accessTokenDisplay"></div>
<script>
// Function to update the access token display
function updateAccessTokenDisplay() {
// Retrieve access token from local storage
var accessToken = localStorage.getItem("accessToken");
// Check if access token is defined
if (accessToken !== null) {
// Display the access token on the screen
document.getElementById("accessTokenDisplay").innerText = accessToken;
} else {
// If access token is not defined, display a message
document.getElementById("accessTokenDisplay").innerText = "Access token is not set.";
}
}
// Call the function initially to display the access token
updateAccessTokenDisplay();
// Listen for changes in local storage (e.g., when access token is updated)
window.addEventListener("storage", function(event) {
if (event.key === "accessToken") {
// Update the access token display
updateAccessTokenDisplay();
}
});
</script>
<!-- Access Token Field (Hidden) -->
<input type="hidden" id="accessToken" />
<!-- Footer -->
<div id="socials"></div>
<script>
window.addEventListener("DOMContentLoaded", (event) => {
fetch("/Pages/navbar.html")
.then((response) => response.text())
.then((data) => {
document.getElementById("navbar").innerHTML = data;
});
fetch("/Pages/socials.html")
.then((response) => response.text())
.then((data) => {
document.getElementById("socials").innerHTML = data;
});
});
function signInWithMicrosoft() {
window.open(
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?" +
"client_id=c91a3691-c800-4aed-9e85-1a2c896327de" + // Replace with your client ID
"&response_type=code" +
"&redirect_uri=http://localhost:5500/Pages/Callback.html" + // Replace with your redirect URI
"&scope=openid%20offline_access%20Files.ReadWrite.All",
"_blank"
);
}
function uploadFile() {
var fileInput = document.getElementById("fileInput");
var file = fileInput.files[0];
if (file) {
var formData = new FormData();
formData.append("file", file);
// Retrieve access token from local storage
var accessToken = localStorage.getItem("accessToken");
if (!accessToken) {
alert("Access token not found. Please sign in again.");
return;
}
fetch(
"https://graph.microsoft.com/v1.0/me/drive/root:/Photos/" +
file.name +
":/content",
{
method: "PUT",
body: formData,
headers: {
Authorization: "Bearer " + accessToken,
},
}
)
.then((response) => {
if (response.ok) {
alert("File uploaded successfully!");
} else {
alert("Error uploading file!");
}
})
.catch((error) => console.error("Error:", error));
} else {
alert("Please select a file to upload.");
}
}
function previewImage() {
var fileInput = document.getElementById("fileInput");
var file = fileInput.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById("previewImage").src =
e.target.result;
document.getElementById("previewImage").style.display =
"block";
};
reader.readAsDataURL(file);
}
}
function logout() {
// Clear access token from local storage
localStorage.removeItem("accessToken");
// Open the logout endpoint in a new window
window.open(
"https://login.microsoftonline.com/common/oauth2/v2.0/logout" +
"?post_logout_redirect_uri=http://localhost:5500/Pages/logout.html"
); // Replace with your logout redirect URI
}
</script>
</body>
</html>