Blame view

public/js/services/SessionServ.js 2.46 KB
8bb442a5   Geoffrey PREUD'HOMME   Possibilité de se...
1
2
3
  angular.module('SessionsServ', []).service('SessionService', ['$http',
      function ($http) {
          a = {
dafb4eeb   Geoffrey PREUD'HOMME   Affichage des nom...
4
              nom: "Invité",
8bb442a5   Geoffrey PREUD'HOMME   Possibilité de se...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
              logged: false,
              status: 0,
              changeHandlers: [],
              onChange: function (fun) {
                  this.changeHandlers.push(fun)
              },
              triggerChange: function () {
                  for (fun in this.changeHandlers) {
                      this.changeHandlers[fun]()
                  }
              },
              updateSessionInfos: function (data) {
                  if (typeof data === 'object') {
                      console.log("Connected")
                      this.logged = true
dafb4eeb   Geoffrey PREUD'HOMME   Affichage des nom...
20
                      this.nom = data.nom
8bb442a5   Geoffrey PREUD'HOMME   Possibilité de se...
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
                  } else {
  
                      this.logged = false
                  }
                  this.triggerChange()
              },
              get: function (cb) { // Fetch infos if needed
                  console.log("Session: get")
                  if (status == 0) {
                      this.status = 1 // Fetching
                      _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)
                              }
                          }
                      })
                  } else {
                      console.warn("get multiple times")
                  }
              },
              connect: function (login, pass, cb) {
                  console.log("Session: connecting with login:", login)
                  _this = this
                  $http.post('/api/session', {
                      login: login,
                      pass: pass
                  }).success(function (body) {
                      _this.updateSessionInfos(body)
                      if (cb) {
                          if (this.logged) {
                              cb(null)
                          } else {
                              cb(body)
                          }
                      }
                  })
              },
              disconnect: function () {
                  console.log("Session: disconnect", this.name)
                  $http.delete('/api/session')
                  this.logged = false
              }
9a023783   Geoffrey PREUD'HOMME   [WIP] Session mis...
69
          }
8bb442a5   Geoffrey PREUD'HOMME   Possibilité de se...
70
71
          a.get()
          return a
9a023783   Geoffrey PREUD'HOMME   [WIP] Session mis...
72
      }
8bb442a5   Geoffrey PREUD'HOMME   Possibilité de se...
73
  ])