-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
196 lines (169 loc) · 6.6 KB
/
server.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require('dotenv').config();
const mongoose = require("mongoose");
const express = require("express");
const methodOverride=require("method-override");
const bcrypt=require("bcrypt")
const jwt = require("jsonwebtoken");
const User = require("./Schema/UserSchema")
const Post = require("./Schema/PostSchema")
const authenticateJWT = require('./MiddleWare/Authorize')
const cors = require("cors")
const app = express();
const PORT = process.env.PORT || 4500;
app.use(express.json());
app.use(express.urlencoded({extended:true}));
app.use(methodOverride("_method"));
app.use(cors());
let DB_URL= "mongodb://localhost:27017/highon"
if(process.env.DB_URL) DB_URL=process.env.DB_URL
mongoose.connect(DB_URL,{ useNewUrlParser: true, useUnifiedTopology: true, dbName: "courses" })
.then(()=> console.log("Database Connected Successfully"))
.catch((err)=> console.log("Error connecting to the Database ",err))
app.post('/users/signup',async (req,res)=>{
const {username,email,password,imageURL}=req.body;
//Server Side Validtion
if((username==undefined || password==undefined || email==undefined || imageURL==undefined )) return res.sendStatus(400);
//Checking if the user Already Exists
let user = await User.findOne({email});
if(user) return res.status(403).json({ "message":"User Already Exists "});
//Creating a Hash for the password
const hash = await bcrypt.hash(password,12);
//Storing user in the Database
try {
const userObject = {username:username,email:email,hashedPassword:hash,imageURL:imageURL}
const newUser = new User(userObject);
await newUser.save();
//Creating a JWT token to Authorize the user for future Requests
const token = jwt.sign({id:newUser.id,username:username,email:email},process.env.SECRET,{expiresIn:'1m'});
res.status(201).json({message:"User Created Successfully",
token,
userId:newUser.id,
uid:newUser._id,
imageURL:newUser.imageURL,
username:newUser.username
});
}
catch(e){
res.status(500).json({message:"Internal Server Error"});
}
})
//Only For Dev
app.get('/users/',async(req,res)=>{
const users = await User.find();
res.json(users);
})
app.post('/users/login',async(req,res)=>{
const {email,password} = req.body;
// Server Side Validations
if(email==undefined || password == undefined) return res.sendStatus(400);
//Checking if the User exists or not
let user=null;
try{
user = await User.findOne({email})
}
catch(e){
return res.sendStatus(500);
}
if(user==undefined) return res.status(401).json({"message":"User Not Registered"})
// Comparing the password with the hashed ones
if(await bcrypt.compare(password,user.hashedPassword))
{
//Creating a token
const token = jwt.sign({id:user.id,username:user.username,email:user.email},process.env.SECRET,{expiresIn:"1h"});
return res.status(200).json({message:"User Logged In Successfully ",
token,
uid:user._id,
imageURL:user.imageURL,
username:user.username
});
}
else
{
return res.status(401).json({message:"Invalid Credentials"});
}
})
app.post('/users/image',authenticateJWT,async (req,res)=>{
const userid=req.id;
if(userid==undefined) return res.status(400).json({"message":"Fields Missing"})
try{
const user = await User.findById(userid);
if(user) return res.status(200).json({imageURL:user.imageURL});
else return res.status(400).json({message:"User Does not exists"});
}
catch(e){
return res.status(500).json({messsage:"Internal Server Error"})
}
})
app.get('/posts/',async (req,res)=>{
const {userid}=req.headers;
try{
let posts = await Post.find();
posts = posts.map((post)=>{
let liked = false;
if(userid)
{
temp = post.likes.find((like) => like.userId==userid)
if(temp!=undefined) liked=true;
}
return {id:post._id,
authorImageURL:post.authorImageURL,
description:post.description,
location:post.location,
author:post.author,
authorName:post.authorName,
imageURL:post.imageURL,
likes:post.likes,
createdAt:post.createdAt,
liked:liked};
})
res.status(200).json({posts});
}
catch(e){
return res.status(500).json({message:"Internal Server Error"})
}
})
app.post('/post/create',authenticateJWT,async (req,res)=>{
const {description,imageURL,location} = req.body;
if(description==undefined || imageURL==undefined || location==undefined) return res.status(400).json({message:"Missing Info"});
const user = await User.findById(req.id);
req.imageURL=user.imageURL;
try{
const postObject = {
description,
author:req.id,
authorName:req.username,
authorImageURL:req.imageURL,
imageURL,
location,
}
const post = new Post(postObject);
await post.save();
return res.status(201).json({"message":"Post Created Successfully"});
}
catch(e){
return res.status(500).json({message:"Internal Server Error, Try After some time "})
}
})
app.post('/post/like',authenticateJWT,async (req,res)=>{
const postid=req.headers.postid;
if(postid==undefined) return res.status(400).json({message:"Empty Fields"})
const userId=req.id;
const user = await User.findById(req.id);
req.imageURL=user.imageURL;
try{
const post = await Post.findById(postid);
let likes=post.likes
const like = likes.find((like) => like.userId==userId);
if(like===undefined) likes.push({userId:userId,userImageURL:req.imageURL,userName:req.username});
else likes = likes.filter((like) => like.userId!=userId);
Object.assign(post,{...post,likes})
await Post.findByIdAndUpdate(postid,post,{new:true});
return res.status(202).json({message:"Post Updated Successfully"});
}
catch(e){
return res.status(500).json({message:"Internal Server Error"});
}
})
app.listen(PORT, ()=>{
console.log("Listening on PORT ",PORT);
})