diff --git a/app/src/api/api.ts b/app/src/api/api.ts index b99da0a..dccb3d4 100644 --- a/app/src/api/api.ts +++ b/app/src/api/api.ts @@ -307,8 +307,8 @@ const Bookmark = gql` const CreateContentFromUrl = gql` ${BaseContentFields} - mutation CreateContentFromUrl($url: String!, $authProviderId: String) { - createContentFromUrl(url: $url, authProviderId: $authProviderId) { + mutation CreateContentFromUrl($url: String!) { + createContentFromUrl(url: $url) { ...BaseContentFields } } diff --git a/chrome-extension-mv3-firebase/dist.zip b/chrome-extension-mv3-firebase/dist.zip deleted file mode 100644 index 8ca7532..0000000 Binary files a/chrome-extension-mv3-firebase/dist.zip and /dev/null differ diff --git a/chrome-extension-mv3-firebase/src/css/popup.css b/chrome-extension-mv3-firebase/src/css/popup.css index 3531446..5d03378 100644 --- a/chrome-extension-mv3-firebase/src/css/popup.css +++ b/chrome-extension-mv3-firebase/src/css/popup.css @@ -70,6 +70,12 @@ h1 { font-size: 18px; } +.btn__sign_out { + margin-top: 10px; + background-color: #f44336; + color: white; +} + .btn__google:hover { background-color: #fff; color: #4285f4; @@ -92,6 +98,6 @@ h1 { display: none; } -#main-content { +/* #main-content { display: none; -} +} */ diff --git a/chrome-extension-mv3-firebase/src/popup/main-script.js b/chrome-extension-mv3-firebase/src/popup/main-script.js index 86e0cdb..5a81fd3 100644 --- a/chrome-extension-mv3-firebase/src/popup/main-script.js +++ b/chrome-extension-mv3-firebase/src/popup/main-script.js @@ -3,6 +3,7 @@ import { GoogleAuthProvider, onAuthStateChanged, signInWithCredential, + signOut, } from "firebase/auth"; import { firebaseApp } from "./firebase_config"; @@ -10,12 +11,22 @@ const auth = getAuth(firebaseApp); console.log("popup main!"); +const apiUrl = process.env.API_URL; +console.log(`API URL: ${apiUrl}`); + +const isDevelopment = + apiUrl.includes("localhost") || apiUrl.includes("127.0.0.1"); + function handleAuthStateChange(user) { if (user) { console.log("logged in!"); console.log("current user:", user); document.getElementById("likeContent").style.display = "block"; saveCurrentLink(); + + if (isDevelopment) { + document.getElementById("signOutContainer").style.display = "block"; + } } else { console.log("No user"); window.location.replace("./popup.html"); @@ -42,27 +53,32 @@ function checkForExistingToken() { }); } -const apiUrl = process.env.API_URL; -console.log(`API URL: ${apiUrl}`); - async function saveCurrentLink() { const messageDiv = document.getElementById("message"); chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { const tab = tabs[0]; const user = auth.currentUser; - const authProviderId = user ? user.uid : null; + + if (!user) { + console.error("No user logged in"); + messageDiv.textContent = "Please log in to save links."; + return; + } console.log("saving link using api url:", apiUrl); try { + const idToken = await user.getIdToken(); + const response = await fetch(`${apiUrl}/graphql`, { method: "POST", headers: { "Content-Type": "application/json", + Authorization: `Bearer ${idToken}`, }, body: JSON.stringify({ - query: `mutation($url: String!, $authProviderId: String) { - createContentFromUrl(url: $url, authProviderId: $authProviderId) { + query: `mutation($url: String!) { + createContentFromUrl(url: $url) { id title websiteUrl @@ -73,7 +89,6 @@ async function saveCurrentLink() { }`, variables: { url: tab.url, - authProviderId, }, }), }); @@ -111,21 +126,28 @@ async function checkBookmarkStatus() { const tab = tabs[0]; const url = tab.url; const user = auth.currentUser; - const authProviderId = user ? user.uid : null; + + if (!user) { + console.error("No user logged in"); + messageDiv.textContent = "Please log in to check bookmark status."; + return; + } try { + const idToken = await user.getIdToken(); + const response = await fetch(`${apiUrl}/graphql`, { method: "POST", headers: { "Content-Type": "application/json", + Authorization: `Bearer ${idToken}`, }, body: JSON.stringify({ - query: `query($url: String!, $authProviderId: String) { - getIsBookmarked(url: $url, authProviderId: $authProviderId) + query: `query($url: String!) { + getIsBookmarked(url: $url) }`, variables: { url, - authProviderId, }, }), }); @@ -169,24 +191,31 @@ async function toggleBookmark() { } const user = auth.currentUser; - const authProviderId = user ? user.uid : null; + + if (!user) { + console.error("No user logged in"); + messageDiv.textContent = "Please log in to toggle bookmark."; + return; + } try { + const idToken = await user.getIdToken(); + const response = await fetch(`${apiUrl}/graphql`, { method: "POST", headers: { "Content-Type": "application/json", + Authorization: `Bearer ${idToken}`, }, body: JSON.stringify({ - query: `mutation($contentId: ID!, $authProviderId: String) { - bookmarkContent(contentId: $contentId, authProviderId: $authProviderId) { + query: `mutation($contentId: ID!) { + bookmarkContent(contentId: $contentId) { id isBookmarked } }`, variables: { contentId: contentId, - authProviderId, }, }), }); @@ -217,8 +246,29 @@ async function toggleBookmark() { }); } +console.log("isDevelopment", isDevelopment); + +function signOutUser() { + console.log("signing out user now"); + + signOut(auth) + .then(() => { + console.log("User signed out successfully"); + window.location.replace("./popup.html"); + }) + .catch((error) => { + console.error("Error signing out:", error); + }); +} + document .getElementById("likeContent") .addEventListener("click", toggleBookmark); +if (isDevelopment) { + document + .getElementById("signOutButton") + .addEventListener("click", signOutUser); +} + onAuthStateChanged(auth, handleAuthStateChange); diff --git a/chrome-extension-mv3-firebase/src/popup/main.html b/chrome-extension-mv3-firebase/src/popup/main.html index 6b3d860..34798f2 100644 --- a/chrome-extension-mv3-firebase/src/popup/main.html +++ b/chrome-extension-mv3-firebase/src/popup/main.html @@ -4,12 +4,15 @@ +
Will appear on your profile
+ -