Last active
June 2, 2020 14:46
-
-
Save zachary-russell/4c4eb5e3fdb86581bb0b373519cf38a5 to your computer and use it in GitHub Desktop.
Firestore Security Rules
This file contains hidden or 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
import * as firebase from "firebase/app"; | |
import { db } from "./db"; | |
import "firebase/auth"; | |
/** | |
* Check if the account used for login is valid | |
* | |
* @returns {Promise} | |
*/ | |
function validAccountCheck() { | |
const user = firebase.auth().currentUser; | |
return db | |
.collection("Employees") | |
.doc(user.uid) | |
.set( | |
{ | |
displayName: user.displayName, | |
email: user.email, | |
photoURL: user.photoURL | |
}, | |
{ | |
merge: true | |
} | |
) | |
.then(() => true) | |
.catch(err => { | |
// Not a white-listed domain | |
console.log(err); | |
return false; | |
}); | |
} | |
export { validAccountCheck }; |
This file contains hidden or 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
service cloud.firestore { | |
match /databases/{database}/documents { | |
function validAccount(userEmail){ | |
return userEmail.split('@')[1] == 'example.com' || userEmail.split('@')[1] == 'othersite.com'; | |
} | |
match /Employees { | |
match /{userId}{ | |
allow read: if request.auth != null && validAccount(request.auth.token.email); | |
allow write: if request.auth.uid == userId /// Avoid editing another person's profile | |
&& validAccount(request.auth.token.email); | |
} | |
} | |
match /{document=**} { | |
allow read, write: if request.auth.uid != null && validAccount(request.auth.token.email); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment