Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mernStack
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Venkaiah Naidu Singamchetty
mernStack
Commits
fc961257
Commit
fc961257
authored
Mar 11, 2024
by
Venkaiah Naidu Singamchetty
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
'login-api-changed'
parent
16e6f8f2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
29 deletions
+62
-29
server.js
server.js
+28
-7
Login.tsx
src/components/Login/Login.tsx
+34
-22
No files found.
server.js
View file @
fc961257
...
...
@@ -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
)
=>
{
...
...
src/components/Login/Login.tsx
View file @
fc961257
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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment