Adonisjs 5, Firebase admin, Firebase for Adonis5
Firebase admin providers for AdonisJS 5
npm i --save adonis5-firebase-admin
Compile your code:
node ace serve --watch
Connect all dependences:
node ace invoke adonis5-firebase-admin
- For other configuration, please update the
config/firebase.ts
.
After adding firebase-admin provider to your app, you can import firebaseAdmin to access all the functions of the firebase admin
import firebaseAdmin from '@ioc:Adonis/Addons/FirebaseAdmin'
- For example, you can create a middleware to check if the user is authenticated.
node ace make:middleware Authenticate
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import firebaseAdmin from '@ioc:Adonis/Addons/FirebaseAdmin'
export default class Authenticate {
public async handle ({ request, response }: HttpContextContract, next: () => Promise<void>) {
const idToken = request.header('Authorization') as string
firebaseAdmin.auth().verifyIdToken(idToken)
.then(async function (decodedToken) {
const uid = decodedToken.uid
...
await next()
}).catch(function (error) {
response.status(401).send(error)
})
}
}
For additional details of Firebase Admin API, please check the Firebase SDK documentation by this link Firebase docs