-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
55 lines (49 loc) · 1.92 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { createClient } from "https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm";
const supabaseUrl = "https://qgphiubbfnunppcnpudx.supabase.co";
const supabaseKey =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFncGhpdWJiZm51bnBwY25wdWR4Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTAxMzc4NDksImV4cCI6MjAwNTcxMzg0OX0.tq0YKntPaAPhUVXA1LePZDwlVren-TJ3OnTs07JSAGM";
const supabaseClient = createClient(supabaseUrl, supabaseKey);
//live server deployment url on render
const baseUrl = "https://to-do-main.onrender.com";
// Event listener for Google login button
document.addEventListener("DOMContentLoaded", () => {
document
.getElementById("google-login")
.addEventListener("click", async () => {
try {
const { error } = await supabaseClient.auth.signInWithOAuth({
provider: "google",
});
if (error) {
throw error;
}
} catch (error) {
console.error("Error logging in with Google:", error.message);
}
});
// Event listener for logout button
// document.getElementById("logout").addEventListener("click", async () => {
// try {
// const { error } = await supabaseClient.auth.signOut();
// if (error) {
// throw error;
// }
// } catch (error) {
// console.error("Error logging out:", error.message);
// }
// });
// Handling auth state changes
supabaseClient.auth.onAuthStateChange((event, session) => {
if (event === "SIGNED_IN") {
console.log("User signed in:", session.user);
const todoBox = document.querySelector(".todo-box");
todoBox.style.display = "block";
// Update UI for logged-in user
} else if (event === "SIGNED_OUT") {
console.log("User signed out");
// Update UI for logged-out user
document.querySelector(".login-box").classList.add("active");
document.querySelector(".todo-box").classList.remove("active");
}
});
});