Socialify

Folder ..

Viewing register.ts
53 lines (48 loc) • 1.7 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
import '../styles/login.css';
import { app } from "../handlers/firebaseHandler";
import { db } from '../handlers/databaseHandler';
import { getAuth, createUserWithEmailAndPassword, sendEmailVerification } from "firebase/auth";
import { doc, setDoc } from "firebase/firestore";

const auth = getAuth(app);


auth.onAuthStateChanged(async (user) => {
    if (user && user.emailVerified) {
        window.location.href = "/";
    } else if (user && !user.emailVerified) {
        sendEmailVerification(user);
        const data = {
            uid: user.uid,
            status: "",
            level: 1,
            xp: 0,
            role: "user",
            bio: "",
            isPrivate: false
        }
        const docRef = doc(db, "userProfiles", user.uid);
        await setDoc(docRef, data);
        if (docRef.id) {
            auth.signOut();
            window.location.href = "/login/?registered=true";
        }
    }
});

const registerForm = document.getElementById("registerForm");
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");
const confirmPasswordInput = document.getElementById("confirmPassword");

registerForm?.addEventListener("submit", (e) => {
    e.preventDefault();
    const email = emailInput as HTMLInputElement;
    const password = passwordInput as HTMLInputElement;
    const confirmPassword = confirmPasswordInput as HTMLInputElement;
    if (password.value !== confirmPassword.value) {
        alert("Passwords do not match.");
        return;
    }
    createUserWithEmailAndPassword(auth, email.value, password.value)
        .then(() => {})
        .catch((error) => {
            const errorMessage = error.message;
            alert(errorMessage);
        });
});