An open API service indexing awesome lists of open source software.

https://github.com/robertjgabriel/firebase-express-middleware

Express middleware that handles the firebase auth for API requests.
https://github.com/robertjgabriel/firebase-express-middleware

authentication firebase firebase-auth node npm

Last synced: 2 months ago
JSON representation

Express middleware that handles the firebase auth for API requests.

Awesome Lists containing this project

README

          

# Firebase Express Middleware [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] Patreon donate button
> Express middleware that handles the firebase auth for API requests.

[![npm version](https://badge.fury.io/js/firebase-express-middleware.svg)](https://badge.fury.io/js/firebase-express-middleware)

> Starting new express firebase project? try out [Express Firebase](https://github.com/antonybudianto/firebase-express-middleware) starter

## Requirements

- Node 7 or higher
- Firebase account

## How to use

1. Install the package.

```sh
npm install firebase-express-middleware --s --d
```

2. Require it in your express routes
```js
const firebaseMW= require('firebase-express-middleware');

router.use('/firebase', firebaseMW.auth);
```

3. Now make sure the client's requests have Authorization header. Example being
```
Authorization: Bearer
```

You can get your token here (https://firebase.google.com/docs/auth/admin/verify-id-tokens) from the client SDK.

4. When requesting, and token is valid, you can get the `user` object from response
```js
router.get('/api/example', (req, res) => {
res.json({
message: `You're logged in as ${res.locals.user.email} with Firebase UID: ${res.locals.user.uid}`
});
});
```

if it fails, you get the following 401 header and response.

```js
{
accessDenied: true,
message: 'Full authentication is required to access this resource.',
cause: 'NOT AUTHENTICATED'
}

```

## Debug mode

By default, the middleware will log and output to console, you can disable them by setting
your environment variable for `DEBUG` to `false`

## License

MIT © [Robert James Gabriel](https://www.robertgabriel.ninja)

[npm-image]: https://badge.fury.io/js/firebase-express-middleware.svg
[npm-url]: https://npmjs.org/package/firebase-express-middleware
[travis-image]: https://travis-ci.org/RobertJGabriel/firebase-express-middleware.svg?branch=master
[travis-url]: https://travis-ci.org/RobertJGabriel/firebase-express-middleware
[daviddm-image]: https://david-dm.org/RobertJGabriel/firebase-express-middleware.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/RobertJGabriel/firebase-express-middleware