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
31
login.js
31
login.js
|
|
@ -1,28 +1,47 @@
|
||||||
import { initializeApp } from "https://www.gstatic.com/firebasejs/12.6.0/firebase-app.js";
|
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";
|
import { firebaseConfig } from "./config.js";
|
||||||
|
|
||||||
// Init
|
// Init
|
||||||
const app = initializeApp(firebaseConfig);
|
const app = initializeApp(firebaseConfig);
|
||||||
const auth = getAuth(app);
|
const auth = getAuth(app);
|
||||||
|
|
||||||
// Håndter klikk
|
// Håndter klikk på Logg Inn
|
||||||
const loginBtn = document.getElementById('btn-login');
|
const loginBtn = document.getElementById('btn-login');
|
||||||
|
|
||||||
if (loginBtn) {
|
if (loginBtn) {
|
||||||
loginBtn.addEventListener('click', async () => {
|
loginBtn.addEventListener('click', async () => {
|
||||||
|
const email = document.getElementById('inp-email').value;
|
||||||
|
const password = document.getElementById('inp-password').value;
|
||||||
const msg = document.getElementById('error-msg');
|
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 = "";
|
msg.textContent = "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await signInAnonymously(auth);
|
// Prøver å logge inn med e-post og passord
|
||||||
|
await signInWithEmailAndPassword(auth, email, password);
|
||||||
|
|
||||||
// Suksess -> Gå til hovedsiden
|
// Suksess -> Gå til hovedsiden
|
||||||
window.location.href = "Røsstad.html";
|
window.location.href = "Røsstad.html";
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
msg.textContent = "Feil: " + error.message;
|
// Gi en forståelig feilmelding
|
||||||
loginBtn.textContent = "Prøv igjen";
|
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 = "Logg Inn";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue