-
Notifications
You must be signed in to change notification settings - Fork 3
/
profile.js
112 lines (93 loc) · 3.32 KB
/
profile.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
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
// This is a work in progress, do not expect any of these features to work.
// - cobblesteve01
const { Client } = Appwrite;
const client = new Client();
client.setProject('673cc7500006c91704ce');
function displayLogin() {
const loginForm = document.getElementById("login");
const registerForm = document.getElementById("register");
loginForm.style.display = "block";
registerForm.style.display = "none";
}
function displayRegister() {
const loginForm = document.getElementById("login");
const registerForm = document.getElementById("register");
loginForm.style.display = "none";
registerForm.style.display = "block";
}
function showToast(message) {
const toast = document.getElementById("toast");
toast.textContent = message;
toast.classList.add("show");
setTimeout(() => toast.classList.remove("show"), 6000);
}
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.0.2/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/11.0.2/firebase-analytics.js";
const firebaseConfig = {
apiKey: "AIzaSyCaQhCh3NMAZ9evrxGzx_ifbX_xJngifxU",
authDomain: "classplay-login.firebaseapp.com",
projectId: "classplay-login",
storageBucket: "classplay-login.firebasestorage.app",
messagingSenderId: "648989613074",
appId: "1:648989613074:web:6f641d0b151712c7e48ccb",
measurementId: "G-2F1WPF3JPK"
};
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
async function registerUser() {
const username = document.getElementById('register-username').value;
const password = document.getElementById('register-password').value;
const pseudoEmail = `${username}@yourapp.com`;
const usernameRef = db.collection("usernames").doc(username);
const doc = await usernameRef.get();
if (doc.exists) {
showToast('stop trying to steal someone\'s username >:(');
return;
}
auth.createUserWithEmailAndPassword(pseudoEmail, password)
.then((userCredential) => {
const user = userCredential.user;
// Save username to Firestore
usernameRef.set({ uid: user.uid });
db.collection("users").doc(user.uid).set({ username: username, createdAt: new Date() });
showToast('successful login :D')
showLogout();
})
.catch((error) => {
showToast('not successful login :( ' + error.message)
});
}
function loginUser() {
const username = document.getElementById('login-username').value;
const password = document.getElementById('login-password').value;
const pseudoEmail = `${username}@yourapp.com`;
auth.signInWithEmailAndPassword(pseudoEmail, password)
.then((userCredential) => {
const user = userCredential.user;
window.alert("Login successful!");
showLogout();
})
.catch((error) => {
showToast('not successful login :( ' + error.message)
});
}
function logoutUser() {
auth.signOut().then(() => {
showToast('logged out :) ' + error.message)
showLogin();
}).catch((error) => {
showToast('not successful logout :( ' + error.message)
});
}
function showLogout() {
document.getElementById('register').style.display = 'none';
document.getElementById('login').style.display = 'none';
document.getElementById('logout').style.display = 'block';
}
auth.onAuthStateChanged((user) => {
if (user) {
showLogout();
} else {
displayLogin();
}
});