Commit 10852373bc42797ce984639ed67dec2f066d9b3c
1 parent
a7189c82
Session contrôleur serveur & API
Showing
4 changed files
with
119 additions
and
1 deletions
Show diff stats
app/controllers/membres.js
@@ -14,7 +14,7 @@ membres.add = function (data, cb) { | @@ -14,7 +14,7 @@ membres.add = function (data, cb) { | ||
14 | }, cb); | 14 | }, cb); |
15 | } | 15 | } |
16 | 16 | ||
17 | -membres.delete = function (id, cb) { | 17 | +membres.remove = function (id, cb) { |
18 | Membre.remove({ | 18 | Membre.remove({ |
19 | _id: id | 19 | _id: id |
20 | }, cb); | 20 | }, cb); |
@@ -0,0 +1,75 @@ | @@ -0,0 +1,75 @@ | ||
1 | +var Session = require('../models/session'); | ||
2 | + | ||
3 | +var sessions = {} | ||
4 | + | ||
5 | +sessions.find = function (id, cb) { | ||
6 | + Session.find({ | ||
7 | + '_id': id | ||
8 | + }, cb) | ||
9 | +} | ||
10 | + | ||
11 | +sessions.valid = function (session) { | ||
12 | + return session.started + 3600 > Date.now() | ||
13 | +} | ||
14 | + | ||
15 | +sessions.delete = function (id, cb) { | ||
16 | + Session.remove({ | ||
17 | + _id: id | ||
18 | + }, cb); | ||
19 | +} | ||
20 | + | ||
21 | +sessions.close = function (id, cb) { | ||
22 | + | ||
23 | +} | ||
24 | + | ||
25 | +sessions.verify = function (id, cb) { | ||
26 | + session.find(id, function (err, session) { | ||
27 | + if (err) { | ||
28 | + cb('error'); | ||
29 | + } else { | ||
30 | + if (sessions.valid(session)) { | ||
31 | + cb(session); | ||
32 | + } else { | ||
33 | + cb('expired'); | ||
34 | + sessions.delete(id) | ||
35 | + } | ||
36 | + } | ||
37 | + }); | ||
38 | +} | ||
39 | + | ||
40 | +sessions.create = function (login, cb) { | ||
41 | + Session.create({ | ||
42 | + login: login | ||
43 | + }, cb); | ||
44 | +} | ||
45 | + | ||
46 | +sessions.login = function (data, cb) { | ||
47 | + // DUMMY | ||
48 | + if (data.login == 'cool' && data.pass == 'cool') { | ||
49 | + cb(null, true); | ||
50 | + } else { | ||
51 | + cb(null, false); | ||
52 | + } | ||
53 | +} | ||
54 | + | ||
55 | +sessions.open = function (data, cb) { | ||
56 | + sessions.login(data, function (err, res) { | ||
57 | + if (err) { | ||
58 | + cb('error') | ||
59 | + } else { | ||
60 | + if (res) { | ||
61 | + sessions.create(data.login, function (err, session) { | ||
62 | + if (err) { | ||
63 | + cb('error'); | ||
64 | + } else { | ||
65 | + cb(session); | ||
66 | + } | ||
67 | + }); | ||
68 | + } else { | ||
69 | + cb('invalid') | ||
70 | + } | ||
71 | + } | ||
72 | + }); | ||
73 | +} | ||
74 | + | ||
75 | +module.exports = sessions; | ||
0 | \ No newline at end of file | 76 | \ No newline at end of file |
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +var mongoose = require('mongoose'); | ||
2 | + | ||
3 | +module.exports = mongoose.model('Session', { | ||
4 | + login: { // On récupèrera le nom via les passwd | ||
5 | + type: String, | ||
6 | + default: 'login' | ||
7 | + }, | ||
8 | + started: { | ||
9 | + type: Date, | ||
10 | + default: Date.now | ||
11 | + } | ||
12 | +}); | ||
0 | \ No newline at end of file | 13 | \ No newline at end of file |
app/routes/api.js
1 | var membres = require('../controllers/membres'); | 1 | var membres = require('../controllers/membres'); |
2 | +var sessions = require('../controllers/sessions'); | ||
2 | var express = require('express'); | 3 | var express = require('express'); |
3 | 4 | ||
4 | var api = express() | 5 | var api = express() |
5 | 6 | ||
7 | +// Sessions | ||
8 | +api.get('/session', function (req, res) { // Informations sur la session | ||
9 | + if (req.cookies && req.cookies.session) { | ||
10 | + res.send(sessions.verify(req.cookies.session)) | ||
11 | + // TODO si pas bon : res.clearCookie('session') | ||
12 | + } else { | ||
13 | + res.send('missing'); | ||
14 | + } | ||
15 | +}); | ||
16 | + | ||
17 | +api.post('/session', function (req, res) { // Se connecter | ||
18 | + sessions.open(req.body, function (status) { | ||
19 | + if (typeof status === 'object') { | ||
20 | + res.cookie('session', status._id); | ||
21 | + } | ||
22 | + res.send(status); | ||
23 | + }) | ||
24 | +}) | ||
25 | + | ||
26 | +api.delete('/session', function (req, res) { // Se déconnecter | ||
27 | + if (req.cookies) { | ||
28 | + sessions.delete(req.cookies.id, function () { | ||
29 | + res.clearCookie('session'); | ||
30 | + }) | ||
31 | + } else { | ||
32 | + res.send('missing') | ||
33 | + } | ||
34 | +}) | ||
35 | + | ||
36 | + | ||
6 | // Membres | 37 | // Membres |
7 | api.get('/membres', function (req, res) { // Liste des membres | 38 | api.get('/membres', function (req, res) { // Liste des membres |
8 | membres.list(function (err, membres) { | 39 | membres.list(function (err, membres) { |