aboutsummaryrefslogtreecommitdiffstats
path: root/routes/sessions.js
blob: 6f04d2fdc503a16f77f74af76e7d50ae9404f635 (plain) (blame)
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
(function() {
  module.exports = function(passport) {
    return {
      "new": function(req, res) {
        return res.render('sessions/new', {
          layout: 'layout',
          js: (function() {
            return global.js;
          }),
          css: (function() {
            return global.css;
          })
        });
      },
      create: function(req, res, next) {
        return passport.authenticate('local', function(err, user, info) {
          if (err) {
            return next(err);
          }
          if (!user) {
            return res.render('sessions/new', {
              layout: 'layout',
              js: (function() {
                return global.js;
              }),
              css: (function() {
                return global.css;
              }),
              badpw: true
            });
          }
          return req.login(user, function(err) {
            var url;
            if (err) {
              return next(err);
            }
            if (/edit/.test(req.headers['referer'])) {
              url = "/" + user.username + "/edit";
            } else {
              url = "/" + user.username;
            }
            return res.redirect(url);
          });
        })(req, res, next);
      },
      destroy: function(req, res) {
        req.logout();
        return res.redirect('/login');
      }
    };
  };

}).call(this);