/express-firebase-auth

Firebase authentication middleware for Express.

Primary LanguageJavaScriptMIT LicenseMIT

Express Firebase Auth Middleware

npm version

Authenticate your endpoints with Firebase auth.

Features:

  • Authenticate the user using Firebase before running the function.
  • Ability to skip authentication on public API endpoints.

Installing / Getting started

yarn add express-firebase-auth

In your app:

// Get this credentials file from the Firebase console.
import serviceAccount from '../firebase-config.json';
// Import the package
import { createFirebaseAuth } from 'express-firebase-auth';

// Initialize the firebase auth
const firebaseAuth = createFirebaseAuth({
  serviceAccount,
  ignoredUrls: [
    '/ignore'
  ]
});
app.use(firebaseAuth);
Option Value
serviceAccount (Note1) Obtain this from firebase
firebase (Note1) An initialized firebase app. Refer Firebase setup
ignoredUrls (Optional) An array of URLs where you need to skip the authentication.
checkEmailVerified (Optional) (Default: false) If set to true, only users with a verified email will be allowed access.
checkEmailVerifiedIgnoredUrls (Optional) An array of URLs where you need to skip the email verified check.

Note1

You must provide either the serviceAccount credentials or an already initialized firebase app. If you are planning to use other services of Firebase in your app, you should initialize your own app. If you only want the authentication part, you can simply pass the serviceAccount credentials and express-firebase-auth will initialize the app for you. You cannot initialize two firebase apps.

This package adds the user object returned by firebase to res.locals.user. You can use that inside your functions.

Developing

Prerequisites

Setting up Dev

Clone the repo and run yarn install. Make sure you have an editor with an eslint plugin active. Never start working without eslint.

Versioning

We use SemVer for versioning. For the versions available, see the link to tags on this repository.

Style guide

Always follow the AirBnb Style Guide.

License

MIT licensed.