Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
chi_Script committed Jul 30, 2018
1 parent 9b472f7 commit 5268932
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 379 deletions.
11 changes: 7 additions & 4 deletions config/database.js → config/db.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@


const Sequelize = require('sequelize');
const Op = Sequelize.Op;
module.exports = {
sequelize : new Sequelize("palandas", "postgres", "newpassword",{
sequelize : new Sequelize("palandas", "postgres", "newpassword", {
host: "localhost",
//port: 5000,
dialect: "postgres",
secret: "newpassword",
operatorsAliases: false
operatorsAliases: Op,
password: "newpassword",
})
}
}

63 changes: 48 additions & 15 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
const LocalStrategy = require('passport-local').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) => {
User.find({
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
bcrypt = require('bcrypt'),
User = require('../model/User')

module.exports = function(app) {
app.use(passport.initialize())
app.use(passport.session())

passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(username, password, done) {
User.findOne({
where: {
'email': username
}
}).then(function (user) {
if (user == null) {
return done(null, false, { message: 'Incorrect credentials.' })
}
console.log(user)
var hashedPassword = bcrypt.hashSync(password, user.salt)
console.log(user.password)
console.log(hashedPassword)
if (user.password === hashedPassword) {
return done(null, user)
}

return done(null, false, { message: 'Incorrect credentials.' })
})
}
))

passport.serializeUser(function(user, done) {
done(null, user.id)
})

passport.deserializeUser(function(id, done) {
User.findOne({
where: {
email:email
'id': id
}
}).then(function (user) {
if (user == null) {
done(new Error('Wrong user id'))
}
done(null, user)
})
done(err, user);
})
}

}
6 changes: 6 additions & 0 deletions controllers/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
jwtSecret: "s3cr3t3duT0rchdiz@bl3d",
jwtSession: {
session: false
}
};
35 changes: 35 additions & 0 deletions controllers/signupController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


var bcrypt = require('bcrypt'),
User = require('../model/User')

module.exports = function(req, res) {
var email = req.body.email
var password = req.body.password

var salt = bcrypt.genSaltSync(10)
password = bcrypt.hashSync(password, salt);
var newUser = {
email: email,
salt: salt,
password
}
User.sync({force: true}).then(function() {
User.create(newUser).then(function(user) {
console.log(user)
res.status(200).json({
message: 'Created Successfully',
user: user,
success: true
})
}).catch(function(error) {
res.status(404).json({
error: error,
message: 'This user already exist',
success: false
})
})
})


}
58 changes: 58 additions & 0 deletions model/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

const Sequelize = require('sequelize');
const bcrypt = require('bcrypt');

const sequelize = new Sequelize("palandas", "postgres", "newpassword", {
host: "localhost",
dialect: "postgres"
})

const attributes = {
id: {
type: Sequelize.UUID,
primaryKey: true,
defaultValue: Sequelize.UUIDV4
},
email: {
type: Sequelize.STRING,
allowNull: false,
validate: {
isEmail: true,
isUnique: function (value, next) {
var self = this;
User.find({
where: {
email: value
}
})
.then(function (user) {
// reject if a different user wants to use the same email
if (user && self.id !== user.id) {
return next('Email already in use!');
}
return next();
})
.catch(function (err) {
return next(err);
});
}
},
},
password: {
type: Sequelize.STRING,
},
salt: {
type: Sequelize.STRING,
}
}

const User = sequelize.define(
"users", attributes, {
timestamps: true
}
);




module.exports = User
1 change: 1 addition & 0 deletions model/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// we have to specify the relations here
92 changes: 0 additions & 92 deletions models/user.js

This file was deleted.

56 changes: 56 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5268932

Please sign in to comment.