-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (120 loc) · 5.54 KB
/
app.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
const express = require('express'); //requiring express
const app = express();
const cookieParser = require('cookie-parser'); // cookie parser for generating cookie
const path = require('path'); //require path for direct file access
const jwt = require('jsonwebtoken');// require jwt for send cookies in jwt format
const userModel = require('./models/user');// require user from the database
const postModel = require('./models/post');// require post from the database
const bcrypt = require('bcrypt');//requiring bcrypt for Authentication and authorisation
const upload = require('./config/multerconfig');
app.set("view engine", "ejs"); //set up ejs view engine
app.use(express.json()); //set up middleware
app.use(express.urlencoded({extended: true})); //set up middleware
app.use(express.static(path.join(__dirname, 'public'))) //redirect the short path to public so we are not require to write the full path
app.use(cookieParser());
app.get('/', (req,res)=>{
res.render('index'); //render to index.ejs
});
app.get('/profile/upload', (req,res)=>{
res.render('profileUpload'); //render to index.ejs
});
app.post('/upload', isLoggedIn, upload.single("image") , async (req,res)=>{
let user = await userModel.findOne({email: req.user.email})
user.profilepic = req.file.filename;
await user.save();
res.redirect("/profile")
});
app.get('/login' , (req,res)=>{
res.render('login'); //rendering login page
})
app.post('/register', async (req,res)=>{ //this route create user
let {email, password, username, name, dob} = req.body; // getting data from index.ejs form
let user = await userModel.findOne({email}); //find user on the basis of email id cheaking already register or not
if(user) return res.status(500).send("user already registter");
bcrypt.genSalt(10 , (err,salt)=>{ // generating slat for cheating hash password
bcrypt.hash(password, salt , async (err,hash)=>{ // creating hash password for security
let user = await userModel.create({ // creating user
username,
email,
dob,
name,
dob,
password:hash
});
let token = jwt.sign({email:email, userid: user._id}, "shhhh"); // crating jwt token
res.cookie("token",token); // sending jwt token as cookie
res.send("user registered successfully"); // sending message
});
});
});
app.post('/login',async (req,res)=>{ // creating login route to getting data for login
let {email, password} = req.body; // getting data from user form of login route
let user = await userModel.findOne({email}); // finding user on the basis of email
if(!user) return res.status(500).send("Something went wrong");
bcrypt.compare(password, user.password , async (err,result)=>{ // cheaking password
if(result){ // if matched
let token = jwt.sign({email:email, userid: user._id}, "shhhh"); // generating token
res.cookie("token",token); // send token as cookie for long time user uses
res.status(200).redirect('/profile'); // if all correct them redirect to profile page
}
else res.redirect("/login") // if not matched
})
})
app.get('/logout', (req,res)=>{ // creating logout route for removing cookie
res.cookie("token", "");
res.redirect('/login');
});
app.get('/profile',isLoggedIn, async (req,res)=>{ // crating profile route for logged in user uses
let user = await userModel.findOne({email: req.user.email});
await user.populate("posts");
res.render('profile',{user});
})
app.get('/home',isLoggedIn, async (req,res)=>{ // crating profile route for logged in user uses
// const userx = await userModel.findOne({email:req.user.email});
const allposts = await postModel.find({}).populate("user");
res.render('home',{allposts});
})
app.get('/like/:id', isLoggedIn, async (req,res)=>{ // crating profile route for logged in user uses
let post = await postModel.findOne({_id: req.params.id}).populate("user");
if(post.likes.indexOf(req.user.userid) == -1){
post.likes.push(req.user.userid);
}
else{
post.likes.splice(post.likes.indexOf(req.user.userid), 1);
}
await post.save();
res.redirect("/profile")
});
app.get('/edit/:id', isLoggedIn, async (req,res)=>{ // crating profile route for logged in user uses
let post = await postModel.findOne({_id: req.params.id}).populate("user");
res.render("edit",{post})
});
app.post('/update/:id',isLoggedIn, async (req,res)=>{ // crating profile route for logged in user uses
let post = await postModel.findOneAndUpdate({_id: req.params.id}, {content: req.body.content},{new:true});
res.redirect('/profile');
});
app.post('/post',isLoggedIn, async (req,res)=>{ // crating profile route for logged in user uses
let user = await userModel.findOne({email: req.user.email});
let {content} = req.body;
let post = await postModel.create({
user: user._id,
content
});
user.posts.push(post._id);
await user.save();
res.redirect('/profile')
});
//function for warn any page to login
function isLoggedIn(req,res, next){ // this function cheaks user logged in or not
if(req.cookies.token == ""){ // cheaking based on cookie
res.redirect("/login");
}
else{
let data = jwt.verify(req.cookies.token, "shhhh") // getting data from jwt cookie
req.user = data; // setting user with the jwt data
next();
}
}
app.listen(3000, ()=>{ // running server on 3000 port
console.log("server on");
});