Socialify

Folder ..

Viewing login.ts
42 lines (37 loc) • 1.5 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
import '../styles/login.css';
import { app } from "../handlers/firebaseHandler";
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";

const auth = getAuth(app);

// check auth state
auth.onAuthStateChanged((user) => {
    if (user && user.emailVerified) {
        window.location.href = "/";
    } else if (user && !user.emailVerified) {
        auth.signOut();
        alert("Please verify your email before logging in.");
    }
});

const loginForm = document.getElementById("loginForm");
const emailInput = document.getElementById("email");
const passwordInput = document.getElementById("password");

loginForm?.addEventListener("submit", (e) => {
    e.preventDefault();
    const email = emailInput as HTMLInputElement;
    const password = passwordInput as HTMLInputElement;
    signInWithEmailAndPassword(auth, email.value, password.value)
        .then(() => {})
        .catch((error) => {
            const errorMessage = error.message;
            alert(errorMessage);
        });
});

const urlParams = new URLSearchParams(window.location.search);
const registered = urlParams.get('registered');
if (registered) {
    const messageBox = document.createElement("div");
    messageBox.classList.add("messageBox");
    const messageBoxText = document.createElement("p");
    messageBoxText.innerText = "You have successfully registered! Please check your email to verify your account.";
    messageBox.appendChild(messageBoxText);
    document.body.appendChild(messageBox);
}