-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
125 lines (110 loc) · 3.14 KB
/
server.js
File metadata and controls
125 lines (110 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const express = require("express")
const app = express()
require("dotenv").config();
const port = process.env.PORT||8000;
const bcrypt = require("bcryptjs")
require("./db/conn")
const User = require("./db/schema/UserSchema")
const stripe = require("stripe")(process.env.STRIPE_SECRET_TEST);
const cors = require("cors");
var jwt = require('jsonwebtoken');
app.use(cors());
app.use(express.json());
app.get("/users",async(req,res)=>{
const email = req.query.email;
const password = req.query.password;
const users = await User.findOne({email})
if(typeof(users)==="undefined"){
res.send("User Not found")
}else{
try{
if(await bcrypt.compare(password,users.password)){
var token = await jwt.sign({ id: users._id }, process.env.SECRET, {});
const _id = users._id
await User.updateOne({_id},{ $set: {token: token}})
res.send({users,token})
}else{
res.send("Invalid Password")
}
}catch(e){
res.send("Something Went Wrong!!")
}
}
})
app.post("/register",async(req,res)=>{
const email = req.body.email
const password = await bcrypt.hash(req.body.password,12);
const users = await User.find({email})
if(typeof(users[0])==="undefined"){
try{
const user = await new User({
name:req.body.name,
email,
password
})
await user.save()
res.send("Registered Successfully!!")
}catch(e){
res.send("Something errorOccured")
}
}else{
res.send("Email already registered!!")
}
})
app.post("/stripe/charge",cors(), async (req, res) => {
let { amount, id } = req.body;
try {
const payment = await stripe.paymentIntents.create({
amount: amount,
currency: "INR",
description: "Your Company Description",
payment_method: id,
confirm: true,
});
console.log("success");
res.json({
message: "Payment Successful",
success: true,
});
} catch (error) {
console.log(error);
res.json({
message: "Payment Failed",
success: false,
});
}
});
app.post("/orders",async(req,res)=>{
const orders = req.body;3
try{
const user=await User.updateOne({_id:orders.id},{ $set: {orders: orders.basket }})
res.send("Success")
}catch(e){
res.send("Failed")
}
})
app.get("/orders",async(req,res)=>{
const _id = req.query.id
try{
const user = await User.findOne({_id})
res.send(user.orders)
}catch(e){
console.log(e)
}
})
app.get("/ver",async(req,res)=>{
const token = req.query.token
if(!token){
res.send("No String")
}else{
const ver = await jwt.verify(token,process.env.SECRET)
const users = await User.findOne({_id:ver.id})
res.send(users)
}
})
if(process.env.NODE_ENV==="production"){
app.use(express.static("client/build"))
}
app.listen(port,()=>{
console.log(`Listening at ${port}`)
})