iTranslated by AI
A Guide to Firebase After One Year of Experience
Introduction
It has been a year since I started using Firebase for both work and personal projects. I have summarized the ideas and concepts I wanted to know after finishing the tutorials. The sample code is in JavaScript.
Note: There is still much I want to write, but I ran out of time, so I plan to add more in the next post.
Firebase Functions
Environment Variables
In Firebase Functions, you can store information that you don't want to hard-code, such as passwords, in environment variables.
Set environment variables:
firebase functions:config:set service.id="123456789" service.pass="987654321"
Get the current list of environment variables:
firebase functions:config:get
It returns the following result:
{
"service": {
"id": "123456789",
"pass": "987654321"
}
}
Delete environment variables:
firebase functions:config:unset service.id service.pass
Use environment variables:
const functions = require('firebase-functions');
exports.userCreated = functions.database.ref('/users/{id}').onWrite(event => {
//
console.log(functions.config().service.id)
console.log(functions.config().service.pass)
});
Logging
While Firebase Functions logs can be output using console.log(), there are limitations, such as not being able to output as errors using console.error().
By using functions.logger.log(), you can output logs appropriately.
const functions = require("firebase-functions");
functions.logger.log("log:", someObj);
functions.logger.error("error:", someObj);
Testing Scheduled Execution
Scheduled execution in Firebase Functions is convenient, but when testing, you have to wait for the set time, which makes debugging difficult.
However, Firebase Functions schedules are actually registered in the Cloud Scheduler of the Google Cloud Platform (GCP). And the GCP Cloud Scheduler has a "Run now" button.
This is extremely convenient for development.
Firebase Authentication
Permission Management
- When you want to strengthen access permissions for Firestore or Firebase Storage for specific users only
- When you want to create pages accessible only to specific users
If you want to attach labels to Authentication users like this, it is convenient to use the claims of the Firebase Admin SDK.
admin
.auth()
.setCustomUserClaims(uid, { admin: true })
.then(() => {
//
});
In this example, true is set for the admin claim.
To retrieve it on the client side:
firebase.auth().currentUser.getIdTokenResult()
.then((idTokenResult) => {
// Confirm the user is an Admin.
if (idTokenResult.claims.admin) {
// If admin is true
} else {
// If admin is false
}
})
.catch((error) => {
console.log(error);
});
Control Access with Custom Claims and Security Rules | Firebase
Firebase Hosting
Automatic Deployment from GitHub
Firebase officially supports automatic deployment from GitHub.
Add automatic deployment to a new project:
firebase init hosting
Add automatic deployment to an existing project:
firebase init hosting:github
Deploy to live and preview channels via GitHub pull requests
Conclusion
Topics I would like to write about in future posts:
- Stripe payments
- Firestore, Storage security rules
- Hosting multiple site deployment
- Functions deployment regions
- Functions memory, timeout
- Extensions
I have summarized the knowledge I gained from developing at home every day for two and a half years! Please take a look if you are interested.
Discussion