diff --git a/app/controllers/sessions.js b/app/controllers/sessions.js index 07a26c7..9ced69b 100644 --- a/app/controllers/sessions.js +++ b/app/controllers/sessions.js @@ -3,13 +3,11 @@ var Session = require('../models/session'); var sessions = {} sessions.find = function (id, cb) { - Session.find({ - '_id': id - }, cb) + Session.findById(id, cb) } sessions.valid = function (session) { - return session.started + 3600 > Date.now() + return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date() } sessions.delete = function (id, cb) { @@ -18,20 +16,21 @@ sessions.delete = function (id, cb) { }, cb); } -sessions.close = function (id, cb) { - -} - sessions.verify = function (id, cb) { - session.find(id, function (err, session) { + _this = this + Session.findById(id, function (err, session) { if (err) { cb('error'); } else { - if (sessions.valid(session)) { - cb(session); + if (session) { + if (sessions.valid(session)) { + cb(err, session); + } else { + cb('expired'); + _this.delete(id) + } } else { - cb('expired'); - sessions.delete(id) + cb('unknown') } } }); diff --git a/app/routes/api.js b/app/routes/api.js index 15b3e33..3ab784d 100644 --- a/app/routes/api.js +++ b/app/routes/api.js @@ -7,7 +7,13 @@ var api = express() // Sessions api.get('/session', function (req, res) { // Informations sur la session if (req.cookies && req.cookies.session) { - res.send(sessions.verify(req.cookies.session)) + sessions.verify(req.cookies.session, function (err, session) { + if (err) { + res.send(err) + } else { + res.send(session) + } + }) // TODO si pas bon : res.clearCookie('session') } else { res.send('missing'); @@ -24,9 +30,10 @@ api.post('/session', function (req, res) { // Se connecter }) api.delete('/session', function (req, res) { // Se déconnecter - if (req.cookies) { - sessions.delete(req.cookies.id, function () { + if (req.cookies.session) { + sessions.delete(req.cookies.session, function () { res.clearCookie('session'); + res.end() }) } else { res.send('missing') diff --git a/package.json b/package.json index f78a234..e42aabf 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "main": "server.js", "dependencies": { "body-parser": "^1.12.0", + "cookie-parser": "^1.3.4", "express": "^4.12.2", "method-override": "^2.3.1", "mongoose": "^3.8.25", diff --git a/public/js/app.js b/public/js/app.js index 1021af2..949ac57 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1 +1 @@ -angular.module('ciApp', ['ngRoute', 'appRoutes', 'MembreCtrl', 'SessionsServ', 'SessionsCtrl']); \ No newline at end of file +angular.module('ciApp', ['ngRoute', 'appRoutes', 'MembreCtrl', 'SessionsServ', 'SessionsCtrl', 'ConnectCtrl']); \ No newline at end of file diff --git a/public/js/appRoutes.js b/public/js/appRoutes.js index 55d23e3..dc78578 100644 --- a/public/js/appRoutes.js +++ b/public/js/appRoutes.js @@ -1,7 +1,6 @@ // public/js/appRoutes.js angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { - $routeProvider .when('/', { templateUrl: 'views/home.html' @@ -11,8 +10,8 @@ angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', controller: 'MembreController' }) .when('/connect', { - templateUrl: 'views/connect.html' - // controller: 'ConnectController' + templateUrl: 'views/connect.html', + controller: 'ConnectController' }); $locationProvider.html5Mode(true); diff --git a/public/js/controllers/ConnectCtrl.js b/public/js/controllers/ConnectCtrl.js new file mode 100644 index 0000000..e861ddb --- /dev/null +++ b/public/js/controllers/ConnectCtrl.js @@ -0,0 +1,9 @@ +angular.module('ConnectCtrl', []).controller('ConnectController', ['$scope', 'SessionService', + function ($scope, SessionService) { + $scope.connect = { + connect: function () { + SessionService.connect($scope.connect.login, $scope.connect.pass) + } + } + } +]); \ No newline at end of file diff --git a/public/js/controllers/SessionCtrl.js b/public/js/controllers/SessionCtrl.js index 778de72..7885ee7 100644 --- a/public/js/controllers/SessionCtrl.js +++ b/public/js/controllers/SessionCtrl.js @@ -1,12 +1,11 @@ -angular.module('SessionsCtrl', []).controller('SessionController', ['$scope', '$http', 'SessionService', - function ($scope, $http, SessionService) { - $scope.session = { - logged: true, - name: SessionService.get() - } - - $scope.disconnect = function () { - $scope.session.logged = false; - } +angular.module('SessionsCtrl', []).controller('SessionController', ['$scope', 'SessionService', + function ($scope, SessionService) { + $scope.session = SessionService + // $scope.session.onChange(function () { + // // TODO + // }) + // $scope.$on("$destroy", function () { + // // TODO + // }) } ]); \ No newline at end of file diff --git a/public/js/services/SessionServ.js b/public/js/services/SessionServ.js index 3394eff..c66c44e 100644 --- a/public/js/services/SessionServ.js +++ b/public/js/services/SessionServ.js @@ -1,7 +1,73 @@ -angular.module('SessionsServ', []).service('SessionService', function () { - return { - get: function () { - return 'BANANE'; +angular.module('SessionsServ', []).service('SessionService', ['$http', + function ($http) { + a = { + name: "Invité", + 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 + this.name = data.login + } 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 + } } + a.get() + return a } -}) \ No newline at end of file +]) \ No newline at end of file diff --git a/public/views/connect.html b/public/views/connect.html index 8aa4f78..67fd8c0 100644 --- a/public/views/connect.html +++ b/public/views/connect.html @@ -5,12 +5,12 @@
\ No newline at end of file diff --git a/public/views/index.html b/public/views/index.html index 978805c..13317ae 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -14,6 +14,7 @@ + @@ -31,7 +32,7 @@ diff --git a/server.js b/server.js index 389d93a..ce2f684 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,7 @@ var mongoose = require('mongoose'); var morgan = require('morgan'); var bodyParser = require('body-parser'); var methodOverride = require('method-override'); +var cookieParser = require('cookie-parser') // Application ================================================================ @@ -24,6 +25,9 @@ app.use(bodyParser.urlencoded({ })); app.use(methodOverride('X-HTTP-Method-Override')); +// Cookie-parser +app.use(cookieParser()) + // Dossier public app.use(express.static(__dirname + '/public')); -- libgit2 0.21.2