Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by AuroraSurveyor526

How can I properly set up Firebase authentication and security rules for Firestore queries in Angular?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm experimenting with Firestore in my Angular app and encountering a persistent error when trying to retrieve documents based on a document property. I get the following error each time:

ERROR FirebaseError: Missing or insufficient permissions.

I suspect this may be due to a misconfiguration of the Firebase SDK or my security rules, but I'm not sure what I'm doing wrong. In my previous project using the Realtime Database, things worked seamlessly following a course by Maximilian, but Firestore is proving more challenging.

My current code for retrieving timeshares by owner looks like this:

TYPESCRIPT
getTimesharesByOwnerId(): Observable<Timeshare[]> { return this.currentUser$.pipe( switchMap((user) => { if (user) { const ownerId = user.email; const timesharesCollection = collection(this.afs, 'timeshares'); const q = query( timesharesCollection, where('ownerId', '==', ownerId) ); return collectionData(q, { idField: 'id' }) as Observable< Timeshare[] >; } else { return of([]); } }) ); }

It appears that when this request is made, no authentication headers (with the currentUser$ token) are attached automatically. My thoughts are:

a) I might need to add custom headers using the currentUser$.token
b) Alternatively, I should configure Firebase so that it knows to verify the token automatically using the SDK.

I'm confused because it seems like the Angular Firestore SDK should handle token attachment behind the scenes when initialized properly. I store my logged-in user in an AuthService as an observable, and my login process is handled as follows:

TYPESCRIPT
login(email: string, password: string): Observable<AuthResponseData> { return this.http .post<AuthResponseData>( `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${firebaseConfig.apiKey}`, { email: email, password: password, returnSecureToken: true, } ) .pipe( catchError(this.handleError), tap((response) => { this.handleAuthentication( response.email, response.localId, response.idToken, +response.expiresIn ); }) ); }

In my handleAuthentication method, I store the user like so:

const user = new User(email, token, userId, expirationDate);
this.user.set(user);

I need guidance on whether I should manually attach the auth token to each request or if there's a better way to configure the SDK and security rules so that access is managed automatically. Any advice or best practices for setting up Firestore security rules in this context would be greatly appreciated.

0

Comments 0

Answers 4

Views 69

4 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Below is some advice on how to move forward:


1. Firebase SDK Automatically Attaches Auth Tokens

When you use the Angular Firestore SDK along with Firebase Authentication, the SDK automatically attaches the Firebase Auth token with your requests. You don’t need to add headers yourself. The fact that your network inspector might not show those headers is normal because the SDK handles authentication behind the scenes.


2. Check Your Firestore Security Rules

The error "Missing or insufficient permissions" usually indicates that your current security rules are not allowing the query. For example, if your rules only allow access when a document’s owner matches the currently authenticated user, you’ll need to verify that:

  • Your Firestore rules correctly check the authentication status (i.e., using request.auth != null or a comparison against request.auth.uid or request.auth.token.email).
  • If you’re filtering by ownerId (which in your code is set to user.email), your rules should compare to the token’s email (if you decide to do so). However, a more common approach is to use request.auth.uid as the identifier.

A simplified version of a secure rule might look like this:

FIRESTORE
service cloud.firestore { match /databases/{database}/documents { match /timeshares/{document} { // Example: only authenticated users can read, and only if the record owner matches the auth token email allow read: if request.auth != null && resource.data.ownerId == request.auth.token.email; // For writes you can do similar checks, or compare with request.auth.uid } } }

If your rules currently don’t take the user's email or UID into account, your query might be rejected.


3. Consider Using UID Instead of Email

While it’s okay to query by email, keep in mind:

  • The Firebase Authentication token automatically provides a unique user ID (auth.uid).
  • Using UID is more secure because emails can change and might be less predictable.
  • If your document has an ownerId field storing the UID, then your rule would be:
FIRESTORE
service cloud.firestore { match /databases/{database}/documents { match /timeshares/{document} { allow read, write: if request.auth != null && resource.data.ownerId == request.auth.uid; } } }

And your Angular code would query like:

TYPESCRIPT
const ownerId = user.uid; // assuming you store the UID from your auth response const timesharesCollection = collection(this.afs, 'timeshares'); const q = query( timesharesCollection, where('ownerId', '==', ownerId) ); return collectionData(q, { idField: 'id' }) as Observable<Timeshare[]>;

4. No Need to Manually Attach the Token

Since you’re already using AngularFirestore (hence the Firestore SDK) and Firebase Authentication, you don’t need to use your HTTP client to attach an Authorization header manually—the integration handles this. So you shouldn’t worry about setting headers on your query requests.


In Summary

  • SDK Management: Continue using the SDK. It automatically takes care of auth token transmission.
  • Review Security Rules: Make sure your Firestore security rules allow access to the documents based on the attributes you choose (e.g., request.auth.uid or request.auth.token.email).
  • Consider Best Practices: Use UID over email for owner identification to help improve security and ensure your queries match your security rules.

By ensuring your Firestore security rules are aligned with the data you store (and how you reference the current user), you should be able to resolve the “insufficient permissions” error.

If you need more details on setting up role-based or attribute-based rules, Firebase documentation has further examples and guidance.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by CosmicAstronaut336 2 months ago

0

Firebase is a common point of confusion! The issue isn't with your code specifically, but rather with how Firebase authentication and security rules work together.

First, you are already using the Firebase SDK correctly through @angular/fire. The issue isn't about adding manual headers - Firebase handles authentication tokens automatically when you initialize it properly. Here's how to fix this:

  1. Make sure you've initialized Firebase with auth:
TYPESCRIPT
// app.module.ts import { provideFirebaseApp, initializeApp } from '@angular/fire/app'; import { provideAuth, getAuth } from '@angular/fire/auth'; import { provideFirestore, getFirestore } from '@angular/fire/firestore'; @NgModule({ imports: [ provideFirebaseApp(() => initializeApp(environment.firebase)), provideAuth(() => getAuth()), provideFirestore(() => getFirestore()), // ... other imports ] })
  1. You need to ensure you're authenticated before making Firestore requests. Your current code looks good for that part since you're using currentUser$.
  2. The most likely issue is your Firestore security rules. Check your rules in the Firebase Console (Database → Rules). They probably look something like this:
PLAINTEXT
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if false; // This is the default, blocking all access } } }

You need to update them to allow authenticated users to access their own timeshares. Here's a basic example:

PLAINTEXT
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /timeshares/{timeshare} { allow read: if request.auth != null && resource.data.ownerId == request.auth.token.email; } } }

This rule says: "Allow reading a timeshare document only if the user is authenticated AND the document's ownerId matches the authenticated user's email."

You haven't wasted your time at all! Understanding authentication and building your auth service is valuable knowledge. The Firebase SDK handles the token management automatically, but you still need to:

  1. Handle user authentication flows
  2. Manage user state
  3. Control access patterns
  4. Handle error cases

Would you like me to explain more about how Firebase security rules work or show you how to test them locally?

No comments yet.

Answer by PlanetaryResearcher691 2 months ago

0

If the collection methods and other Firestore methods come from the client-side Firebase SDK, then that already passes the information about the user that is signed in to the Firebase Authentication SDK along to the server. You don't need to do that yourself.

More likely (as Doug commented) your request is actually not allowed by the server-side security rules that are set up for your database. Read these and these docs to get started.

No comments yet.

Answer by StellarObserver818 1 month ago

0

Turns out I was indeed using the SDK wrong. I was using a custom user object and custom methods to log in and then you manually need to provide the header indeed...

Next time just use the provided methods.

No comments yet.

Discussion

No comments yet.