There is a little problem here with the Firestore security rules. I have a React/Redux SPA with a custom API using Firebase and Firestore.
I currently use the session cookies as described by the Firebase official documentation (https://firebase.google.com/docs/auth/admin/manage-cookies). So when I log in, the cookie is set and sent with the subsequent requests to the API, no problem with that.
But now when I'm logged in with the session cookie system, the Firestore rules don't let me read or write data with a "permission denied" error since the request.auth object of the Firestore request is no longer set with the session cookie system...
How can I detect if a user is authenticated through a session cookie in my Firestore security rules?
login route in my Firebase external API using session cookie:
login(req, res) {
auth.setPersistence(firebase.auth.Auth.Persistence.NONE);
auth.signInWithEmailAndPassword(req.query.email, req.query.password)
.then((response) => response.user.getIdToken().then((idToken) => {
const expiresIn = 60 * 15 * 1000;
admin.auth().createSessionCookie(idToken, { expiresIn })
.then((sessionCookie) => {
const options = { maxAge: expiresIn, httpOnly: true, secure: !!process.env.NODE_ENV === 'production' };
res.cookie('session', sessionCookie, options);
auth.signOut();
res.json(response);
}, () => {
res.status(401).write('Error while verifying token.');
});
})).catch((err) => {
console.error(err);
});
},
basic user get in the same API using Firestore; called right after login with cookie set in request header:
get(req, res) {
const { uid } = req;
db.collection('users').doc(uid).get().then((doc) => {
res.json(doc.data());
})
.catch((err) => {
console.log(req.path);
console.error(err);
res.json(err);
});
},
permission denied error from Firestore security rules:
Error [FirebaseError]: Missing or insufficient permissions.
the old Firestore security rules that i need to work:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
match /users/{userId} {
allow create, update, get: if isSignedIn();
}
match /medic/{medicId} {
allow create, read: if isSignedIn();
}
}
}
Thanks a lot!