-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (85 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
import express from "express";
import path from "path";
import cookieParser from "cookie-parser";
import loginrouter from "./routes/login.js";
import servicerouter from "./routes/services.js";
import connectrouter from "./routes/connect.js";
import profilerouter from "./routes/profile.js";
import { isPresent } from "./controllers/login.js";
import {connectDB} from "./data/database.js";
import connectRouter from "./routes/connect.js"
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import json from "jsonwebtoken";
import { users } from "./models/users.js";
import { now } from "mongoose";
const app = express();
connectDB();
app.set("view engine","ejs");
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// setting Middlewares
app.use(express.static(path.join("public")));
app.use(express.urlencoded({extended:true}));
app.use(cookieParser());
app.use("/connect",connectRouter);
app.use(express.static(__dirname + '/views'));
// setting routes
app.use(loginrouter);
app.use(servicerouter);
app.use(connectrouter);
app.use(profilerouter);
// before login ---------------------------------------------------------------------------------------------------------------------------------------------------------------
app.get("/",isPresent,(req,res) =>{
res.render("index");
})
// before login ---------------------------------------------------------------------------------------------------------------------------------------------------------------
// after login--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
app.get("/afterhome",async(req,res)=>{
let {token}=req.cookies;
if(token)
{
const decoded = json.verify(token,"arimeee");
const user = req.user = await users.findById(decoded._id);
let userName=user.username;
res.render("afterhome",{userName});
}
else
res.redirect("/");
})
//after login-------------------------------------------------------------------------------------------------------------------------------------------------------------------
// services page-----------------------------------------------------------------------------------------------------------------------------------------------------------------
app.get("/afterhome/services",async(req,res)=>{
let {token}=req.cookies;
if(token){
const decoded = json.verify(token,"arimeee");
const user = req.user = await users.findById(decoded._id);
let userName = user.username;
res.render("servicesafter",{userName});
}
else{
res.render("services");
}
}
)
app.get("/services",async(req,res)=>{
let {token}=req.cookies;
if(!token)
return res.render("services");
const decoded = json.verify(token,"arimeee");
const user = req.user = await users.findById(decoded._id);
let userName=user.username;
res.render("servicesafter",{userName});
}
)
// services page-----------------------------------------------------------------------------------------------------------------------------------------------------------------
app.get("/logout",async(req,res)=>{
res.cookie("token",null,{
httpOnly:true,
expires:new Date(Date.now()),
});
res.redirect("/");
})
app.listen(3000,()=>{
console.log("SERVER IS WORKING");
})