-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (107 loc) · 3.42 KB
/
index.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
const path = require('path')
const express = require('express');
const app = express();
const ejs = require('ejs');
const userModel = require('./models/user');
const jwt = require('jsonwebtoken');
const cookie = require('cookie-parser');
const bcrypt = require('bcrypt');
const noteModel = require('./models/note');
const { title } = require('process');
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname, '/public')))
app.get("/", (req, res) => {
res.render("landingPage");
})
app.get('/login', (req, res) => {
res.render('login');
})
app.post("/login", async (req, res) => {
let user=await userModel.findOne({email:req.body.email})
if(!user)
return res.send("something is wrong");
else
bcrypt.compare(req.body.password,user.password,(err,result)=>{
if(result){
let token=jwt.sign({email:user.email},"boyismine");
res.cookie("token",token);
return res.redirect('/dashboard/'+user.username);
}
else
return res.send("something is wrong");
})
})
app.get('/signup', (req, res) => {
res.render('signup');
})
app.post("/signup", (req, res) => {
let { username, email, password } = req.body;
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(password, salt, async (err, hash) => {
let u = await userModel.create({
username,
email,
password: hash
})
res.redirect('dashboard/'+username);
})
let token=jwt.sign({email},"boyismine");
res.cookie("token",token) ;
})
})
app.get('/create/:username',(req,res)=>{
res.render('create',{username:req.params.username});
})
app.post('/create/:username',async (req,res)=>{
let {title,data}=req.body;
let note=await noteModel.create({
title,
data
})
res.redirect('/dashboard/'+req.params.username);
// console.log(req.params.username);
})
// get them users:del later
app.get("/user", (req, res) => {
res.render("user");
})
app.get('/dashboard/:username',async (req,res)=>{
const notes=await noteModel.find({});
const user = await userModel.findOne({username: req.params.username });
// res.send(notes) learnt from this ref
// console.log(user);
res.render('dashboard',{username:user.username,notes:notes});
})
app.get('/readmore/:username/:noteTitle',async (req,res)=>{
let noteData=await noteModel.findOne({
title:req.params.noteTitle})
// console.log(noteData);
res.render('readNote',{title:req.params.noteTitle,data:noteData.data,username:req.params.username});
})
app.get('/delete/:title/:username',async (req,res)=>{
let note=await noteModel.findOneAndDelete({title:req.params.title});
res.redirect('/dashboard/'+req.params.username)
})
app.get('/products', (req, res) => {
res.render('products');
})
app.get('/teams', (req, res) => {
res.render('teams');
})
app.get('/individuals', (req, res) => {
res.render('individuals');
})
app.get('/pricing', (req, res) => {
res.render('pricing');
})
app.get('/privacy', (req, res) => {
res.render('privacy');
})
// app.get('/logout',(req,res)=>{
// let token=jwt.sign({email},"boyismine");
// res.cookie("",token);
// res.redirect('/');
// })
app.listen(3000);