Last change
on this file since 16237c4 was 16237c4, checked in by Nace Gjorgjievski <nace.gorgievski123@…>, 22 months ago |
Added Order Functionality
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | import express from "express";
|
---|
2 | import expressAsyncHandler from "express-async-handler";
|
---|
3 | import User from "../models/userModel.js";
|
---|
4 | import bcrypt from "bcryptjs";
|
---|
5 | import { generateToken } from "../utils.js";
|
---|
6 |
|
---|
7 | const userRouter = express.Router();
|
---|
8 |
|
---|
9 | userRouter.post(
|
---|
10 | "/signin",
|
---|
11 | expressAsyncHandler(async (req, res) => {
|
---|
12 | const user = await User.findOne({ email: req.body.email });
|
---|
13 | if (user) {
|
---|
14 | if (bcrypt.compareSync(req.body.password, user.password)) {
|
---|
15 | res.send({
|
---|
16 | _id: user.id,
|
---|
17 | name: user.name,
|
---|
18 | contact: user.contact,
|
---|
19 | email: user.email,
|
---|
20 | isAdmin: user.isAdmin,
|
---|
21 | token: generateToken(user),
|
---|
22 | });
|
---|
23 | return;
|
---|
24 | }
|
---|
25 | }
|
---|
26 | res.status(401).send({ message: "Invalid email or password" });
|
---|
27 | })
|
---|
28 | );
|
---|
29 |
|
---|
30 | userRouter.post(
|
---|
31 | "/signup",
|
---|
32 | expressAsyncHandler(async (req, res) => {
|
---|
33 | const newUser = new User({
|
---|
34 | name: req.body.name,
|
---|
35 | contact: req.body.contact,
|
---|
36 | email: req.body.email,
|
---|
37 | password: bcrypt.hashSync(req.body.password),
|
---|
38 | });
|
---|
39 | const user = await newUser.save();
|
---|
40 | res.send({
|
---|
41 | _id: user.id,
|
---|
42 | name: user.name,
|
---|
43 | contact: user.contact,
|
---|
44 | email: user.email,
|
---|
45 | isAdmin: user.isAdmin,
|
---|
46 | token: generateToken(user),
|
---|
47 | });
|
---|
48 | })
|
---|
49 | );
|
---|
50 |
|
---|
51 | export default userRouter;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.