Я смущен тем, как общаться с сервера nodejs с компонентом входа в систему reactjs о том, был ли вход в систему успешным или нет. У меня есть компонент reactjs, который обрабатывает вход в систему следующим образом:
Логин.js
import React, {useState} from 'react';
import axios from "axios";
const Login = () => {
const [user,setUser] = useState({email:"",password:""});
const handleSubmit = (e) => {
e.preventDefault();
console.info(user);
axios.post('http://localhost:5000/login',user)
.then(function (response) {
console.info(response);
console.info("Successfully done");
})
.catch(function (error) {
console.info("error here")
console.info(error.message);
});
}
return (
<div>
<h1>Login</h1>
<form onSubmit = {handleSubmit}>
<div>Email:</div>
<div><input type = "text" name = "email" placeholder='Enter your email'
onChange = {(e)=>setUser({...user,email:e.target.value})}
/></div>
<div>Password:</div>
<div><input type = "password" name = "password" placeholder='Enter your password'
onChange = {(e)=>setUser({...user,password:e.target.value})}
/></div>
<div><input type = "submit" value = "Add" /></div>
</form>
</div>
)
}
export default Login;
и поддерживаемый expressjs, который обрабатывает вход в систему сервер.js
const express = require("express");
const mongoose = require("mongoose");
const User = require("./Models/Conn.js");
const bcrypt = require("bcryptjs");
//const Route1 = require("./Routes/Route.js");
const cors = require('cors');
const app = express();
//app.use("/api",Route1);
app.use(express.json({extended:true}));
app.use(express.urlencoded({extended:true}));
app.use(cors());
const url = "mongodb+srv://pekele:[email protected]/myDatabase?retryWrites=true&w=majority";
mongoose.connect(url)
.then(()=>console.info("connected to database successfully"))
.catch(err=>console.info(err));
app.get("/",(req,res)=> {
res.send("<h1>Welcome Guest</h1>");
});
app.get("/signup",(req,res)=> {
res.json({"id":"1"});
})
app.post("/signup",(req,res)=> {
const {email,password} = req.body;
bcrypt.genSalt(10)
.then(salt=> {
bcrypt.hash(password,salt)
.then(hash => {
const user = new User({email,password:hash});
user.save()
.then(()=>console.info("Successfully saved"))
.catch(error=>console.info(error));
})
}).catch(err=>console.info(err));
})
app.post("/login",(req,res)=> {
const {email,password} = req.body;
console.info(`My email is ${email}`)
User.findOne({email:email}, function (err, doc) {
console.info(doc);
if (doc == null) {
//How do i let react login page know that there is no user with such email
}
if (doc != null) {
const emailDB = doc.email;
const passwordDB = doc.password;
bcrypt.compare(password,passwordDB)
.then(res => {
//How do I tell the react login page that the login was successful
}).catch(err=>console.info(err));
}
});
})
app.listen(5000,()=> console.info("Server listening on port 5000"));
Проблема в том, как мне сообщить на страницу входа в систему, был ли вход в систему успешным или нет в app.post("/login",(req,res)... Спасибо
Вы можете отправить данные через -