Firebase是Alphabet(google)的子公司,提供了一些很好用的服務,例如,身分驗證 (Authentication)、Firestore(NoSQL database)。Firestore的主要是以兩個概念構成:collection及document,一個collection中可以有很多document,每個document中還可以有collection。Authentication可以利用不同方法登入,最簡單的就是透過電子郵件、密碼登入。
const auth = getAuth();
const res = await createUserWithEmailAndPassword(auth, account.email, account.password);
try {
const res = await createUserWithEmailAndPassword(
auth,
account.email,
account.password
);
setMessage("帳號已產生");
console.log({ res });
} catch (error) {
let message = "";
switch (error.code) {
case "auth/email-already-in-use":
message = "電子信箱已註冊";
break;
case "auth/weak-password":
message = "密碼強度不足";
break;
case "auth/invalid-email":
message = "電子郵件格式錯誤";
break;
default:
message = "系統錯誤:" + error.code;
}
setMessage(message);
}
const auth = getAuth();
const res = await signInWithEmailAndPassword(
auth,
account.email,
account.password
);
**useEffect(() => {
const unsub = onAuthStateChanged(auth,** (user)=>{
setCurrentUser(user);
console.log(user);
}**);
return () => {
unsub();
}
}, []);**