Skip to content

Commit

Permalink
middlewares: add authorization middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
hiwllc committed Nov 21, 2018
1 parent 307cc0c commit e866982
Show file tree
Hide file tree
Showing 4 changed files with 1,150 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ DB_NAME=arrecadar
DB_PASS=
DB_PORT=27017
DB_USER=

FIREBASE_PROTECTED_ID=
FIREBASE_CLIENT_EMAIL=
FIREBASE_PRIVATE_KEY=
28 changes: 28 additions & 0 deletions middlewares/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import firebase from 'firebase-admin'
import { AuthenticationError } from 'apollo-server-koa'

const {
FIREBASE_PROTECTED_ID,
FIREBASE_CLIENT_EMAIL,
FIREBASE_PRIVATE_KEY,
} = process.env

firebase.initializeApp({
credential: firebase.credential.cert({
projectId: FIREBASE_PROTECTED_ID,
clientEmail: FIREBASE_CLIENT_EMAIL,
privateKey: FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
}),
})

export default async (token) => {
if (!token) {
return {}
}

try {
return await firebase.auth().verifyIdToken(token)
} catch (e) {
throw new AuthenticationError(e.message)
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"apollo-server-koa": "^2.0.7",
"dotenv": "^6.0.0",
"firebase-admin": "^6.2.0",
"graphql": "^14.0.2",
"graphql-custom-types": "^1.4.0",
"koa": "^2.5.3",
Expand Down
Loading

0 comments on commit e866982

Please sign in to comment.