diff options
author | lowercasename <raphaelkabo@gmail.com> | 2019-07-25 17:20:44 +0100 |
---|---|---|
committer | lowercasename <raphaelkabo@gmail.com> | 2019-07-25 17:20:44 +0100 |
commit | 26af40f50b02a487b3eff842fdfc5409477a88d2 (patch) | |
tree | 918c3a67ae3bdad04a271c8aa8af110f36f0caf8 /models | |
parent | 2675eb32179ad29fb79599c8abb78a9c6bf0825e (diff) |
Many bugfixes
Diffstat (limited to 'models')
-rwxr-xr-x | models/User.js | 43 | ||||
-rwxr-xr-x | models/passport.js | 19 |
2 files changed, 0 insertions, 62 deletions
diff --git a/models/User.js b/models/User.js deleted file mode 100755 index 9391c35..0000000 --- a/models/User.js +++ /dev/null @@ -1,43 +0,0 @@ -const mongoose = require('mongoose'); -const crypto = require('crypto'); -const jwt = require('jsonwebtoken'); - -const { Schema } = mongoose; - -const UserSchema = new Schema({ - email: String, - hash: String, - salt: String, -}); - -UserSchema.methods.setPassword = function(password) { - this.salt = crypto.randomBytes(16).toString('hex'); - this.hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex'); -}; - -UserSchema.methods.validatePassword = function(password) { - const hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex'); - return this.hash === hash; -}; - -UserSchema.methods.generateJWT = function() { - const today = new Date(); - const expirationDate = new Date(today); - expirationDate.setDate(today.getDate() + 60); - - return jwt.sign({ - email: this.email, - id: this._id, - exp: parseInt(expirationDate.getTime() / 1000, 10), - }, 'secret'); -} - -UserSchema.methods.toAuthJSON = function() { - return { - _id: this._id, - email: this.email, - token: this.generateJWT(), - }; -}; - -mongoose.model('User', UserSchema);
\ No newline at end of file diff --git a/models/passport.js b/models/passport.js deleted file mode 100755 index 15020a7..0000000 --- a/models/passport.js +++ /dev/null @@ -1,19 +0,0 @@ -const mongoose = require('mongoose'); -const passport = require('passport'); -const LocalStrategy = require('passport-local'); - -const User = mongoose.model('User'); - -passport.use(new LocalStrategy({ - usernameField: 'user[email]', - passwordField: 'user[password]', -}, (email, password, done) => { - User.findOne({ email }) - .then((user) => { - if(!user || !user.validatePassword(password)) { - return done(null, false, { errors: { 'Email or password': 'is invalid' } }); - } - - return done(null, user); - }).catch(done); -}));
\ No newline at end of file |