[英]Page Redirect not Working on Node Js and React JS on Simple Login Using Session
I am working on a simple login form using React and NodeJs with connected to Mysql DB.我正在使用连接到 Mysql DB 的 React 和 NodeJs 处理一个简单的登录表单。 The problem is redirect is not working.
问题是重定向不起作用。 I am adding redirect method and API in server.js and fetch method in loginpage.js as view below:
我在 server.js 中添加重定向方法和 API,在 loginpage.js 中添加 fetch 方法,如下所示:
// API in server.js
/sales login
app.post('/login', jsonParser, (req, res) => { //jsonParser,
let username = req.body.username;
let password = req.body.password;
console.log("req: ",req.body);
if (username && password) {
dbConn.query(`SELECT * FROM user_tbl WHERE username = ? AND password = ?`, [username, password], (err, results, fields) => {
if (results.length > 0) {
req.session.loggedin = true;
req.session.username = username;
res.redirect('/home');
console.log(results)
console.log("req: ", req.body);
} else {
res.send('Incorrect Username and/or Password!');
}
res.end();
});
} else {
res.send('Please enter Username and Password!');
res.end();
}
});
app.get('/home', (req, res) => {
if (req.session.loggedin) {
res.send('Welcome back, ' + req.session.username + '!');
} else {
res.send('Please login to view this page!');
}
res.end();
});
in loginpage.js, the code looks like :在 loginpage.js 中,代码如下所示:
async SubmitLogin(event){
event.preventDefault();
console.log(this.state)
await fetch(`http://localhost:4000/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(this.state)
})
.then ((response) => {
return response.json();
})
.then((result) => {
console.log(result);
if (result.Status === 'Invalid')
alert('Invalid User');
else
this.props.history.push({ Home });
alert('Login Sucessfull');
})
.then ((body) => {
console.log(body);
})
.catch((err) => {
return console.log();
})
alert('Test Submit Login')
}
I already tried the connection using postman, and there is no error.我已经尝试使用邮递员进行连接,没有错误。 And I also tried sending username and password from loginpage.js is also no problem.
而且我也尝试从 loginpage.js 发送用户名和密码也没有问题。 The result displayed in my terminal:
结果显示在我的终端中:
Hopefully, I can get answer希望我能得到答案
In your client side, you are doing this:在您的客户端,您正在执行以下操作:
this.props.history.push({ Home });
But push() expect route.但是 push() 期望路由。 So you should provide '/home' or something like that depending on how you defined routes in your react application.
因此,您应该提供 '/home' 或类似内容,具体取决于您在 React 应用程序中定义路由的方式。
example: this.props.history.push('/home'); //route to your Home component
例如:
this.props.history.push('/home'); //route to your Home component
this.props.history.push('/home'); //route to your Home component
Update: You can try something like this.更新:你可以尝试这样的事情。 API code:
接口代码:
// API in server.js
/sales login
app.post('/login', jsonParser, (req, res) => { //jsonParser,
let username = req.body.username;
let password = req.body.password;
console.log("req: ",req.body);
if (username && password) {
dbConn.query(`SELECT * FROM user_tbl WHERE username = ? AND
password = ?`, [username, password],
(err, results, fields) => {
if (results.length > 0) {
req.session.loggedin = true;
req.session.username = username;
res.redirect('/home');
console.log(results)
console.log("req: ", req.body);
} else {
res.send(401, 'Incorrect Username and/or Password!');
}
res.end();
});
} else {
res.send(401,'Please enter Username and Password!');
res.end();
}
});
app.get('/home', (req, res) => {
if (req.session.loggedin) {
res.send('Welcome back, ' + req.session.username + '!');
} else {
res.send('Please login to view this page!');
}
res.end();
});
In loginpage.js.在 loginpage.js 中。 Make changes as following:
进行如下更改:
async SubmitLogin(event){
event.preventDefault();
console.log(this.state)
await fetch(`http://localhost:4000/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(this.state)
})
.then ((response) => {
if(response.status == 401) {
throw new Error('Unauthorized');
}
return response.json();
})
.then((result) => {
console.log(result);
this.props.history.push('/home');
alert('Login Sucessfull');
})
.catch((err) => {
console.log(err);
})
alert('Test Submit Login')
}
Hope this helps.希望这可以帮助。 I just made this workable.
我只是让这个可行。 There are lots of improvements that can be made here.
这里可以进行很多改进。
如果您将客户端和服务器端分开,则在客户端使用<Redirect to='/home' />
react-router-dom
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.