-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
128 lines (98 loc) · 4.16 KB
/
routes.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
var users = require('./controllers/users'),
trails = require('./controllers/trails'),
posts = require('./controllers/posts'),
comments = require('./controllers/comments'),
passport = require('./config/passport'),
gridFs = require('./controllers/gridFS'),
multer = require("multer"),
upload = multer({dest: "./uploads"});
module.exports = function(app, passport) {
app.get('/', function(req, res) {
res.writeHead(200, {"Content-Type": "application/json"});
res.end("MobilePassport API v1");
});
//==========Authentication Routes============
// Login [x]
app.post('/login', users.login);
// Register [x]
app.post('/signup', users.create);
// Search For User by ID [x]
app.get('/user/search/id/:id', isLoggedIn, users.read);
// Search For User by Username [x]
app.get('/user/search/username/:username', isLoggedIn, users.readByUsername);
// Search For Users by Username [x]
app.get('/user/search/usernames/:usernames', isLoggedIn, users.findUsers);
// Follow User by Username [x]
app.post('/user/follow/userId', isLoggedIn, users.follow);
// Unfollow User by Username [x]
app.post('/user/unfollow/userId', isLoggedIn, users.unfollow);
// My Profile for Currently Logged in User [x]
app.get('/user/profile', isLoggedIn, users.me);
// Update As Currently Logged In User [x]
app.put('/user/update', isLoggedIn, users.update);
// Delete Currently Logged in User [x]
app.delete('/users/search/id/:id', isLoggedIn, users.delete);
// [x]
app.post('/logout', isLoggedIn, function(req, res) {
req.session.destroy(function(err){
if(err)
res.status(500).end('an internal error occurred');
else
res.end('logged out');
});
});
//==========File Store Routes============
// Read an existing filestream [x]
app.get('/upload/:fileId', isLoggedIn, gridFs.read);
// Create a new filestream [x]
app.post('/upload', upload.single("recfile"), gridFs.create);
// Delete an existing filestream [x]
app.delete('/upload:fileId', isLoggedIn, gridFs.delete);
//==========Trail Mapping Routes============
// Create a new trail [x]
app.post('/trails', isLoggedIn, trails.create);
// Get all trails [x]
app.get('/trails', isLoggedIn, trails.getTrails);
// delete trail by id [x]
app.get('/users/id/:id/trails/search/id/:trailId', isLoggedIn, trails.deleteTrail);
//==========Trail Event Routes============
//==========Post Routes============
// Create a new post [x]
app.post('/posts', isLoggedIn, posts.create);
//Get all posts of current user and following
app.get('/posts/:lastSeen', isLoggedIn, posts.getPosts);
//Get all the posts of only the current user
app.get('/posts/user', isLoggedIn, posts.getUserPosts);
//A post is liked [x]
app.post('/posts/like', isLoggedIn, posts.like);
//A post is unliked
app.post('/posts/unlike', isLoggedIn, posts.unlike);
//A post is commented on [x]
app.post('/posts/comment', isLoggedIn, comments.create);
app.get('/posts/comments/:postId/:lastSeen', isLoggedIn, comments.getComments);
//A comment is deleted [x]
app.delete('users/id/:id/comments/search/id/:commentId', isLoggedIn, comments.deleteComment);
//A reply is deleted [x]
app.delete('users/id/:id/comments/search/id/:commentId/replies/:replyId', isLoggedIn, comments.deleteReply);
//Delete a post by ID [x]
app.delete('users/id/:id/posts/search/id/:postId', isLoggedIn, posts.deletePost);
//======Facebook Authentication Routes=======
// Authenticate a user with facebook [x]
app.post('/auth/facebook/token',
passport.authenticate('facebook-token'),
function (req, res) {
if (req.user){
return res.status(200).end(JSON.stringify(req.user));
} else
return res.status(401).end('Facebook user not found');
});
}
// Make a call to redis to ensure an active session [x]
function isLoggedIn(req, res, next) {
//if(req.session.key)
//console.log(JSON.stringify(req.user));
//if(req.user)
if(req.session.key)
return next();
return res.status(401).end('Not logged in');
}