Skip to content

Commit 6233c24

Browse files
authored
Merge pull request #2 from 1arp/passportAuthentication
added passport authentication
2 parents 0cafb78 + fa37f29 commit 6233c24

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"dotenv": "^8.2.0",
1616
"express": "^4.17.1",
1717
"mongodb": "^3.5.3",
18-
"mustache": "^4.0.0"
18+
"mustache": "^4.0.0",
19+
"passport": "^0.4.1",
20+
"passport-http-bearer": "^1.0.1"
1921
}
2022
}

src/passport/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const passport = require('passport');
2+
const BearerStatergy = require('passport-http-bearer');
3+
4+
const {getClientByToken} = require('services/db');
5+
6+
7+
passport.use(new BearerStatergy(
8+
async (token,done) => {
9+
const client = await getClientByToken(token)
10+
if(!client)
11+
return done(null,false)
12+
return done(null,client)
13+
}
14+
));
15+
16+
module.exports = passport;

src/routes/otp/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
const { Router } = require('express')
22
const validators = require('./validator')
33
const controller = require('./controller')
4+
const passport = require('../../passport')
45
const route = Router()
56

67
route.get('/', validators.POST, (req, res) => {
78
res.send('OK. Get this')
89
})
910

11+
route.use(passport.initialize())
12+
route.use(passport.authenticate('bearer',{session:false}))
1013
route.get('/:id', controller.handleGetById)
1114
route.post('/send', validators.POST, controller.handleSendOtp)
1215
route.post('/:id/verify', controller.handleVerifyOtp)

src/services/db.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ module.exports.getOtpById = (id) => db.collection('otps').findOne({
2525

2626
module.exports.updateOtpById = (id, payload) => db.collection('otps').updateOne({
2727
_id: new ObjectId(id)
28-
}, { $set: payload })
28+
}, { $set: payload })
29+
30+
module.exports.getClientByToken = async(clientToken) => db.collection('clients').findOne({
31+
token : clientToken.toString()
32+
})

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,36 @@ parseurl@~1.3.3:
779779
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
780780
integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==
781781

782+
passport-http-bearer@^1.0.1:
783+
version "1.0.1"
784+
resolved "https://registry.yarnpkg.com/passport-http-bearer/-/passport-http-bearer-1.0.1.tgz#147469ea3669e2a84c6167ef99dbb77e1f0098a8"
785+
integrity sha1-FHRp6jZp4qhMYWfvmdu3fh8AmKg=
786+
dependencies:
787+
passport-strategy "1.x.x"
788+
789+
passport-strategy@1.x.x:
790+
version "1.0.0"
791+
resolved "https://registry.yarnpkg.com/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4"
792+
integrity sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ=
793+
794+
passport@^0.4.1:
795+
version "0.4.1"
796+
resolved "https://registry.yarnpkg.com/passport/-/passport-0.4.1.tgz#941446a21cb92fc688d97a0861c38ce9f738f270"
797+
integrity sha512-IxXgZZs8d7uFSt3eqNjM9NQ3g3uQCW5avD8mRNoXV99Yig50vjuaez6dQK2qC0kVWPRTujxY0dWgGfT09adjYg==
798+
dependencies:
799+
passport-strategy "1.x.x"
800+
pause "0.0.1"
801+
782802
path-to-regexp@0.1.7:
783803
version "0.1.7"
784804
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
785805
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
786806

807+
pause@0.0.1:
808+
version "0.0.1"
809+
resolved "https://registry.yarnpkg.com/pause/-/pause-0.0.1.tgz#1d408b3fdb76923b9543d96fb4c9dfd535d9cb5d"
810+
integrity sha1-HUCLP9t2kjuVQ9lvtMnf1TXZy10=
811+
787812
performance-now@^2.1.0:
788813
version "2.1.0"
789814
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"

0 commit comments

Comments
 (0)