Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Début login

  • Loading branch information...
commit 72ae84b1f55df855dba09f9417a0b8f3eef4e429 1 parent 9846c3f
François Robichet authored
View
48 app.js
@@ -35,6 +35,8 @@ var express = require('express')
, zip = require('node-native-zip')
, argv = require('optimist').argv
, less = require('less')
+ , passport = require('passport')
+ , LocalStrategy = require('passport-local').Strategy
;
/**
@@ -93,6 +95,8 @@ app.configure(function() {
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(express.bodyParser({ keepExtensions: true, uploadDir: nconf.get('documents:dirs:tmp') }));
app.use(express.methodOverride());
+ app.use(passport.initialize());
+ app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
@@ -119,6 +123,7 @@ app.configure('production', function() {
models.define(mongoose, function() {
app.Document = Document = mongoose.model('Document');
app.Tag = Tag = mongoose.model('Tag');
+ app.User = User = mongoose.model('User');
db = mongoose.connect(nconf.get('mongo:connection'));
});
@@ -179,6 +184,9 @@ app.get('/documents', function(req, res) {
*/
app.get('/documents/:id', function(req, res) {
+ User.findById('4f3a8b4f514f99e31a000001', function(err, user) {
+
+ });
Document.findById(req.params.id, function(err, doc) {
res.respond(err || doc.toJSON2(), err ? 500 : ( doc ? 200 : 404 ));
});
@@ -457,10 +465,12 @@ app.get('/documents/:id/file', function(req, res, next) {
// }):
+
/**
* TAGS Routes :
*/
+
/**
* GET /tags : all tags
*/
@@ -492,6 +502,44 @@ app.del('/tags/:id', function(req, res) {
});
+
+
+/**
+ * USERS Routes :
+ */
+
+
+// @TODO http://passportjs.org/guide/authenticate.html
+passport.use(new LocalStrategy(
+ function(login, password, done) {
+ User.findOne({ email: login }, function (err, user) {
+ if (err) { return done(err); }
+ if (!user) { return done(null, false); }
+ if (!user.validPassword(password)) { return done(null, false); }
+ return done(null, user);
+ });
+ }
+));
+/**
+ * GET login: connect the user
+ */
+
+app.get('/login', function(req, res) {
+ User.findOne({ email: req.query.login }, function(err, user) {
+ // Si l'on a trouvé l'utilisateurr
+ if (user) {
+ // Si le mot de passe est correct
+ if (user.validPassword(req.query.password)) {
+ res.send(user, 200);
+ } else {
+ res.send('Mauvais mot de passe', 200);
+ }
+ } else {
+ res.send('Pas d\'utilisateur', 200);
+ }
+ });
+});
+
/**
* Documentation :
*/
View
30 models.js
@@ -4,6 +4,7 @@ var fs = require('fs')
, im = require('imagemagick')
, _ = require('underscore')
, nconf = require('nconf')
+ , sha1 = require('sha1')
, extrafields = require('./extrafields')
;
@@ -366,6 +367,34 @@ function define(mongoose, fn) {
return this.find(query).sort('label', 'ascending').execFind(callback);
};
+
+ /*
+ * Schemas : User
+ */
+
+ User_Schema = new Schema({
+ name: String
+ , email: String
+ , salt: String
+ , password: String
+ // @TODO gérer les roles
+ })
+ .pre('save', function(next) {
+ if (!this.salt) {
+ this.salt = sha1(this.email + +new Date);
+ }
+ this.password = sha1(this.salt + this.password);
+ next();
+ });
+
+ // Valid the password
+ User_Schema
+ .virtual('validPassword')
+ .get(function() {
+ return function(password) {
+ return sha1(this.salt + password) === this.password;
+ }
+ })
/**
* Collections' declaration
@@ -399,6 +428,7 @@ function define(mongoose, fn) {
var Tag = mongoose.model('Tag', Tag_Schema);
+ var User = mongoose.model('User', User_Schema);
// Launch callback
fn();
View
3  package.json
@@ -13,5 +13,8 @@
, "nconf": ">=0.5.0"
, "optimist": ">=0.3.0"
, "less": ">=1.2.0"
+ , "sha1": ">=0.0.1"
+ , "passport": ">=0.1.6"
+ , "passport-local": ">=0.1.0"
}
}
View
6 public/css/bootstrap.css
@@ -411,6 +411,12 @@ button.btn::-moz-focus-inner,input[type=submit].btn::-moz-focus-inner{padding:0;
.label.notice{background-color:#62cffc;}
.label .iconic{margin-right:5px;}
header{margin-bottom:40px;}
+.topbar .dropdown-login>li{display:none;}
+.dropdown-login.notLogged>li:last-child{display:block;}
+.dropdown-login:not(.notLogged)>li:first-child{display:block;}
+.dropdown-login>:last-child>.dropdown-menu{padding:10px 15px;}
+.dropdown-login input{width:auto;margin:0 0 5px;}
+.dropdown-login button{width:auto;margin-top:10px;}
#content{position:relative;margin-top:20px;}
#content section h3{color:#ffffff;background-color:#141c24;background-color:#141c24;background-repeat:repeat-x;background-image:-khtml-gradient(linear, left top, left bottom, from(#2e3f52), to(#141c24));background-image:-moz-linear-gradient(top, #2e3f52, #141c24);background-image:-ms-linear-gradient(top, #2e3f52, #141c24);background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #2e3f52), color-stop(100%, #141c24));background-image:-webkit-linear-gradient(top, #2e3f52, #141c24);background-image:-o-linear-gradient(top, #2e3f52, #141c24);background-image:linear-gradient(top, #2e3f52, #141c24);filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#2e3f52', endColorstr='#141c24', GradientType=0);-webkit-box-shadow:0 1px 3px rgba(0, 0, 0, 0.25),inset 0 -1px 0 rgba(0, 0, 0, 0.1);-moz-box-shadow:0 1px 3px rgba(0, 0, 0, 0.25),inset 0 -1px 0 rgba(0, 0, 0, 0.1);box-shadow:0 1px 3px rgba(0, 0, 0, 0.25),inset 0 -1px 0 rgba(0, 0, 0, 0.1);-webkit-border-radius:3px 3px 0 0;-moz-border-radius:3px 3px 0 0;border-radius:3px 3px 0 0;padding-left:10px;padding-right:10px;font-family:'lato',"Helvetica Neue",Helvetica,Arial,sans-serif;font-weight:normal;}
#content section h3 .iconic{float:right;font-size:1.6em;color:#ffffff;}
View
4 views/index.html
@@ -106,8 +106,8 @@
<ul class="dropdown-menu">
<li>
<form class="" action="/?" method="get">
- <input type="text" placeholder="Login" name="login">
- <input type="password" placeholder="Password" name="password">
+ <input type="text" placeholder="Email" name="login">
+ <input type="password" placeholder="Mot de passe" name="password">
<button type="submit" class="btn info">Se connecter</button>
</form>
</li>
Please sign in to comment.
Something went wrong with that request. Please try again.