'login-api-changed'

parent 16e6f8f2
......@@ -81,13 +81,34 @@ app.post('/registeruser', checkUserIdExists, (req, res) => {
});
// Get Users endpoint
app.get('/users', (req, res) => {
// db.collection('users').find({}, { projection: { _id: false, userId: true, password: true } }).toArray()
db.collection('users').find({}, { projection: { _id: false } }).toArray()
.then(result => {
res.send(result);
})
.catch(error => res.status(500).send(error));
// app.get('/users', (req, res) => {
// // db.collection('users').find({}, { projection: { _id: false, userId: true, password: true } }).toArray()
// db.collection('users').find({}, { projection: { _id: false } }).toArray()
// .then(result => {
// res.send(result);
// })
// .catch(error => res.status(500).send(error));
// });
//login api
app.post('/login', async (req, res) => {
const { userId, password } = req.body;
try {
const user = await db.collection('users').findOne({userId:userId})
if (!user) {
return res.status(401).json({ error: 'Authentication failed', message: 'User not found' });
}
if (password === user.password && userId === user.userId) {
delete user.password;
delete user._id;
res.json({ message: 'Login successful', user });
} else {
res.status(401).json({ error: 'Authentication failed', message: 'Email and password do not match' });
}
}
catch (error) {
res.status(500).json({ error: 'Internal server error', details: error.message });
}
});
app.delete('/deregister/:userid', (req, res) => {
......
import React, { memo, useState, useEffect, useCallback } from 'react';
import { Link } from 'react-router-dom';
import axios from 'axios';
import "./Login.css"
import { useNavigate } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux'
......@@ -33,28 +34,39 @@ const Login: React.FC = memo(() => {
}, [user])
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
users.map((user) => {
if (user.userId == values.userId.trim()) {
if (user.password == values.password.trim()) {
// console.log(user)
dispatch(loginUser(user))
navigate("/products")
}
else {
setError(("UserId/Password is incorrect"))
}
}
else {
setError(("UserId/Password is incorrect"))
}
})
};
const handleSubmit = async (e: any) => {
e.preventDefault()
await axios.post('http://localhost:4000/login', values)
.then((res) => {
dispatch(loginUser(res.data.user))
// console.log(res.data.user)
navigate("/products")
})
.catch((err) => setError(err.response.data.message))
}
// const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
// e.preventDefault();
// users.map((user) => {
// if (user.userId == values.userId.trim()) {
// if (user.password == values.password.trim()) {
// // console.log(user)
// dispatch(loginUser(user))
// navigate("/products")
// }
// else {
// setError(("UserId/Password is incorrect"))
// }
// }
// else {
// setError(("UserId/Password is incorrect"))
// }
// })
// };
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment