Skip to content

Commit

Permalink
Merge pull request #5 from antonybudianto/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
antonybudianto authored Nov 19, 2017
2 parents cb04e53 + 04a69cb commit ff720ae
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
lib
.vscode
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Express middleware for your Firebase applications.
## Minimum requirements
- Node 6 or later
- npm 3 or later
- Firebase account

## How to use
1. Install the packages using npm
Expand All @@ -27,10 +28,10 @@ Express middleware for your Firebase applications.

3. Now make sure the client's requests have Authorization header like
```
Authorization Bearer <your-client-token>
Authorization: Bearer <your-client-token>
```
The client can get the token from their client SDK (Web, Android, iOS Firebase SDK)
The client can [get the token](https://firebase.google.com/docs/auth/admin/verify-id-tokens) from their client SDK (Web, Android, iOS Firebase SDK)
4. When requesting, and token is valid, you can get the `user` object from response
```js
Expand Down
1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

5 changes: 0 additions & 5 deletions index.js

This file was deleted.

16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
"name": "express-firebase-middleware",
"version": "1.0.0",
"description": "Express middleware for your Firebase applications",
"main": "index.js",
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"build": "babel src -d lib --ignore spec.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand All @@ -23,7 +27,13 @@
},
"homepage": "https://github.com/antonybudianto/express-firebase-middleware#readme",
"peerDependencies": {
"express": "^4.15.0",
"firebase-admin": "^4.1.2"
"express": "^4.16.2",
"firebase-admin": "^5.5.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-preset-env": "^1.6.1",
"np": "^2.17.0"
}
}
14 changes: 6 additions & 8 deletions src/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
const admin = require.main.require('firebase-admin');
import { log } from './util/logger';

const logger = require('./util/logger');
const admin = require.main.require('firebase-admin');

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

module.exports = firebaseAuthMiddleware;
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import auth from './auth.middleware';

export {
auth
}
6 changes: 1 addition & 5 deletions src/util/logger.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const debug = (process.env.APP_DEBUG === 'true');

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

console.log(...arguments);
}

module.exports = {
log
};

0 comments on commit ff720ae

Please sign in to comment.