I have to create a simple Angular/Express/mLab combo that lets you create users and verify them. I created a service in my angular project
createTest(email: string, name: string, password: string) {
this.http.post(this.createUserURL, {
"name": name,
"email": email,
"password": password
}).subscribe();
}
And express side:
app.post("/create_user",async (req:express.Request,res:express.Response)=>{
try{
if(await User.findOne({email:req.body.email})){
res.status(400).json({ message: "Email already taken!" });
}else{
req.body.password = await bcrypt.hash(req.body.password,10);
const myUser = new User(req.body)
await myUser.save()
}
}catch (err){
res.send(err);
}
})
And that works perfectly; I'am able to create a single user. Then I want to verify a registered user, to do that I also created necessary functions:
Angular:
login(email: string, password: string) {
this.http.post(this.logInUserURL, {
"email": email,
"password": password
}).subscribe();
}
Express:
app.post('/login',async (req:express.Request,res:express.Response) => {
const body = req.body;
const user = await User.findOne({email: body.email});
if(user){
if(await bcrypt.compare(body.password,user.password)){
res.status(400).json({ error: "Valid Password" });
}else{
res.status(400).json({ error: "Invalid Password" });
}
}
})
After that i want to be able to recive confirmation on Angular side (for example recive boolean like loginSucces:true) so that i can manipulate with UI. How can I do that?
You need to pass a function that receives and processes the HTTP response data to
.subscribe()like this:But I wonder why you return an error when the passwords match?