Blame view

public/js/services/SessionServ.js 2.21 KB
7a1fe62d   Geoffrey PREUD'HOMME   Consistence des noms
1
2
  angular.module('SessionsServ', []).service('SessionServ', ['$http', 'EncryptServ',
      function ($http, EncryptServ) {
2f593328   Geoffrey PREUD'HOMME   Linting
3
4
          a = {
              cur: false,
2f593328   Geoffrey PREUD'HOMME   Linting
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
              changeHandlers: [],
              onChange: function (fun) {
                  this.changeHandlers.push(fun);
              },
              triggerChange: function () {
                  for (var fun in this.changeHandlers) {
                      this.changeHandlers[fun]();
                  }
              },
              updateSessionInfos: function (data) {
                  console.log("Connection:", data);
                  if (typeof data === 'object') {
                      this.cur = data;
                  } else {
                      this.cur = false;
                  }
                  this.triggerChange();
              },
              get: function (cb) { // Fetch infos if needed
338930e1   Geoffrey PREUD'HOMME   La session n'étai...
24
25
26
27
28
29
30
31
32
                  _this = this;
                  // TODO Verify if cookies to prevent uneeded request
                  $http.get('/api/session').success(function (body) {
                      _this.updateSessionInfos(body);
                      if (cb) {
                          if (this.logged) {
                              cb(null);
                          } else {
                              cb(body);
2f593328   Geoffrey PREUD'HOMME   Linting
33
                          }
338930e1   Geoffrey PREUD'HOMME   La session n'étai...
34
35
                      }
                  });
2f593328   Geoffrey PREUD'HOMME   Linting
36
37
38
39
40
41
42
              },
              connect: function (login, pass, cb) {
                  _this = this;
                  data = JSON.stringify({
                      login: login,
                      pass: pass
                  });
7a1fe62d   Geoffrey PREUD'HOMME   Consistence des noms
43
                  EncryptServ.encrypt(data, function (dataCrypt) {
2f593328   Geoffrey PREUD'HOMME   Linting
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
                      $http.post('/api/session', [dataCrypt]).success(function (body) {
                          _this.updateSessionInfos(body);
                          if (cb) {
                              if (this.logged) {
                                  cb(null);
                              } else {
                                  cb(body);
                              }
                          }
                      });
                  });
              },
              disconnect: function () {
                  this.updateSessionInfos(false);
                  $http.delete('/api/session');
8bb442a5   Geoffrey PREUD'HOMME   Possibilité de se...
59
              }
2f593328   Geoffrey PREUD'HOMME   Linting
60
61
62
63
          };
          a.get();
          return a;
      }
2201e360   Geoffrey PREUD'HOMME   Le login se fait ...
64
  ]);