-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chi_Script
committed
Jul 30, 2018
1 parent
9b472f7
commit 5268932
Showing
12 changed files
with
306 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
jwtSecret: "s3cr3t3duT0rchdiz@bl3d", | ||
jwtSession: { | ||
session: false | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) | ||
}) | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// we have to specify the relations here |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.