-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
200 lines (161 loc) · 5.39 KB
/
script.js
File metadata and controls
200 lines (161 loc) · 5.39 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const SUPABASE_URL = "https://jwpvozanqtemykhdqhvk.supabase.co";
const SUPABASE_ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imp3cHZvemFucXRlbXlraGRxaHZrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzkzNzY3MDgsImV4cCI6MjA1NDk1MjcwOH0.uoNqHwXBalSEoaJgtlmPE8gMr4VmTGmL-XDPFJq1Xr0";
const sb = window.supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
var popupid = 0;
async function signUpUser(email, password) {
const { data, error } = await sb.auth.signUp({
email,
password
});
if (error) {
console.error("Sign-up error:", error.message);
return { data, error };
}
return { data, error };
}
async function logInUser(email, password) {
const { data, error } = await sb.auth.signInWithPassword({
email,
password
});
return { data, error };
}
async function logOut() {
await sb.auth.signOut();
window.location.href = "/";
}
async function signInWithProvider(provider) {
try {
const { data: authData, error: authError } = await sb.auth.signInWithOAuth({
provider: provider,
options: {
redirectTo: "https://compnus.github.io/u/verify.html"
}
});
if (authError) {
throw new Error(authError.message);
}
const user = authData.user;
const email = user.email;
const { data: existingUser, error: userError } = await sb
.from("users")
.select("id")
.eq("email", email)
.single();
if (userError) {
throw new Error("Error checking user in the database: " + userError.message);
}
if (existingUser) {
await sb
.from("users")
.update({ provider: provider })
.eq("email", email);
} else {
const { error: insertError } = await sb
.from("users")
.insert({ id: user.id, email, provider: provider });
if (insertError) {
throw new Error("Error inserting new user: " + insertError.message);
}
}
} catch (error) {
console.error("Error during login/signup with provider:", error.message);
}
}
async function resetPassword() {
const email = document.getElementById("email").value;
const status = document.getElementById("status");
status.innerHTML = "Please wait...";
if (!email) {
status.innerHTML = "Please enter your email.";
return;
}
const { error } = await sb.auth.resetPasswordForEmail(email);
if (error) {
console.log("Error: " + error.message);
status.innerHTML = error.message;
} else {
status.innerHTML = "Password reset link sent! Check your email.";
}
}
async function handlePostVerification() {
const { data, error } = await sb.auth.getUser();
if (error || !data.user) {
window.location.href = "/u/signup.html?error=oauth_failed";
return error;
}
const { error: dbError } = await sb.from("users").insert([
{
id: data.user.id,
email: data.user.email,
username: data.user.email.split("@")[0],
created: data.user.created_at
}
], { onConflict: ['id'] });
if (dbError) {
return dbError.code;
}
return true;
}
async function getUser() {
const { data, error } = await sb.auth.getUser();
if (error || !data.user) {
return { user:false, data:error };
}
return { user: true, data: data.user };
}
function popup(title, message, close = true, compact = false) {
var x = document.createElement("div");
x.style.opacity = 0;
x.style.transition = '0.1s';
x.id = "popup" + popupid++;
x.className = "popup";
x.innerHTML = `
<div onclick="e = window.event; e.stopPropagation()" class="${compact?"cm":""}">
<div class="inside">
<h1>${title}</h1>
<h2 onclick="document.getElementById('${x.id}').style.opacity = 0; window.setTimeout(() => document.body.removeChild(document.getElementById('${x.id}')), 201)">X</h2>
</div>
<p style="margin-bottom:0">
${message}
</p>
</div>
`;
if (close) {
x.onclick = () => {
x.style.opacity = 0;
window.setTimeout(() => document.body.removeChild(document.getElementById(x.id)), 201);
}
}
document.body.appendChild(x);
window.setTimeout(() => x.style.opacity = 1, 1);
}
async function getBalance(uid) {
const { data: balance, error: userExistsErrorn } = await sb
.from("udata")
.select("balance_nus, balance_noca, balance_sats")
.eq("user_id", uid)
.single();
if (!balance || userExistsErrorn) return false;
return [balance.balance_nus, balance.balance_noca, balance.balance_sats];
}
function GetURLParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}
async function getVariable(vars) {
const { data, error } = await sb
.from("variable")
.select("value")
.eq("key", vars)
.single();
if (!data || error) return null;
else return data.value;
}
console.log("script loaded");