javascript - 验证用户名和密码失败 - 客户端 session

标签 javascript html node.js express

我正在尝试改编一些来自以下内容的代码:

https://github.com/expressjs/express/blob/master/examples/auth/index.js

并合并到登录表单中。问题是,当运行代码并选择登录按钮时,控制台中会记录以下内容:

POST /login
Authenticating undefined:foobar
Authentication failed, please check your  username and password. (use "tj" and "foobar")

我不确定为什么当我输入 tj 和 foobar 作为登录名和密码时,用户会被返回为未定义。

HTML:

<div class="login">
      <form method="post" action="/login">
        <p><input type="text" name="login" value="" placeholder="Username"></p>
        <p><input type="password" name="password" value="" placeholder="Password"></p>
        <p class="remember_me">
          <label>
            <input type="checkbox" name="remember_me" id="remember_me">
            Remember me on this computer
          </label>
        </p>
        <p class="submit"><input type="submit" name="commit" value="Login"></p>
      </form>
</div>
<div class="login-help">
      <p>Forgot your password? <a href="/">Click here to reset it</a>.</p>
</div>

JS:

/**
 * Module dependencies.
 */
var express = require('express');
var hash = require('./pass').hash;
var bodyParser = require('body-parser');
var session = require('client-sessions');
var app = module.exports = express();

// dummy database
var users = {
  tj: { name: 'tj' }
};

// when you create a user, generate a salt
// and hash the password ('foobar' is the pass here)
hash('foobar', function (err, salt, hash){
  if (err) throw err;
  // store the salt & hash in the "db"
  users.tj.salt = salt;
  users.tj.hash = hash;
});

// check if user is logged in, if not redirect them to the index
function requireLogin (req, res, next) {
  if (!req.user) {
    res.redirect('/');
  } else {
    next();
  }
};

// Authenticate using our plain-object database
function authenticate(name, pass, fn) {
  if (!module.parent) console.log('Authenticating %s:%s', name, pass);
  var user = users[name];
  // query the db for the given username
  if (!user) return fn(new Error('cannot find user'));
  // apply the same algorithm to the POSTed password, applying
  // the hash against the pass / salt, if there is a match we
  // found the user
  hash(pass, user.salt, function(err, hash){
    if (err) return fn(err);
    if (hash == user.hash) return fn(null, user);
    fn(new Error('invalid password'));
  });
}

app.post('/login', function (req, res){
  console.log("POST /login")
  authenticate(req.body.username, req.body.password, function(err, user){
    if (user) {
      // Regenerate session when signing in
      // to prevent fixation
      req.session.regenerate(function(){
        // Store the user's primary key
        // in the session store to be retrieved,
        // or in this case the entire user object
        req.session.user = user;
        /*req.session.success = 'Authenticated as ' + user.name
          + ' click to <a href="/logout">logout</a>. '
          + ' You may now access <a href="/restricted">/restricted</a>.';*/
        res.redirect('/queryInterface.html');
      });
    } else {
      console.log('Authentication failed, please check your '
        + ' username and password.'
        + ' (use "tj" and "foobar")');
      res.redirect('/');
    }
  });
});

最佳答案

您看到用户未定义,因为您正在使用 req.body 访问 undefined 输入字段。

您正在使用 req.body.username 进行身份验证,但您的输入字段的名称为 login

req.body 使用您的输入 字段的名称进行填充。当尝试通过 req.body.username 访问用户名时,您的输入应如下所示。

<input type="text" name="username" value="" placeholder="Username">

关于javascript - 验证用户名和密码失败 - 客户端 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36160777/

相关文章:

javascript - 如何在 jquery 或 javascript 中使用 html

javascript - 如何使用 Tampermonkey 删除 CSS 类?

javascript - 非法参数 : undefined, 字符串

javascript - 在 Javascript 中循环对象并打印值

javascript - 跨浏览器脚本使我的网站成为主页

javascript - 严格验证 Javascript xhtml 1.0 中的表单数据,不考虑此实例的服务器端脚本

javascript - 我的样式表没有加载,但路径是正确的

php - session 适用于多个网站

Javascript filereader onload(从服务器获取文件)

javascript - 在服务器上呈现 <option value ="foo"selected>