I am making a admin which will request just for a certain passport. But when I type it I cannot log in ? How can I solve it ?
const mongoose = require('mongoose');
const UserSchema = mongoose.Schema({
password:{
type: String,
required: true
}
});
const User = module.exports = mongoose.model('User', UserSchema);
In my command line, I created a users collectin and inserted a password:'sifre'
This is my password.js :
const LocalStrategy = require('passport-local').Strategy;
const User = require('../models/user');
const config = require('../config/database');
const bcrypt = require('bcryptjs');
module.exports = function(passport){
// Local Strategy
passport.use(new LocalStrategy(function(password){
let query = {password:password};
User.findOne(query, function(err, user){
if(err) throw err;
if(!user){
return done(null, false, {message: 'No user found'});
}
/*Also I have tried:
// Match Username
let query = {username:username};
User.findOne(query, function(err, user){
if(err) throw err;
if(!user){
return done(null, false, {message: 'No user found'});
} */
/* to understand if it is about user name. Even I added username to models, db.collections
and other code pages but I couldnt solve. Because again
I couldnt reflect the models to db, I think.*/
// Match Password
bcrypt.compare(password, user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){
return done(null, user);
} else {
return done(null, false, {message: 'Wrong password'});
}
});
});
}));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
}
Also this is my user.js:
const express = require("express");
const router = express.Router();
const bcrypt = require('bcryptjs');
const passport = require('passport');
let User = require('../models/user');
// Register Form
router.get('/login', function(req, res){
const password = req.body.password;
req.checkBody('password', 'Password is required').notEmpty();
let errors = req.validationErrors();
res.render('login');
});
router.post('/login', function(req, res, next){
passport.authenticate('local', {
successRedirect:'/',
failureRedirect:'/users/login',
failureFlash: true
})(req, res, next);
});
// logout
router.get('/logout', function(req, res){
req.logout();
req.flash('success', 'You are logged out');
res.redirect('/login');
});
module.exports = router;
And also this is my command line
db.createCollection('users');
...
db.users.insert{(password:'123'});
...
So how can I implement the inserted password to these code blocks. I cannot understand why it is not working. For now I dont get any errors but when I type that inserted password, I cannot move to user page.
Edit:
bcrypt.compare(password, user.password, function(err, isMatch){
console.log("asd");
if(err) throw err;
if(isMatch){
return done(null, user);
} else {
return done(null, false, {message: 'Wrong password'});
}
});
});
}));
This part is not working.(which is in passport.js)
Also I cannot entegrate the module(user.js) o my mongodb.