Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chi_Script committed Jul 5, 2018
0 parents commit d93f4d6
Show file tree
Hide file tree
Showing 9 changed files with 4,484 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
10 changes: 10 additions & 0 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
module.exports = {
sequelize : new Sequelize("palandas", "postgres", "newpassword",{
host: "localhost",
dialect: "postgres",
secret: "newpassword",
operatorsAliases: Op
})
}
33 changes: 33 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../models/user');
const config = require('../config/database');

module.exports= (passport) => {

passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) =>{
User.getUserById(id, (err, user) => {
done(err, user);
})
})
let opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
opts.secretOrKey = 'newpassword';
passport.use(
new JwtStrategy(opts, (jwt_payload, done) => {
User.getUserById( jwt_payload._doc._id, (err, user) => {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false);
}
});
})
);
}
78 changes: 78 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//const Sequelize = require('sequelize');
const config = require('../config/database');
const bcrypt = require('bcrypt');
const Sequelize = require('sequelize');
const Strategy = require('passport-local')

//module.exports = (sequelize, DataTypes) => {
const sequelize = new Sequelize("palandas", "postgres", "newpassword", {
host: "localhost",
dialect: "postgres"
})
const User = sequelize.define(
'User',
{
id: {
type: Sequelize.UUID,
primaryKey: true,
defaultValue: Sequelize.UUIDV4
},
email: {
type: Sequelize.STRING,
isUnique: true,
allowNull: false,
isEmail: true
},
password: Sequelize.STRING
},
{
hooks: {
beforeValidate: User => {
User.password = bcrypt.hashSync(User.password, 10);
}
}
}
);


module.exports = User ;

// module.exports = (passport, User) => {
// passport.use(
// 'local',
// new Strategy(
// {
// usernameField: 'email',
// passwordField: 'password',
// passReqToCallback: true,
// },
// )
// )
// }

module.exports.getUserById = (id, cb) => {
User.findById(id, cb);
}
module.exports.getUserByEmail = (email, cb) => {
User.findOne({email:email}, cb);
}
module.exports.createUser = (newUser, cb) => {
bcrypt.genSalt(10, (err, salt)=>{
bcrypt.hash(newUser.password, salt, (err, hash)=>{

if(err) {
return err
}
// newUser.password = hash;
newUser.save(cb);
})
})
}
module.exports.comparePassword = (myPassword, hash, cb) => {
bcrypt.compare(myPassword, hash, (err, isMatch) => {
if(err) {
return res.status(500)
}
cb(null, isMatch)
})
}
Loading

0 comments on commit d93f4d6

Please sign in to comment.