Fastify Firebase Admin plugin
npm install @now-ims/fastify-firebase



!npm (scoped)
This plugin adds the Firebase Admin SDK to Fastify so you can easy use Firebase Auth, Firestore, Cloud Storage, Cloud Messaging, and more.
```
yarn add @now-ims/fastify-firebase
npm i @now-ims/fastify-firebase -S
Add it to you project with register and you're done! firebase
This plugin will add a namespace in your Fastify instance - you can access the Firebase SDK objects via fastify.firebase:
``
auth - e.g., fastify.firebase.auth().createUser()
firestore - e.g., fastify.firebase.firestore().collection('users').get()
storage - e.g., fastify.firebase.store().bucket('thumbnails')
machineLearning - e.g., fastify.firebase.machineLearning().createModel()
- options - if deploying from Google Cloud options are optionalname
- (_Default: 'default', Type: string_) is required if you want to load configurations for multiple projectscert
- (_Optional, Type: string_) is required if you you are not on Google Cloud or want to load multiple configurationsdatabaseURL
- (_Optional, Type: string_) is required for Realtime DatabasestorageBucket
- (_Optional, Type: string_) e.g., - do not include any gs://projectId
- (_Default: 'cert.projectId', Type: string_) e.g., my-google-cloud-project
#### Google Cloud - Application Default Credentials
`js
const fastify = require('fastify')({ logger: true });
fastify.register(require('fastify-sensible'));
fastify.register(require('@now-ims/fastify-firebase'));
fastify.get('/user/:id', async (req, reply) => {
const user = await fastify.firebase
.firestore()
.collection('users')
.doc(req.params.id)
.get();
if (!user.exists) {
return reply.notFound();
}
return user.data();
});
fastify.listen(4331, (err) => {
if (err) throw err;
console.log(server listening on ${fastify.server.address().port});`
});
#### Register multiple Firebase projects w/ options and Separate Route File
`js
const fastify = require('fastify')({ logger: true });
fastify.register(require('fastify-sensible'));
// Here we will load multiple configurations for multiple projects!
// You most likely will not need this, but it is available should you need it.
fastify.register(require('@now-ims/fastify-firebase'), {
name: 'client1',
cert: '/path/to/my/cert/',
});
// this can be called with either fastify.firebase or fastify.firebase['client1']
fastify.register(require('@now-ims/fastify-firebase'), {
name: 'client2',
cert: process.env.FIREBASE_CONFIG,
storageBucket: 'thumbnails.appspot.com',
});
// this can only be called with fastify.firebase['client2']
fastify.listen(4331, (err) => {
if (err) throw err;
console.log(server listening on ${fastify.server.address().port});`
});
In your route file you can simply do the following e.g.:
`js
async function userRoute(fastify, options) {
fastify.get('/users/:id', async (req, reply) => {
const { id } = req.params;
const user = await fastify.firebase['client2']
.firestore()
.collection('users')
.get(id)
.get();
if (!user.exists) {
return reply.notFound();
}
return user.data();
});
}
``
This project is sponsored and maintained by:
- Now IMS
Licensed under MIT.