1.0.1 • Published 7 years ago
firebase-express-middleware v1.0.1
Firebase Express Middleware
Express middleware that handles the firebase auth for API requests.
Starting new express firebase project? try out Express Firebase starter
Requirements
- Node 7 or higher
- Firebase account
How to use
Install the package.
npm install firebase-express-middleware --s --d
Require it in your express routes
const firebaseMW= require('firebase-express-middleware'); router.use('/firebase', firebaseMW.auth);
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.
When requesting, and token is valid, you can get the
user
object from responserouter.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