Implement email/password login and validation
Updated login functionality to use email and password authentication instead of anonymous sign-in. Added validation for empty fields and improved error handling.
This commit is contained in:
parent
e6141b5f7d
commit
6f41d8eaf1
1 changed files with 25 additions and 6 deletions
29
login.js
29
login.js
|
|
@ -1,28 +1,47 @@
|
|||
import { initializeApp } from "https://www.gstatic.com/firebasejs/12.6.0/firebase-app.js";
|
||||
import { getAuth, signInAnonymously } from "https://www.gstatic.com/firebasejs/12.6.0/firebase-auth.js";
|
||||
import { getAuth, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/12.6.0/firebase-auth.js";
|
||||
import { firebaseConfig } from "./config.js";
|
||||
|
||||
// Init
|
||||
const app = initializeApp(firebaseConfig);
|
||||
const auth = getAuth(app);
|
||||
|
||||
// Håndter klikk
|
||||
// Håndter klikk på Logg Inn
|
||||
const loginBtn = document.getElementById('btn-login');
|
||||
|
||||
if (loginBtn) {
|
||||
loginBtn.addEventListener('click', async () => {
|
||||
const email = document.getElementById('inp-email').value;
|
||||
const password = document.getElementById('inp-password').value;
|
||||
const msg = document.getElementById('error-msg');
|
||||
|
||||
loginBtn.textContent = "Autentiserer...";
|
||||
// Enkel sjekk at feltene ikke er tomme
|
||||
if (!email || !password) {
|
||||
msg.textContent = "Du må skrive inn både e-post og passord.";
|
||||
return;
|
||||
}
|
||||
|
||||
loginBtn.textContent = "Sjekker...";
|
||||
msg.textContent = "";
|
||||
|
||||
try {
|
||||
await signInAnonymously(auth);
|
||||
// Prøver å logge inn med e-post og passord
|
||||
await signInWithEmailAndPassword(auth, email, password);
|
||||
|
||||
// Suksess -> Gå til hovedsiden
|
||||
window.location.href = "Røsstad.html";
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
// Gi en forståelig feilmelding
|
||||
if (error.code === 'auth/invalid-credential' || error.code === 'auth/user-not-found' || error.code === 'auth/wrong-password') {
|
||||
msg.textContent = "Feil e-post eller passord.";
|
||||
} else if (error.code === 'auth/too-many-requests') {
|
||||
msg.textContent = "For mange forsøk. Prøv igjen senere.";
|
||||
} else {
|
||||
msg.textContent = "Feil: " + error.message;
|
||||
loginBtn.textContent = "Prøv igjen";
|
||||
}
|
||||
loginBtn.textContent = "Logg Inn";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue