Firebase是Alphabet(google)的子公司,提供了一些很好用的服務,例如,身分驗證、Firestore(NoSQL database)。Firebase現在提供新的Web modular API,跟過去的Web namespaced API語法不太一樣,參考網路文件時要注意。本課程以Web modular API為主。
<aside> 🚧 尚未修改
</aside>
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();
}
}, []);**
在介面中,可以判斷state變數currentUser是否為null,就知道現在是否已登入,透過這樣的方式來改變介面的呈現內容。
{**!currentUser** ? (
<Button>LOGIN</Button>
) : (
<Button>LOGOUT</Button>
)}
登出可使用signOut()。
const handleLogout = async function () {
await signOut(auth);
alert("已登出");
};
useEffect(() => {
async function fetchData() {
const querySnapshot = await getDocs(collection(db, **"English"**));
const temp = [];
querySnapshot.forEach((doc) => {
temp.push(doc.data());
console.log(`${doc.id} => ${doc.data().question}`);
});
setExams(() => [...temp]);
}
fetchData();
// eslint-disable-next-line
}, []);
import { doc, getDoc } from "firebase/firestore";
const docRef = doc(db, **"English"**, **"1"**);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
console.log("Document data:", docSnap.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
useEffect(() => {
async function fetchData() {
const examCollection = collection(db, "English");
const queryExam = query(examCollection, **orderBy("unit"));**
const querySnapshot = await getDocs(queryExam);
const temp = [];
querySnapshot.forEach((doc) => {
temp.push(doc.data());
console.log(`${doc.id} => ${doc.data().question}`);
});
setExams(() => [...temp]);
//set the first question and clean result here
setQuestion(temp[0].question);
setResult("");
}
fetchData();
// eslint-disable-next-line
}, []);
useEffect(() => {
async function fetchData() {
const examCollection = collection(db, "English");
const queryExam = query(examCollection, **where("unit", "==", 1)**);
const querySnapshot = await getDocs(queryExam);
const temp = [];
querySnapshot.forEach((doc) => {
temp.push(doc.data());
console.log(`${doc.id} => ${doc.data().question}`);
});
setExams(() => [...temp]);
//set the first question and clean result here
setQuestion(temp[0].question);
setResult("");
}
fetchData();
// eslint-disable-next-line
}, []);
**await addDoc(collection(db, "user"), {
school: account.school
});**
**await setDoc(doc(db, "user", uid), {
school: account.school
});**
**await setDoc(doc(db, "user", uid), {
school: account.school
});**
await **updateDoc**(doc(db, "user", uid), {
school: account.school
});
const ref = doc(db, "text", props.article.docId);
updateDoc(ref,{**count: increment(1)**});
await updateDoc(ref, {
collect: arrayUnion(uid)
});
await updateDoc(ref, {
collect: arrayRemove(uid)
});
await **deleteDoc**(doc(db, "user", uid);
**deleteDoc(doc(db, "user", user.uid, "English", counterDoc.id));**
Timestamp
{time.toDate().toLocaleDateString("zh-TW")}
安全規則