-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.js
More file actions
65 lines (51 loc) · 1.26 KB
/
Copy pathcookie.js
File metadata and controls
65 lines (51 loc) · 1.26 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
import express from "express";
import path from "path";
import mongoose, { Schema } from "mongoose";
import cookieParser from "cookie-parser";
// using middleware for post request
server.use(express.urlencoded({extended:true}));
//using middle ware for parsing cookies
server.use(cookieParser());
const user = mongoose.model("User" /* collections name */, userSchema);
const isAuth = (req,res,next)=>{
const {token} = req.cookies;
if(token)
{
next();
}
else
{
res.render("login.ejs");
}
};
server.get("/",isAuth,(req,res)=>
{
res.render("logout.ejs");
});
server.post("/logout",(req,res)=>{
// console.log("irun");
res.cookie("token",null,{
httpOnly:true,
expires:new Date(Date.now())
});
res.redirect("/");
});
server.get("/success",(req,res)=>{
const pathloc = path.resolve();
res.sendFile(path.join(pathloc,"success.html"));
});
server.post("/login", (req,res)=>{
res.cookie("token","iamin",{
httpOnly:true,expires:new Date(Date.now()+60*1000)
});
//console.log(Date.now());
res.redirect("/");
});
server.get("/users", (req,res)=>{
res.json({
users
});
});
server.listen(5000,()=>{
console.log("server is working");
});