'login-api-changed'

parent 16e6f8f2
...@@ -81,13 +81,34 @@ app.post('/registeruser', checkUserIdExists, (req, res) => { ...@@ -81,13 +81,34 @@ app.post('/registeruser', checkUserIdExists, (req, res) => {
}); });
// Get Users endpoint // Get Users endpoint
app.get('/users', (req, res) => { // app.get('/users', (req, res) => {
// db.collection('users').find({}, { projection: { _id: false, userId: true, password: true } }).toArray() // // db.collection('users').find({}, { projection: { _id: false, userId: true, password: true } }).toArray()
db.collection('users').find({}, { projection: { _id: false } }).toArray() // db.collection('users').find({}, { projection: { _id: false } }).toArray()
.then(result => { // .then(result => {
res.send(result); // res.send(result);
}) // })
.catch(error => res.status(500).send(error)); // .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) => { app.delete('/deregister/:userid', (req, res) => {
......
import React, { memo, useState, useEffect, useCallback } from 'react'; import React, { memo, useState, useEffect, useCallback } from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import axios from 'axios';
import "./Login.css" import "./Login.css"
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux' import { useSelector, useDispatch } from 'react-redux'
...@@ -33,28 +34,39 @@ const Login: React.FC = memo(() => { ...@@ -33,28 +34,39 @@ const Login: React.FC = memo(() => {
}, [user]) }, [user])
const handleSubmit = async (e: any) => {
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { e.preventDefault()
e.preventDefault(); await axios.post('http://localhost:4000/login', values)
users.map((user) => { .then((res) => {
if (user.userId == values.userId.trim()) { dispatch(loginUser(res.data.user))
if (user.password == values.password.trim()) { // console.log(res.data.user)
// console.log(user) navigate("/products")
dispatch(loginUser(user)) })
navigate("/products") .catch((err) => setError(err.response.data.message))
}
}
else {
setError(("UserId/Password is incorrect")) // const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
} // e.preventDefault();
} // users.map((user) => {
else { // if (user.userId == values.userId.trim()) {
setError(("UserId/Password is incorrect")) // 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 handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target; 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