プロジェクト作成
https://console.firebase.google.com/
にアクセスしてプロジェクトを作成する
- ウェブアプリに Firebase を追加。アプリ側で apiKey や databaseURL などの設定を行う。
- Database -> Database を作成 -> テストモードで開始
- Database -> ルール -> allow read write
- Authentication -> ログイン方法を設定 -> 匿名
- Storage -> 始める -> OK
- アプリ側で
npm install firebase
を追加
Authentication
https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user
- ログイン状態の変更を監視
firebase.auth().onAuthStateChanged(async (user) => {
- ログインがない場合、匿名ユーザでログインする。
await firebase.auth().signInAnonymously()
- 現在のユーザを取り出す。
firebase.auth().currentUser