Blame view

app/controllers/sessions.js 1.48 KB
10852373   Geoffrey PREUD'HOMME   Session contrôleu...
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
  var Session = require('../models/session');
  
  var sessions = {}
  
  sessions.find = function (id, cb) {
      Session.find({
          '_id': id
      }, cb)
  }
  
  sessions.valid = function (session) {
      return session.started + 3600 > Date.now()
  }
  
  sessions.delete = function (id, cb) {
      Session.remove({
          _id: id
      }, cb);
  }
  
  sessions.close = function (id, cb) {
  
  }
  
  sessions.verify = function (id, cb) {
      session.find(id, function (err, session) {
          if (err) {
              cb('error');
          } else {
              if (sessions.valid(session)) {
                  cb(session);
              } else {
                  cb('expired');
                  sessions.delete(id)
              }
          }
      });
  }
  
  sessions.create = function (login, cb) {
      Session.create({
          login: login
      }, cb);
  }
  
  sessions.login = function (data, cb) {
      // DUMMY
      if (data.login == 'cool' && data.pass == 'cool') {
          cb(null, true);
      } else {
          cb(null, false);
      }
  }
  
  sessions.open = function (data, cb) {
      sessions.login(data, function (err, res) {
          if (err) {
              cb('error')
          } else {
              if (res) {
                  sessions.create(data.login, function (err, session) {
                      if (err) {
                          cb('error');
                      } else {
                          cb(session);
                      }
                  });
              } else {
                  cb('invalid')
              }
          }
      });
  }
  
  module.exports = sessions;