LẬP TRÌNH WEB HTML - CSS - JAVA SCRIPT

BÀI 15: ĐĂNG NHẬP VỚI FIREBASE AUTHENTICATION

  • Chèn đoạn Script này dưới thẻ body

<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-app.js"></script>

<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-auth.js"></script>

  • Chèn config vào file js

const firebaseConfig = {

apiKey: "key",

authDomain: "web.firebaseapp.com",

databaseURL: "https://web-default-rtdb.firebaseio.com",

projectId: "web",

storageBucket: "web.appspot.com",

messagingSenderId: "id",

appId: "appid",

measurementId: "measurementid"

};

firebase.initializeApp(firebaseConfig);

let auth = firebase.auth()

let database = firebase.database()

  • Kiểm tra trạng thái đăng nhập

auth.onAuthStateChanged(function(user) {

if (user) {

// window.location.assign('./machine.html');

console.log("Đăng nhập thành công");

} else {

console.log("Chưa đăng nhập");

alert("Vui lòng đăng nhập hoặc tạo tài khoản mới");

}

});

  • Đăng xuất

function LogOut() {

auth.signOut().then(function(user) {

alert("Tạm biệt " + user.email + ". Miss you!")

})

.catch(function(error) {

alert("Có trục trặc từ hệ thống. Hãy thử lại sau")

});

}

  • Kiểm tra trạng thái xác minh email

auth.onAuthStateChanged(function(user) {

if (user) {

if (user.emailVerified) {

// console.log(user);

alert("Hello " + user.email);

} else {

window.location.assign('./login/xacminh.html');

}

} else {

window.location.assign('./login.html');

}

});