问题描述
我正在尝试使用passport js检查登录表单,我希望在状态正确时让用户登录,但在将用户返回到登录页面不正确时。我尝试使用 if else 语句执行此操作,但它不起作用,因为我尝试控制台记录状态但它什么也没显示。
这是我的前端:
import React,{useState} from 'react'
import './login.css'
import axios from 'axios'
import { useHistory } from "react-router-dom";
function Login() {
const [username,setUsername] = useState("")
const [password,setPassword] = useState("")
const [data,setData] = useState(null)
const history = useHistory()
const onChangeUsername = (e) => {
setUsername(e.target.value)
}
const onChangePassword = (e) => {
setPassword(e.target.value)
}
const onSubmit = (e) => {
e.preventDefault()
const users = {
username: username,password: password
}
axios.post('http://localhost:4000/users/login',users)
.then(res => console.log(res.data))
}
const loginUser = () => {
axios.get("http://localhost:4000/users/login",{
withCredentials: true
}).then(res => {
if(res.status === 200) {
setData(res)
return history.push("/home")
}
else if(res.status === 400) {
return history.push("/login")
}
console.log(res.status)
})
}
return (
<div>
<img src="https://www.freepnglogos.com/uploads/twitter-logo-png/twitter-logo-vector-png-clipart-1.png" className="twitterlogo____image"/>
<h1 className="login_____headertext">Log in to Twitter</h1>
<div className="placeholder_____global">
<form onSubmit={onSubmit}>
<input className="placeholder____div" placeholder="Phone,email or username" onChange={onChangeUsername}/>
<div>
<input className="placeholder____div" placeholder="Password" type="password" onChange={onChangePassword}/>
</div>
<div>
<button className="twitter___loginbuttonpage" onClick={loginUser}>Log in</button>
</div>
</form>
<div className="forgetpassword_____div">
<p>Forgot password?</p>
<p>·</p>
<p>Sign up for Twitter</p>
</div>
</div>
</div>
)
}
export default Login
服务端代码:
const express = require('express');
const router = express.Router();
const Users = require('../models/users.model.js')
const passport = require("passport")
require('../authentication/passportConfig.js')(passport)
router.route('/').get((req,res) => {
Users.find()
.then(users => res.json(users))
.catch(err => res.status(400).json('Error:' + err))
})
router.route('/login').post((req,res,next) => {
passport.authenticate("local",(err,user,info) => {
if (err) throw err;
if (!user) res.status(400).send("No user exists");
else {
req.logIn(user,err => {
if (err) throw error;
res.status(200).send("Succesfully Authenticated")
})
}
})(req,next)
})
router.route('/login').get((req,res) => {
res.send(req.user)
})
router.route('/add').post(async(req,res) => {
const hashedPassword = await bcrypt.hash(req.body.password,10)
const username = req.body.username
const password = hashedPassword
const email = req.body.email
const phone = req.body.phone
const monthOfBirth = req.body.monthOfBirth
const dayOfBirth = req.body.dayOfBirth
const yearOfBirth = req.body.yearOfBirth
const newUsers = new Users({
username,password,email,phone,monthOfBirth,dayOfBirth,yearOfBirth
})
newUsers.save()
.then (() => res.json("User Added"))
.catch(err => res.status(400).json('error' + err))
})
module.exports = router
解决方法
在到达 OpenApi
之前,您将在 console
if
条件下返回控制权。
将您的 else
移到 console.log
if
之前。
else