Commit 8bb442a534302fb8ceaad7db415c4a563e67a0fc
1 parent
9a023783
Possibilité de se connecter
login: cool pass: cool
Showing
11 changed files
with
124 additions
and
39 deletions
Show diff stats
app/controllers/sessions.js
@@ -3,13 +3,11 @@ var Session = require('../models/session'); | @@ -3,13 +3,11 @@ var Session = require('../models/session'); | ||
3 | var sessions = {} | 3 | var sessions = {} |
4 | 4 | ||
5 | sessions.find = function (id, cb) { | 5 | sessions.find = function (id, cb) { |
6 | - Session.find({ | ||
7 | - '_id': id | ||
8 | - }, cb) | 6 | + Session.findById(id, cb) |
9 | } | 7 | } |
10 | 8 | ||
11 | sessions.valid = function (session) { | 9 | sessions.valid = function (session) { |
12 | - return session.started + 3600 > Date.now() | 10 | + return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date() |
13 | } | 11 | } |
14 | 12 | ||
15 | sessions.delete = function (id, cb) { | 13 | sessions.delete = function (id, cb) { |
@@ -18,20 +16,21 @@ sessions.delete = function (id, cb) { | @@ -18,20 +16,21 @@ sessions.delete = function (id, cb) { | ||
18 | }, cb); | 16 | }, cb); |
19 | } | 17 | } |
20 | 18 | ||
21 | -sessions.close = function (id, cb) { | ||
22 | - | ||
23 | -} | ||
24 | - | ||
25 | sessions.verify = function (id, cb) { | 19 | sessions.verify = function (id, cb) { |
26 | - session.find(id, function (err, session) { | 20 | + _this = this |
21 | + Session.findById(id, function (err, session) { | ||
27 | if (err) { | 22 | if (err) { |
28 | cb('error'); | 23 | cb('error'); |
29 | } else { | 24 | } else { |
30 | - if (sessions.valid(session)) { | ||
31 | - cb(session); | 25 | + if (session) { |
26 | + if (sessions.valid(session)) { | ||
27 | + cb(err, session); | ||
28 | + } else { | ||
29 | + cb('expired'); | ||
30 | + _this.delete(id) | ||
31 | + } | ||
32 | } else { | 32 | } else { |
33 | - cb('expired'); | ||
34 | - sessions.delete(id) | 33 | + cb('unknown') |
35 | } | 34 | } |
36 | } | 35 | } |
37 | }); | 36 | }); |
app/routes/api.js
@@ -7,7 +7,13 @@ var api = express() | @@ -7,7 +7,13 @@ var api = express() | ||
7 | // Sessions | 7 | // Sessions |
8 | api.get('/session', function (req, res) { // Informations sur la session | 8 | api.get('/session', function (req, res) { // Informations sur la session |
9 | if (req.cookies && req.cookies.session) { | 9 | if (req.cookies && req.cookies.session) { |
10 | - res.send(sessions.verify(req.cookies.session)) | 10 | + sessions.verify(req.cookies.session, function (err, session) { |
11 | + if (err) { | ||
12 | + res.send(err) | ||
13 | + } else { | ||
14 | + res.send(session) | ||
15 | + } | ||
16 | + }) | ||
11 | // TODO si pas bon : res.clearCookie('session') | 17 | // TODO si pas bon : res.clearCookie('session') |
12 | } else { | 18 | } else { |
13 | res.send('missing'); | 19 | res.send('missing'); |
@@ -24,9 +30,10 @@ api.post('/session', function (req, res) { // Se connecter | @@ -24,9 +30,10 @@ api.post('/session', function (req, res) { // Se connecter | ||
24 | }) | 30 | }) |
25 | 31 | ||
26 | api.delete('/session', function (req, res) { // Se déconnecter | 32 | api.delete('/session', function (req, res) { // Se déconnecter |
27 | - if (req.cookies) { | ||
28 | - sessions.delete(req.cookies.id, function () { | 33 | + if (req.cookies.session) { |
34 | + sessions.delete(req.cookies.session, function () { | ||
29 | res.clearCookie('session'); | 35 | res.clearCookie('session'); |
36 | + res.end() | ||
30 | }) | 37 | }) |
31 | } else { | 38 | } else { |
32 | res.send('missing') | 39 | res.send('missing') |
package.json
@@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
3 | "main": "server.js", | 3 | "main": "server.js", |
4 | "dependencies": { | 4 | "dependencies": { |
5 | "body-parser": "^1.12.0", | 5 | "body-parser": "^1.12.0", |
6 | + "cookie-parser": "^1.3.4", | ||
6 | "express": "^4.12.2", | 7 | "express": "^4.12.2", |
7 | "method-override": "^2.3.1", | 8 | "method-override": "^2.3.1", |
8 | "mongoose": "^3.8.25", | 9 | "mongoose": "^3.8.25", |
public/js/app.js
1 | -angular.module('ciApp', ['ngRoute', 'appRoutes', 'MembreCtrl', 'SessionsServ', 'SessionsCtrl']); | ||
2 | \ No newline at end of file | 1 | \ No newline at end of file |
2 | +angular.module('ciApp', ['ngRoute', 'appRoutes', 'MembreCtrl', 'SessionsServ', 'SessionsCtrl', 'ConnectCtrl']); | ||
3 | \ No newline at end of file | 3 | \ No newline at end of file |
public/js/appRoutes.js
1 | // public/js/appRoutes.js | 1 | // public/js/appRoutes.js |
2 | angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', | 2 | angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', |
3 | function ($routeProvider, $locationProvider) { | 3 | function ($routeProvider, $locationProvider) { |
4 | - | ||
5 | $routeProvider | 4 | $routeProvider |
6 | .when('/', { | 5 | .when('/', { |
7 | templateUrl: 'views/home.html' | 6 | templateUrl: 'views/home.html' |
@@ -11,8 +10,8 @@ angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', | @@ -11,8 +10,8 @@ angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', | ||
11 | controller: 'MembreController' | 10 | controller: 'MembreController' |
12 | }) | 11 | }) |
13 | .when('/connect', { | 12 | .when('/connect', { |
14 | - templateUrl: 'views/connect.html' | ||
15 | - // controller: 'ConnectController' | 13 | + templateUrl: 'views/connect.html', |
14 | + controller: 'ConnectController' | ||
16 | }); | 15 | }); |
17 | 16 | ||
18 | $locationProvider.html5Mode(true); | 17 | $locationProvider.html5Mode(true); |
@@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
1 | +angular.module('ConnectCtrl', []).controller('ConnectController', ['$scope', 'SessionService', | ||
2 | + function ($scope, SessionService) { | ||
3 | + $scope.connect = { | ||
4 | + connect: function () { | ||
5 | + SessionService.connect($scope.connect.login, $scope.connect.pass) | ||
6 | + } | ||
7 | + } | ||
8 | + } | ||
9 | +]); | ||
0 | \ No newline at end of file | 10 | \ No newline at end of file |
public/js/controllers/SessionCtrl.js
1 | -angular.module('SessionsCtrl', []).controller('SessionController', ['$scope', '$http', 'SessionService', | ||
2 | - function ($scope, $http, SessionService) { | ||
3 | - $scope.session = { | ||
4 | - logged: true, | ||
5 | - name: SessionService.get() | ||
6 | - } | ||
7 | - | ||
8 | - $scope.disconnect = function () { | ||
9 | - $scope.session.logged = false; | ||
10 | - } | 1 | +angular.module('SessionsCtrl', []).controller('SessionController', ['$scope', 'SessionService', |
2 | + function ($scope, SessionService) { | ||
3 | + $scope.session = SessionService | ||
4 | + // $scope.session.onChange(function () { | ||
5 | + // // TODO | ||
6 | + // }) | ||
7 | + // $scope.$on("$destroy", function () { | ||
8 | + // // TODO | ||
9 | + // }) | ||
11 | } | 10 | } |
12 | ]); | 11 | ]); |
13 | \ No newline at end of file | 12 | \ No newline at end of file |
public/js/services/SessionServ.js
1 | -angular.module('SessionsServ', []).service('SessionService', function () { | ||
2 | - return { | ||
3 | - get: function () { | ||
4 | - return 'BANANE'; | 1 | +angular.module('SessionsServ', []).service('SessionService', ['$http', |
2 | + function ($http) { | ||
3 | + a = { | ||
4 | + name: "Invité", | ||
5 | + logged: false, | ||
6 | + status: 0, | ||
7 | + changeHandlers: [], | ||
8 | + onChange: function (fun) { | ||
9 | + this.changeHandlers.push(fun) | ||
10 | + }, | ||
11 | + triggerChange: function () { | ||
12 | + for (fun in this.changeHandlers) { | ||
13 | + this.changeHandlers[fun]() | ||
14 | + } | ||
15 | + }, | ||
16 | + updateSessionInfos: function (data) { | ||
17 | + if (typeof data === 'object') { | ||
18 | + console.log("Connected") | ||
19 | + this.logged = true | ||
20 | + this.name = data.login | ||
21 | + } else { | ||
22 | + | ||
23 | + this.logged = false | ||
24 | + } | ||
25 | + this.triggerChange() | ||
26 | + }, | ||
27 | + get: function (cb) { // Fetch infos if needed | ||
28 | + console.log("Session: get") | ||
29 | + if (status == 0) { | ||
30 | + this.status = 1 // Fetching | ||
31 | + _this = this | ||
32 | + // TODO Verify if cookies to prevent uneeded request | ||
33 | + $http.get('/api/session').success(function (body) { | ||
34 | + _this.updateSessionInfos(body) | ||
35 | + if (cb) { | ||
36 | + if (this.logged) { | ||
37 | + cb(null) | ||
38 | + } else { | ||
39 | + cb(body) | ||
40 | + } | ||
41 | + } | ||
42 | + }) | ||
43 | + } else { | ||
44 | + console.warn("get multiple times") | ||
45 | + } | ||
46 | + }, | ||
47 | + connect: function (login, pass, cb) { | ||
48 | + console.log("Session: connecting with login:", login) | ||
49 | + _this = this | ||
50 | + $http.post('/api/session', { | ||
51 | + login: login, | ||
52 | + pass: pass | ||
53 | + }).success(function (body) { | ||
54 | + _this.updateSessionInfos(body) | ||
55 | + if (cb) { | ||
56 | + if (this.logged) { | ||
57 | + cb(null) | ||
58 | + } else { | ||
59 | + cb(body) | ||
60 | + } | ||
61 | + } | ||
62 | + }) | ||
63 | + }, | ||
64 | + disconnect: function () { | ||
65 | + console.log("Session: disconnect", this.name) | ||
66 | + $http.delete('/api/session') | ||
67 | + this.logged = false | ||
68 | + } | ||
5 | } | 69 | } |
70 | + a.get() | ||
71 | + return a | ||
6 | } | 72 | } |
7 | -}) | ||
8 | \ No newline at end of file | 73 | \ No newline at end of file |
74 | +]) | ||
9 | \ No newline at end of file | 75 | \ No newline at end of file |
public/views/connect.html
@@ -5,12 +5,12 @@ | @@ -5,12 +5,12 @@ | ||
5 | <form> | 5 | <form> |
6 | <div class="form-group"> | 6 | <div class="form-group"> |
7 | <label for="login">Identifiant</label> | 7 | <label for="login">Identifiant</label> |
8 | - <input type="text" class="form-control" id="login" placeholder="Identifiant Polytech"> | 8 | + <input type="text" class="form-control" id="login" ng-model="connect.login" placeholder="Identifiant Polytech"> |
9 | </div> | 9 | </div> |
10 | <div class="form-group"> | 10 | <div class="form-group"> |
11 | <label for="pass">Mot de passe</label> | 11 | <label for="pass">Mot de passe</label> |
12 | - <input type="password" class="form-control" id="pass" placeholder="Mot de passe Polytech"> | 12 | + <input type="password" class="form-control" id="pass" ng-model="connect.pass" placeholder="Mot de passe Polytech"> |
13 | </div> | 13 | </div> |
14 | - <button type="submit" class="btn btn-default">Se connecter</button> | 14 | + <button type="submit" class="btn btn-default" ng-click="connect.connect()">Se connecter</button> |
15 | </form> | 15 | </form> |
16 | </div> | 16 | </div> |
17 | \ No newline at end of file | 17 | \ No newline at end of file |
public/views/index.html
@@ -14,6 +14,7 @@ | @@ -14,6 +14,7 @@ | ||
14 | <script src="js/controllers/MembreCtrl.js"></script> | 14 | <script src="js/controllers/MembreCtrl.js"></script> |
15 | <script src="js/services/SessionServ.js"></script> | 15 | <script src="js/services/SessionServ.js"></script> |
16 | <script src="js/controllers/SessionCtrl.js"></script> | 16 | <script src="js/controllers/SessionCtrl.js"></script> |
17 | + <script src="js/controllers/ConnectCtrl.js"></script> | ||
17 | <script src="js/appRoutes.js"></script> | 18 | <script src="js/appRoutes.js"></script> |
18 | <script src="js/app.js"></script> | 19 | <script src="js/app.js"></script> |
19 | </head> | 20 | </head> |
@@ -31,7 +32,7 @@ | @@ -31,7 +32,7 @@ | ||
31 | <ul class="nav navbar-nav navbar-right" ng-controller="SessionController"> | 32 | <ul class="nav navbar-nav navbar-right" ng-controller="SessionController"> |
32 | <li> | 33 | <li> |
33 | <a ng-hide="session.logged" href="/connect">Se connecter</a> | 34 | <a ng-hide="session.logged" href="/connect">Se connecter</a> |
34 | - <a ng-show="session.logged" href="#" ng-click="disconnect()">{{ session.name }} <span class="glyphicon glyphicon-off"></span></a> | 35 | + <a ng-show="session.logged" href="#" ng-click="session.disconnect()">{{ session.name }} <span class="glyphicon glyphicon-off"></span></a> |
35 | </li> | 36 | </li> |
36 | </ul> | 37 | </ul> |
37 | </div> | 38 | </div> |
server.js
@@ -5,6 +5,7 @@ var mongoose = require('mongoose'); | @@ -5,6 +5,7 @@ var mongoose = require('mongoose'); | ||
5 | var morgan = require('morgan'); | 5 | var morgan = require('morgan'); |
6 | var bodyParser = require('body-parser'); | 6 | var bodyParser = require('body-parser'); |
7 | var methodOverride = require('method-override'); | 7 | var methodOverride = require('method-override'); |
8 | +var cookieParser = require('cookie-parser') | ||
8 | 9 | ||
9 | // Application ================================================================ | 10 | // Application ================================================================ |
10 | 11 | ||
@@ -24,6 +25,9 @@ app.use(bodyParser.urlencoded({ | @@ -24,6 +25,9 @@ app.use(bodyParser.urlencoded({ | ||
24 | })); | 25 | })); |
25 | app.use(methodOverride('X-HTTP-Method-Override')); | 26 | app.use(methodOverride('X-HTTP-Method-Override')); |
26 | 27 | ||
28 | +// Cookie-parser | ||
29 | +app.use(cookieParser()) | ||
30 | + | ||
27 | // Dossier public | 31 | // Dossier public |
28 | app.use(express.static(__dirname + '/public')); | 32 | app.use(express.static(__dirname + '/public')); |
29 | 33 |