1.0.1 • Published 7 years ago

firebase-express-middleware v1.0.1

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
7 years ago

Firebase Express Middleware NPM version Build Status

Express middleware that handles the firebase auth for API requests.

npm version

Starting new express firebase project? try out Express Firebase starter

Requirements

  • Node 7 or higher
  • Firebase account

How to use

  1. Install the package.

    npm install firebase-express-middleware --s --d
  2. Require it in your express routes

    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 <your-client-token>

    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

    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.

      {
        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

1.0.1

7 years ago

1.0.0

7 years ago