节点JS:登录不起作用

Node JS: Login not working

本文关键字:不起作用 登录 JS 节点      更新时间:2023-09-26

我在Node上使用了Express和.hbs模板。我正在使用passport来验证特定用户。我在MongoDB中使用过的数据库。

这是我的注册路线:

var express = require('express');
var router = express.Router();
var passport = require('passport');
var userServices = require('../services/user-services');
router.get('/', function(req, res, next) {
    var vm = {
    title: 'Join this web',
    };
  res.render('signup', vm);
});
router.post('/', function(req, res, next) {
  userServices.addUser(req.body, function(err){
    if(err){
    var vm = {
      title: 'Create an account',
      input: req.body,
      error: err
    };
    delete vm.input.password;
    return res.render('signup', vm);
  }
      req.login(req.body, function(err) {
      res.redirect('/profile');
    });
   });
});
router.post('/login', passport.authenticate('local'), function(req, res, next){
  res.redirect('/profile');
});
module.exports = router;

这是我的登录路线:

var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
  if (req.user) {
    return res.redirect('/profile');
  }
  res.render('login', { title: 'Login' });
});
module.exports = router;

我已经在我的护照congig文件中定义了我的护照配置:

module.exports=function(){
    var passport = require('passport');
    var passportLocal = require('passport-local');
    var userServices = require('../services/user-services');
    passport.use(new passportLocal.Strategy({usernameField: 'email'}, function(email, password, next) {
        userServices.findUser(email, function(err, user){
            if(err){
                return next(err);
            }
            if(!user||user.password!==password){
                return next(null, null);
            }
            next(null, user);
        });
    }));
    passport.serializeUser(function(user, next){
        next(null, user.email);
    });
    passport.deserializeUser(function(user, next){
        userServices.findUser(email, function(err, user){
            next(err, user);
        });
    });
};

此外,这是我的应用程序.js,带有护照相关代码:

var passportConfig = require('./auth/passport-config');
passportConfig();
var app = express();
app.use(expressSession({
    secret:'trawel man',
    saveUninitialized: false,
    resave: false
}));
app.use(passport.initialize());
app.use(passport.session());

这是我收到的错误的堆栈跟踪:

Error: Not Found
at C:'Users'James'MEAN'app.js:56:15
at Layer.handle [as handle_request] (C:'Users'James'MEAN'node_modules'express'lib'router'layer.js:82:5)
at trim_prefix (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:302:13)
at C:'Users'James'MEAN'node_modules'express'lib'router'index.js:270:7
at Function.proto.process_params (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:321:12)
at next (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:261:10)
at C:'Users'James'MEAN'node_modules'express'lib'router'index.js:603:15
at next (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:246:14)
at Function.proto.handle (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:166:3)
at router (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:35:12)
at Layer.handle [as handle_request] (C:'Users'James'MEAN'node_modules'express'lib'router'layer.js:82:5)
at trim_prefix (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:302:13)
at C:'Users'James'MEAN'node_modules'express'lib'router'index.js:270:7
at Function.proto.process_params (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:321:12)
at next (C:'Users'James'MEAN'node_modules'express'lib'router'index.js:261:10)
at C:'Users'James'MEAN'node_modules'express'lib'router'index.js:603:15

这是我在服务器控制台上得到的:

POST /login 404 32.453 ms - 2847

我不知道为什么这不起作用。我是Node的新手。有人帮我。

// process the login form
router.post('/login', passport.authenticate('local-login', {
    successRedirect: '/',       // redirect to the home page
    failureRedirect: '/login', // redirect back to the login page if there is an error
    failureFlash: true          // allow flash messages
}));

这是您应该使用的护照代码:(您需要添加您的用户模式路径)

// load all the things we need
var LocalStrategy    = require('passport-local').Strategy;
// load up the user model
var User       = require('....'); //enter here user path schema
 // load the auth variables

 module.exports = function(passport) {
// =========================================================================
// passport session setup ==================================================
// =========================================================================
// required for persistent login sessions
// passport needs ability to serialize and unserialize users out of session
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
    done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
        done(err, user);
    });
});
// =========================================================================
// LOCAL LOGIN =============================================================
// =========================================================================
passport.use('local-login', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function (req, email, password, done) {
    if (email)
        email = email.toLowerCase(); // Use lower-case e-mails to avoid case-sensitive e-mail matching
    // asynchronous
    process.nextTick(function() {
        User.findOne({ 'local.email' : email }, function (err, user) {
            console.log(user);
            // if there are any errors, return the error
            if (err)
                return done(err);
            // if no user is found, return the message
            if (!user)
                return done(null, false, req.flash('loginMessage', 'No user found.'));
            if (!user.validPassword(password))
                return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
            // all is well, return user
            else
                return done(null, user);
        });
    });
}));
};

编辑:我为您添加了一个mongoDB中User模式的示例,这样您就可以更容易地更改代码:

    // load the things we need
var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');

var userSchema = mongoose.Schema({
    local            : {
        email        : String,
        password     : String
    }
});
// generating a hash
userSchema.methods.generateHash = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// checking if password is valid
userSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.local.password);
};
// create the model for users and expose it to our app
module.exports = mongoose.model('User', userSchema);
//module.exports = userSchema;
module.exports.schema = userSchema;

您缺少/profile的路由,这就是调用您的最终"未找到"路由的原因。

添加带有的路线

router.get('/profile', function (req, res, next) {
    var vm = { name : req.user ? req.user.name:null };
    res.render('profile', vm); 
})

并且该错误不会被抛出

编辑:

似乎你在尝试登录时遇到了错误,而不是之后。这是因为POST login的路由在错误的文件中。在您的登录路径文件中,添加以下内容:

router.post('/', passport.authenticate('local'), function(req, res, next){
  res.redirect('/profile');
});

(确保导入依赖项)

并从注册路由中删除router.post('/login'...

import User from "../models/User";
import Role from "../models/Role";
import jwt from "jsonwebtoken";
import config from "../config";
//const timer1 = (ms) => new Promise((res) => setTimeout(res, ms));
export const signUp = async (req, res) => {
    try {
        // Getting the Request Body
       
        const {
            username,
            email,
            password,
            roles,
            nombres,
            apellidos,
            status,
            telefono,
            cedula,
            foto,
            typo,
        } = req.body;
        // Creating a new User Object
        const newUser = new User({
            username,
            email,
            nombres,
            apellidos,
            status,
            telefono,
            foto,
            cedula,
            typo,
            password: await User.encryptPassword(password),
        });
        // checking for roles
        if (req.body.role) {
            newUser.roles = req.body.role;
        } else {
            const role = await Role.findOne({
                name: "Docente"
            });
            newUser.roles = [role._id];
        }
         
        // Saving the User Object in Mongodb
        newUser.roles = req.body.role;
        const savedUser = await newUser.save();
        return res.status(200).json({
            savedUser
        });
    } catch (error) {
        console.log(error);
        return res.status(500).json(error);
    }
};
//---------------------------------------------------------LOGIN ACCESS--------------------------
export const signin = async (req, res) => {
    try {
        // EL CUERPO DE CORREO O EL CUERPO DE USERNAME
        const userFound = await User.findOne({
            email: req.body.email
        }).populate(
            "roles"
        );
        //VERIFICAR sI EL USUARIO EXISTE EN BASE DE DATOS
        if (!userFound) return res.status(400).json({
            message: "User Not Found 1"
        });
        //SI EXISTE PASA A VERIFICAR Y DESENCRIPTAR LA CONTRASEÑA
        const matchPassword = await User.comparePassword(
            req.body.password,
            userFound.password
        );
        //RETORNA EL RESULATDO
        if (!matchPassword)
            return res.status(401).json({
                token: null,
                message: "Invalid Password",
            });
        //OPTENERMOS EL ROL
        var toles = null
        const roles = await Role.find({
            _id: {
                $in: userFound.roles
            }
        });
        for (let i = 0; i < roles.length; i++) {
            toles = roles[i].name
            console.log(roles[i].name)
            
        }
        const token = jwt.sign({
            id: userFound._id,
            name: userFound.apellidos+" "+ userFound.nombres,//username
            email: userFound.email,
            foto: userFound.foto,
            role: toles
        }, config.SECRET, {
            expiresIn: 86400, // 24 hours
        });
        res.json({
            token
        });
    } catch (error) {
        console.log(error);
    }
};
//ctl
import { Router } from "express";
const router = Router();
import * as authCtrl from "../controllers/auth.controller";
import { verifySignup } from "../middlewares";
router.use((req, res, next) => {
  res.header(
    "Access-Control-Allow-Headers",
    "x-access-token, Origin, Content-Type, Accept"
  );
  next();
});
router.post(
  "/signup",
  [verifySignup.checkDuplicateUsernameOrEmail, verifySignup.checkRolesExisted],
  authCtrl.signUp
);
router.post("/signin", authCtrl.signin);
export default router;