diff --git a/app/controllers/membres.js b/app/controllers/membres.js index c0dc7cb..3103a8f 100644 --- a/app/controllers/membres.js +++ b/app/controllers/membres.js @@ -14,7 +14,7 @@ membres.add = function (data, cb) { }, cb); } -membres.delete = function (id, cb) { +membres.remove = function (id, cb) { Membre.remove({ _id: id }, cb); diff --git a/app/controllers/sessions.js b/app/controllers/sessions.js new file mode 100644 index 0000000..07a26c7 --- /dev/null +++ b/app/controllers/sessions.js @@ -0,0 +1,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; \ No newline at end of file diff --git a/app/models/session.js b/app/models/session.js new file mode 100644 index 0000000..2c4838d --- /dev/null +++ b/app/models/session.js @@ -0,0 +1,12 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('Session', { + login: { // On récupèrera le nom via les passwd + type: String, + default: 'login' + }, + started: { + type: Date, + default: Date.now + } +}); \ No newline at end of file diff --git a/app/routes/api.js b/app/routes/api.js index d97ee9a..15b3e33 100644 --- a/app/routes/api.js +++ b/app/routes/api.js @@ -1,8 +1,39 @@ var membres = require('../controllers/membres'); +var sessions = require('../controllers/sessions'); var express = require('express'); 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)) + // TODO si pas bon : res.clearCookie('session') + } else { + res.send('missing'); + } +}); + +api.post('/session', function (req, res) { // Se connecter + sessions.open(req.body, function (status) { + if (typeof status === 'object') { + res.cookie('session', status._id); + } + res.send(status); + }) +}) + +api.delete('/session', function (req, res) { // Se déconnecter + if (req.cookies) { + sessions.delete(req.cookies.id, function () { + res.clearCookie('session'); + }) + } else { + res.send('missing') + } +}) + + // Membres api.get('/membres', function (req, res) { // Liste des membres membres.list(function (err, membres) { -- libgit2 0.21.2