-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
56 lines (53 loc) · 1.22 KB
/
Copy pathauth.js
File metadata and controls
56 lines (53 loc) · 1.22 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
const model=require('../models/eventsInfo');
//To see if the user is a guest
exports.isGuest=(req,res,next)=>
{
if(!req.session.user)
{
return next();
}
else
{
req.flash('error','You have already logged in');
return res.redirect('/users/profile');
}
}
//To see if the user is Logged In
exports.isAuthenticated=(req,res,next)=>
{
if(req.session.user)
{
return next();
}
else
{
req.flash('error','You must be logged in to view this page');
return res.redirect('/users/login');
}
}
//To see if the user is the owner of the event
exports.isAuthorized=(req,res,next)=>{
let id=req.params.id;
model.findById(id).then(event=>{
if(event)
{
if(event.hostName==req.session.user)
{
return next();
}
else
{
let err=new Error('Unauthorized to access the resource');
err.status=401;
return next(err);
}
}
else
{
let err=new Error("Cannot find an event with id "+ id);
err.sttus=404;
next(err);
}
}
).catch(err=>next(err));
}