-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (67 loc) · 2.1 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const SUPABASE_URL = "https://uavpsmlmcsfcplfxuubi.supabase.co";
const SUPABASE_KEY =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJvbGUiOiJhbm9uIiwiaWF0IjoxNjQzMTI5NTc1LCJleHAiOjE5NTg3MDU1NzV9.e0aZ2SUi8lpURmx72EmqKSZgvmAUYazp28Tus7PKl6Y";
const supabase = supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
var signUpForm;
var linksDiv;
var linksNum;
var linksUl;
var signOutButton;
var signedIn = false;
document.addEventListener("DOMContentLoaded", function () {
signUpForm = document.querySelector("#sbform");
linksDiv = document.querySelector("#the-links");
linksNum = linksDiv.querySelector("span.num");
linksUl = linksDiv.querySelector("ul");
signOutButton = signUpForm.querySelector('button[type="reset"]');
signUpForm.onsubmit = signIn.bind(signUpForm);
signUpForm.onreset = signOut.bind(signUpForm);
});
supabase.auth.onAuthStateChange(async (event, session) => {
if ("SIGNED_IN" === event) {
const response = await fetch("/.netlify/functions/generate-stripe-links", {
method: "POST",
body: session.access_token,
});
signOutButton.disabled = false;
const result = await response.json();
displayLinks(result.links);
}
if ("SIGNED_OUT" === event) {
signOutButton.disabled = true;
displayLinks({});
}
});
const displayLinks = (payload) => {
linksNum.innerHTML = payload.length;
let lx = "";
for (const [_, value] of Object.entries(payload)) {
lx += '<li><a href="' + value.url + '">' + value.customer + "</a></li>";
}
linksUl.innerHTML = lx;
};
const signIn = (event) => {
event.preventDefault();
const email = event.target[0].value;
supabase.auth
.signIn({ email })
.then((response) => {
if (response.error) {
throw new Error("signin error: " + response.error.message);
}
})
.catch((err) => {
console.error(err.response.text);
});
};
const signOut = (event) => {
event.target[0].value = "";
supabase.auth
.signOut()
.then(() => {
console.warn("signed out successfully");
})
.catch((err) => {
console.error("error while signing out", err);
});
};