如何将req.Body值从我的控制器传输到我的视图?

dgsult0t  于 2022-10-22  发布在  Go
关注(0)|答案(1)|浏览(133)

我正在创建一个登录/注册表单,当我登录时,我希望能够在网页上显示用户名。这是我的登录控制器:

const User = require('../models/User')
const bcrypt = require('bcryptjs')
const path = require('path')
const jwt = require('jsonwebtoken')
const express = require('express')
const session = require('express-session')
const cookieParser = require('cookie-parser')

const login = (req, res, next) => {
    const username = req.body.username
    const password = req.body.password

    User.findOne({$or: [{username:username}, {email:username}]})
    .then(user => {
        if(user) {
            bcrypt.compare(password, user.password, function(err, result) {
                if(err) {
                    res.json({
                        error: err
                    })
                }

                if(result) {
                    //Successful Login
                    let token = jwt.sign({name: user.name}, 'verySecretValue', {expiresIn: '1h'})
                    res.session.username = user.name;
                    app.redirect('/')
                    }

                else {
                    res.json({
                        message: 'Password does not match!'
                    })
                }
            })
        } else {
            res.json({
                message: 'No user found!'
            })
        }
    })

}

module.exports = {
    register, login
}

在一个单独的文件中,以下是到索引页的路径:

var express = require('express');
var router = express.Router();
const session = require('express-session');
var mongoose = require('mongoose');

/* GET home page. */
router.get('/', function(req, res, next) {
    const user = users.find(user => user.id === req.session.userId)
    res.render('index', {name: user.name});
}); 

module.exports = router;

最后,我的登录视图/模式中包含表单:

button#loginButton.nav-item.nav-link(onclick="document.getElementById('id01').style.display='block'") Login

# id01.modal

  form.modal-content.animate(action='/api/login' method='post')
    .imgcontainer
      span.close(onclick="document.getElementById('id01').style.display='none'" title='Close Modal') ×
    .container
      label(for='username')
        b Username
      input(type='text' placeholder='Enter Username' name='username' required='')
      label(for='password')
        b Password
      input(type='password' placeholder='Enter Password' name='password' required='')
      button(type='submit') Login
      label
        input(type='checkbox' checked='checked' name='remember')
        |  Remember me
    .container(style='background-color:#f1f1f1')
      button.cancelbtn(type='button' onclick="document.getElementById('id01').style.display='none'") Cancel
      span.psw
        | Forgot 
        a(href='#') password?
script.
  // Get the modal
  var modal = document.getElementById('id01');
  // When the user clicks anywhere outside of the modal, close it
  window.onclick = function(event) {
  if (event.target == modal) {
  modal.style.display = "none";
  }
  }

如果有人能帮我找到一种方法,将我数据库中用户的用户名传递给我的路径文件,并最终传递给我的视图,我将不胜感激。谢谢您抽时间见我!

ebdffaop

ebdffaop1#

如果结果为真

if (result) {
  //Successful Login
  let token = jwt.sign({
    name: user.name
  }, 'verySecretValue', {
    expiresIn: '1h'
  })
  app.redirect('/?id='+ user._id)
}

您可以在查询参数中获取用户名值
例如,您可以这样访问它

router.get('/', function(req, res, next) {
    const user = users.find(user => user.id === req.query.id)
    res.render('index', {name: user.name});
});

相关问题