-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9101538
Showing
6 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var auth = require('./src/auth.middleware'); | ||
|
||
module.exports = { | ||
auth | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |