forked from datananda/Project2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
58 lines (32 loc) · 1.4 KB
/
auth.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
const html_routes = require("./html-routes.js");
const api_routes = require("./api-routes.js");
module.exports = function(app, passport) {
app.get('/posts', isLoggedIn, html_routes.posts);
app.get('/posts/edit/:id', isLoggedIn, html_routes.posts_edit);
app.get('/posts/new', isLoggedIn, html_routes.posts_new);
app.get('/users/:id', isLoggedIn, html_routes.user_profile);
app.get('/users/edit/:id', isLoggedIn, html_routes.user_edit);
app.get('/register', html_routes.register);
app.get('/login', html_routes.login);
app.get('/logout', html_routes.logout);
app.get('/failedlogin', html_routes.failedlogin);
app.get('/failedregister', html_routes.failedregister);
app.post('/register', passport.authenticate('local-register', {
successRedirect: '/posts',
failureRedirect: '/failedregister'
}
));
app.post('/login', passport.authenticate('local-login', {
successRedirect: '/posts',
failureRedirect: '/failedlogin'
}));
app.post('/api/posts/new', api_routes.posts_create);
app.put('/api/posts/:id', api_routes.posts_update);
app.put('/api/users/:id', api_routes.users_update);
app.delete('/api/posts/:id', api_routes.posts_delete);
}
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/login');
}