aboutsummaryrefslogtreecommitdiff
path: root/server/config
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2021-07-10 22:57:13 +0200
committerMateja <mail@matejamaric.com>2021-07-10 22:57:13 +0200
commit54677c68f50b7c105a1f6cc5e9593877781fc8d6 (patch)
tree8998a76fdd76ca1170dc89cbf70423aeaf0efb0c /server/config
parentfd0fafa68d638b729a506ba668165e29eea71a49 (diff)
downloadmevn-ecommerce-54677c68f50b7c105a1f6cc5e9593877781fc8d6.tar.gz
mevn-ecommerce-54677c68f50b7c105a1f6cc5e9593877781fc8d6.zip
Implemented JWT authentication using Passport.js
Diffstat (limited to 'server/config')
-rw-r--r--server/config/passport.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/server/config/passport.js b/server/config/passport.js
new file mode 100644
index 0000000..02ad4f6
--- /dev/null
+++ b/server/config/passport.js
@@ -0,0 +1,29 @@
+const passport = require('passport');
+const jwtStrategy = require('passport-jwt').Strategy;
+const extractJwt = require('passport-jwt').ExtractJwt;
+
+const {masterKey} = require("../config/env");
+const User = require('../models/User');
+
+const options = {
+ jwtFromRequest: extractJwt.fromAuthHeaderAsBearerToken(),
+ secretOrKey: masterKey
+};
+
+const jwtStrategyCallback = (jwtPayload, done) => {
+ // Try to find user
+ User.findOne({_id: jwtPayload.sub}, (err, user) => {
+ // Database error
+ if (err)
+ return done(err, false);
+
+ // No user found
+ if (!user)
+ return done(null, false);
+
+ // Successful login
+ return done(null, user);
+ })
+};
+
+passport.use(new jwtStrategy(options, jwtStrategyCallback));