If you have an App using Firebase Auth and need to connect them with your backend API, this is the plugin for you.
This auth strategy verify the token sent in the request and only grant access to valid tokens. Invalid tokens will get a 401 - Unauthorized
response.
- Compatible with Hapi v17
- Firebase Admin initializer and loader
- Gluten-free
npm install hapi-firebase-auth --save
yarn add hapi-firebase-auth
In case you don't want to initialize Firebase Admin externally, pass your Firebase credentials using the property credential
as shown below. This way the plugin will handle it for you.
// Load Hapi-Firebase Auth Strategy
const HapiFirebaseAuth = require('hapi-firebase-auth');
// Register the plugin
await server.register({
plugin: HapiFirebaseAuth
});
// Include auth strategy
server.auth.strategy('firebase', 'firebase', {
credential: {
projectId: '<PROJECT_ID>',
clientEmail: 'foo@<PROJECT_ID>.iam.gserviceaccount.com',
privateKey: '-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n',
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
}
})
You can get the credentials for your project in your Firebase Console. More details here.
If there is already an existing Firebase Admin instance, pass it using the property instance
as shown below.
// Load Hapi-Firebase Auth Strategy
const HapiFirebaseAuth = require('hapi-firebase-auth');
// Initialize the default app
const admin = require('firebase-admin');
// Register the plugin
await server.register({
plugin: HapiFirebaseAuth
});
// Initialize the Admin SDK with your credentials
// This is an example of what it should look in your code
admin.initializeApp({
credential: admin.credential.cert({
projectId: '<PROJECT_ID>',
clientEmail: 'foo@<PROJECT_ID>.iam.gserviceaccount.com',
privateKey: '-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n'
}),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});
// Include auth strategy using existing Firebase Admin instance
server.auth.strategy('firebase', 'firebase', {
instance: admin
})
If you are having issues with Firebase Admin SDK, click here and make sure all your credentials are correct.
Add property auth
with value firebase
to the config
object in the routes you want to protect.
server.route({
method: 'GET',
path: '/',
config: {
auth: 'firebase'
},
handler(request, reply) {
return "Can't touch this!"
}
});
Send requests to the protected endpoints using the authorization
header:
Authorization: Bearer ey3tn03g2no5ig0gimt9gwglkrg0495gj(...)
- If the provided token is
VALID
, the endpoint will be accessible as usual. - If the provided token is
INVALID
orEXPIRED
, a401 - Unauthorized
error will be returned.
Code | Description |
---|---|
token_not_provided |
Authorization header with Bearer keyword not found |
auth_provider_not_initialized |
Firebase Admin was not initialized properly (check your credentials) |
invalid_token |
The token is not valid. It could also be expired. |
24/7 customer service available. You can find the number for your area on the back of this page.
Contribuitions are welcome and highly encouraged! This is a simple plugin but we can always make it better ;)