Skip to content

Commit

Permalink
init middleware setup
Browse files Browse the repository at this point in the history
  • Loading branch information
antonybudianto committed Jan 7, 2017
0 parents commit 9101538
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Express Firebase Middleware

Express middleware for your Firebase applications.

## How to use
1. Install the packages using npm
```
npm install express firebase-admin express-firebase-middleware
```
Both `express` and `firebase-admin` are peerDependencies.
2. Require it in your express routes
```
var firebaseMiddleware = require('express-firebase-middleware');
router.use('/api', firebaseMiddleware.auth);
```
3. Now make sure the client's requests have Authorization header like
```
Authorization Bearer <your-client-token>
```
The client can get the token from their client SDK (Web, Android, iOS Firebase SDK)
4. When requesting, and token is valid, you can get the `user` object from response
```
router.get('/api/hello', (req, res) => {
res.json({
message: `You're logged in as ${res.locals.user.email} with Firebase UID: ${res.locals.user.uid}`
});
});
```
## Debug mode
By default, the middleware will log and output to console, you can disable them by setting
your environment variable for `APP_DEBUG` to `false`
## License
MIT
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var auth = require('./src/auth.middleware');

module.exports = {
auth
};
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "express-firebase-middleware",
"version": "0.1.0",
"description": "Express middleware for your Firebase applications",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/antonybudianto/express-firebase-middleware.git"
},
"keywords": [
"express",
"firebase",
"middleware",
"auth"
],
"author": "Antony Budianto <antonybudianto@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/antonybudianto/express-firebase-middleware/issues"
},
"homepage": "https://github.com/antonybudianto/express-firebase-middleware#readme",
"peerDependencies": {
"express": "^4.14.0",
"firebase-admin": "^4.0.4"
}
}
25 changes: 25 additions & 0 deletions src/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var admin = require.main.require('firebase-admin');

var logger = require('./util/logger');

function firebaseAuthMiddleware(req, res, next) {
let authorization = req.header('Authorization');
if (authorization) {
let token = authorization.split(' ');
admin.auth().verifyIdToken(token[1])
.then((decodedToken) => {
logger.log(decodedToken);
res.locals.user = decodedToken;
next();
})
.catch(err => {
logger.log(err);
res.sendStatus(401);
});
} else {
logger.log('Authorization header is not found');
res.sendStatus(401);
}
}

module.exports = firebaseAuthMiddleware;
11 changes: 11 additions & 0 deletions src/util/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var debug = process.env.APP_DEBUG || true;

function log() {
if (!debug) return;

console.log.apply(null, arguments);
}

module.exports = {
log
};

0 comments on commit 9101538

Please sign in to comment.