莉莉在十二年前創立了連鎖獸醫診所 Pet Theory。過去幾年來,Pet Theory 的連鎖事業成長得非常快速。原本的預約系統無法應付目前的顧客人數,也無法讓顧客自行預約看診時間,因此莉莉拜託 IT 部門的阿克和顧問小茹,請他們建構能輕鬆擴充的雲端式系統。在本實驗室中,您將建構完善的 Firebase 網頁應用程式,供使用者記錄資訊和即時預約看診時間。
added 630 packages, and audited 631 packages in 42s
69 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 10.7.0 -> 10.8.3
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.3
npm notice To update run: npm install -g npm@10.8.3
npm notice
npm warn using --force Recommended protections disabled.
up to date, audited 631 packages in 2s
69 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
請使用方向鍵和空格鍵選取「Firestore」和「託管」,確認殼層與下方所示內容相同,然後按下 Enter 鍵:
? Which Firebase CLI features do you want to set up for this folder? Press Space to select features, then Enter to confirm your choices.
◯ Realtime Database: Configure a security rules file for Realtime Database and (optionally) provision default insta
◉ Firestore: Configure security rules and indexes files for Firestore
◯ Functions: Configure a Cloud Functions directory and its files
❯◉ Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys
◯ Hosting: Set up GitHub Action deploys
◯ Storage: Configure a security rules file for Cloud Storage
完成剩餘步驟來設定 Firebase:
使用按鍵選取「Use an existing project」,然後按下 Enter 鍵。
從清單中選取專案 ID ,然後按下 Enter 鍵。
依序按下 Enter 鍵和 N 鍵,保留 firestore.rules 檔案。
依序按下 Enter 鍵 和 N 鍵,保留 firestore.indexes.json 檔案。
按下 Enter 鍵保留公開目錄,然後按下 N 鍵禁止改寫 /index.html 檔案。
按下 Enter 鍵,然後在畫面上顯示「Set up automatic builds and deploys with GitHub?」時,按下 N 鍵。
系統提示覆寫 404.html 檔案時,輸入 N。
系統提示覆寫 index.html 檔案時,輸入 N。
輸出內容範例:
✔ Wrote public/404.html
✔ Wrote public/index.html
i Writing configuration info to firebase.json...
i Writing project information to .firebase...
i Writing gitignore file to .gitignore...
✔ Firebase initialization complete!
注意事項:
如果出現「browser is not supported or 3rd party cookies and data may be disabled」錯誤,請確保您已在瀏覽器啟用 Cookie。
如要在 Chrome 接受 Cookie,請先點選網址分頁最右側的眼睛圖示,再點選彈出式視窗中的藍色連結。點選「網站無法正常運作嗎?」連結來更新瀏覽器設定,藉此接受 Cookie。
使用提供給您的使用者名稱登入 (),下列頁面隨即開啟:
像 Pet Theory 這樣的小公司沒有足夠的資源,員工也沒有相關技能可以處理這項工作。
這種情況下,讓應用程式使用者以既有的 Google 帳戶 (或其他識別資訊提供者) 登入會很有幫助!
注意事項:
管理密碼十分困難,並有可能讓公司面臨更多風險,使用者也不會想再設定新的使用者 ID 和密碼。
您已部署程式碼,讓使用者透過 Google 驗證功能存取預約應用程式。
工作 7:在網頁應用程式新增顧客頁面
請返回終端機,並使用編輯器查看「public」資料夾中的檔案。
開啟 public/customer.js 檔案,然後複製及貼上下列程式碼:
let user;
firebase.auth().onAuthStateChanged(function(newUser) {
user = newUser;
if (user) {
const db = firebase.firestore();
db.collection("customers").doc(user.email).onSnapshot(function(doc) {
const cust = doc.data();
if (cust) {
document.getElementById('customerName').setAttribute('value', cust.name);
document.getElementById('customerPhone').setAttribute('value', cust.phone);
}
document.getElementById('customerEmail').innerText = user.email;
});
}
});
document.getElementById('saveProfile').addEventListener('click', function(ev) {
const db = firebase.firestore();
var docRef = db.collection('customers').doc(user.email);
docRef.set({
name: document.getElementById('customerName').value,
email: user.email,
phone: document.getElementById('customerPhone').value,
})
})