forked from dmyronuk/kiro
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.js
66 lines (58 loc) · 2.58 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
"use strict";
const listingsController = require('./controllers/listings-controller.js');
const photoController = require('./controllers/photo-controller.js');
const yelpController = require('./controllers/yelp-controller.js');
const messagesController = require('./controllers/messages-controller.js');
const usersController = require('./controllers/users-controller.js');
const ratingsController = require('./controllers/ratings-controller.js');
const jwt = require("jsonwebtoken");
function authMiddleware(req,res,next) {
let token = req.body.token || req.headers.authorization
if (!token) {
console.log("No token");
res.status(401).end();
return;
}
jwt.verify(token, process.env.JWT_PRIVATE_KEY, (err, decoded) => {
if(err) {
console.log("Invalid token")
res.status(401).end();
return;
}
req.decodedToken = decoded;
console.log("User authorized")
next();
});
}
module.exports = function(app) {
// LISTINGS
//public routes
app.post('/api/listings/search', listingsController.searchListings);
app.post('/api/listings/:id/yelp', yelpController.categorySearchByLocation);
app.get('/api/listings/:id', listingsController.getListing);
app.get('/api/listings', listingsController.getListings);
//protected routes
app.get('/api/landlord-listings', authMiddleware, listingsController.getLandlordListings);
app.post('/api/listings', authMiddleware, listingsController.postListings);
app.delete('/api/listings/:id', authMiddleware, listingsController.deleteListing);
app.patch('/api/listings/:id', authMiddleware, listingsController.editListing);
// PHOTOS
//protected routes
app.post('/api/upload', authMiddleware, photoController.uploadFile);
// MESSAGES
app.get ('/api/messages', messagesController.getAllMessages);
app.get('/api/filtered-messages', messagesController.getFilteredMessages);
app.post('/api/newMessage', messagesController.addNewMessage);
// USERS
//public routes
app.post('/api/signup', usersController.signup);
app.post('/api/profile', usersController.profile)
app.post('/api/login', usersController.login)
app.get('/api/ratings', ratingsController.getAllRatingsThatUserRated)
app.get('/api/ratee', ratingsController.getAllRatingsOfRatee)
app.post('/api/ratings', ratingsController.addNewRating)
app.get('/api/threads', usersController.threads)
app.post('/api/users/:id', usersController.getUsernameById)
app.get('/api/get-user-from-landlord-id', usersController.getUserFromLandlordId)
app.post('/api/landlord', usersController.getLandlord)
};