-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
95 lines (76 loc) · 2.47 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
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
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy;
var rest = require("./rest");
var request = require('request');
exports.configure = function (login_url, logout_url, app) {
passport.serializeUser(function(user, callback) {
callback(null, user);
});
passport.deserializeUser(function(user, callback) {
callback(null, user);
});
passport.use(new LocalStrategy(
function(username, password, callback) {
rest.authenticate(username, password, function(error, token) {
if (error) return callback(error);
if (token == null) {
return callback(null, false);
} else {
return callback(null, { email: username, token: token });
}
});
}));
app.get(login_url + "/:targetURL", function (req, res, next) {
res.render('login.jade', { user: req.user });
});
app.post(login_url + "/:targetURL", function (req, res, next) {
if(req.body.signup == "Sign Up"){
res.redirect('/signup');
//return next();
} else {
var target = req.param("targetURL", "/");
passport.authenticate('local', {
failureRedirect: login_url + "/" + encodeURIComponent(target),
successRedirect: target
}
)(req, res, next);
}
});
app.get(logout_url, function (req, res) {
req.logout();
res.redirect('/');
});
/*app.get(login_url + "/:targetURL" + "signup", function (req, res, next) {
res.render('signup.jade', { user: req.user });
});*/
app.get('/signup', function (req, res, next) {
res.render('signup.jade', { user: req.user });
});
app.post('/signup', function (req, res, next) {
rest.executeSignup(req, res, req.body.authusername, req.body.authpassword, 'AddAccount', [req.body.username, req.body.password], function (error, response) {
if (error) {
res.redirect('/signup');
} else {
rest.executeSignup(req, res, req.body.authusername, req.body.authpassword, 'AddNameserverGroup', [req.body.username], function (error, response) {
if (error) {
res.redirect('/signup');
} else {
res.redirect('/');
}
});
}
});
});
exports.ensureAuthenticated = function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()||req.url == "/signup") { return next(); }
res.redirect(login_url + "/" + encodeURIComponent(req.url));
};
return passport;
};
exports.randomString = function () {
var randomstring = '';
for (var idx = 0; idx < 20; idx++) {
randomstring += String.fromCharCode(Math.floor(Math.random() * 256));
}
return randomstring;
};