Created
January 18, 2020 23:02
-
-
Save discoNeko/584abbf2a6ebec60b566b2e14330ff61 to your computer and use it in GitHub Desktop.
onSnapshot.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {String} uid firebase/authentication のユーザーID | |
*/ | |
checkStatus ({ commit, state }, uid) { | |
return new Promise(resolve => { | |
firestore.collection('users').doc(uid) | |
.onSnapshot(doc => { | |
// usersコレクションにレコードがあるかチェック | |
const user = doc.exists ? doc.data() : null | |
// もし管理者権限が無ければログイン画面に飛ばす | |
if (user && user.authority !== 'admin') { | |
router.push({ name: 'login' }) | |
} | |
return resolve() | |
}, error => { | |
// ログアウトするとrulesで権限エラーが発生し、自動でリスナーをデタッチする仕様 | |
// それ以外のエラーが出た場合はエラー画面に飛ばす | |
if (error.code !== 'permission-denied') { | |
router.push({ name: 'error' }) | |
} | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment