Brandon
Brandon

Reputation: 1886

Cloud Firestore Authentication Regex

Is it possible to use a regex to identify users with Firestore?

I have a function:

function isDeveloper(userId) {
        return isUserAuthenticated(userId) && isCorrectEmail(request.auth.token.email);
}

function isCorrectEmail(email) {
    return email == '[email protected]' || email == '[email protected]' || email.matches('<regex>');
}

It can be done for documents:

match /{document} {
  allow write: if document.matches('.*@domain[.]com')
}

But this doesn't work for the email within a request. Does anybody know how I can do this?

Upvotes: 4

Views: 815

Answers (1)

jsaddwater
jsaddwater

Reputation: 1829

This works for me:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read: if isSignedIn();
      ///allow write: if isSignedIn();
    }
    match /users/{uid} {
        allow read: if isSignedIn() && (isGod() || isAllowedDomain())
      allow create: if isSignedIn() && (isGod() || isAllowedDomain())
      allow update: if isSignedIn() && (isGod() || isAllowedDomain()) && isSelf()
    }
    match /leads/{document} {
        allow read: if isSignedIn();
      allow update: if isSignedIn() && (isCreator() || isManager())
      allow delete: if isCreator();
      allow create: if isSignedIn();
    }

    /// Functions ///
    function existingData() {
        return resource.data
    }
    function incomingData() {
        return request.resource.data
    }
    function isSelf() {
        return request.auth.uid == resource.data.uid
    }
    function isCreator() {
        return request.auth.uid == resource.data.reporterUID
    }
    function isManager() {
        return false
    }
    function isSignedIn() {
            return request.auth.uid != null
    }
    function test () {
        return request.auth.uid == '-----------lm1bdWfhVP9GJw1'
    }
    function isGod () {
        return request.auth.uid == '-----------lm1bdWfhVP9GJw1'
    }
    function isAllowedDomain() {
        return request.auth.token.email_verified == true &&
                   request.auth.token.email.matches(".*@purplescout.se")
    }
  }
}

Upvotes: 2

Related Questions