Merge pull request #50 from javorszky/master

User authentication, #10
This commit is contained in:
Hannah Wolfe 2013-05-22 12:37:16 -07:00
commit 503e9fb391
10 changed files with 107 additions and 13 deletions

2
app.js
View File

@ -74,7 +74,9 @@
ghost.app().get(/^\/logout\/?$/, admin.logout);
ghost.app().get('/ghost/login/', admin.login);
ghost.app().get('/ghost/register/', admin.register);
ghost.app().post('/ghost/login/', admin.auth);
ghost.app().post('/ghost/register', admin.doRegister);
ghost.app().get('/ghost/editor/:id', auth, admin.editor);
ghost.app().get('/ghost/editor', auth, admin.editor);
ghost.app().get('/ghost/blog', auth, admin.blog);

View File

@ -60,7 +60,8 @@
client: 'sqlite3',
connection: {
filename: './core/shared/data/testdb.db'
}
},
debug: true
},
staging: {},

View File

@ -61,11 +61,34 @@
});
},
'auth': function (req, res) {
if (req.body.email === 'ghostadmin' && req.body.password === 'Wh0YouGonnaCall?') {
req.session.user = "ghostadmin";
res.redirect(req.query.redirect || '/ghost/');
} else {
res.redirect('/ghost/login/');
console.log(req.body);
api.users.find({email: req.body.email, pw: req.body.password}).then(function (user) {
if (user) {
console.log('user found: ', user);
req.session.user = "ghostadmin";
res.redirect(req.query.redirect || '/ghost/');
} else {
res.redirect('/ghost/login/');
}
});
},
'register': function (req, res) {
res.render('register', {
bodyClass: 'ghost-login',
hideNavbar: true,
adminNav: setSelected(adminNavbar, 'login')
});
},
'doRegister': function (req, res) {
// console.log(req.body);
if (req.body.email !== '' && req.body.password.length > 5) {
// console.log('okay, this is happening');
api.users.add({email: req.body.email, password: req.body.password}).then(function (user) {
console.log('user added', user);
res.redirect('/ghost/login/');
});
}
},
'logout': function (req, res) {
@ -139,6 +162,16 @@
}
res.redirect('/ghost/debug');
});
},
'newUser': function (req, res) {
ghost.dataProvider().addNewUser(req, function (error) {
if (error) {
req.flash('error', error);
} else {
req.flash('success', 'User Added');
}
});
}
}
};

View File

@ -0,0 +1,11 @@
{{!< default}}
<img class="login-logo" src="/core/admin/assets/img/logo.png" alt="" />
<form id="register" method="post">
<div class="email-wrap">
<input class="email" type="text" placeholder="Email Address" name="email">
</div>
<div class="password-wrap">
<input class="password" type="password" placeholder="Password" name="password">
</div>
<button class="button-save" type="submit">Register</button>
</form>

View File

@ -48,7 +48,14 @@
};
// # Users
users = {};
users = {
add: function (postData) {
return when.call(ghost.dataProvider().users.add, postData);
},
find: function (postData) {
return when.call(ghost.dataProvider().users.check, postData);
}
};
// settings: {},
// categories: {},
// post_categories: {}

View File

@ -63,5 +63,4 @@ module.exports = {
"updated_by": 1
}
]
};

View File

@ -35,6 +35,7 @@
t.string('username');
t.string('first_name');
t.string('last_name');
t.string('password');
t.string('email_address');
t.string('profile_picture');
t.string('cover_picture');

View File

@ -8,6 +8,7 @@
var knex = require('./knex_init'),
models = require('./models'),
bcrypt = require('bcrypt'),
DataProvider,
instance;
@ -26,6 +27,7 @@
};
DataProvider.prototype.posts = function () { };
DataProvider.prototype.users = function () { };
/**
* Naive find all
@ -55,6 +57,7 @@
* @param callback
*/
DataProvider.prototype.posts.add = function (_post, callback) {
console.log(_post);
models.Post.forge(_post).save().then(function (post) {
callback(null, post);
}, callback);
@ -80,5 +83,45 @@
});
};
/**
* Naive user add
* @param _user
* @param callback
*
* Could probably do with some refactoring, but it works right now.
*/
DataProvider.prototype.users.add = function (_user, callback) {
console.log('outside of forge', _user);
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(_user.password, salt, function (err, hash) {
var test = {
"password": hash,
"email_address": _user.email
};
new models.User(test).save().then(function (user) {
console.log('within the forge for the user bit', user);
callback(null, user);
}, callback);
});
});
};
DataProvider.prototype.users.check = function (_userdata, callback) {
var test = {
email_address: _userdata.email
};
models.User.forge(test).fetch().then(function (user) {
var _user;
bcrypt.compare(_userdata.pw, user.attributes.password, function (err, res) {
if (res) {
_user = user;
} else {
_user = false;
}
callback(null, _user);
});
});
};
module.exports = DataProvider;
}());

View File

@ -60,15 +60,11 @@
});
User = Bookshelf.Model.extend({
tableName: 'users',
hasTimestamps: true,
posts: function () {
return this.hasMany(Posts, 'created_by');
}
});
Setting = Bookshelf.Model.extend({

View File

@ -18,7 +18,8 @@
"sqlite3": "2.1.x",
"bookshelf": "0.1.x",
"knex": "0.1.x",
"when": "2.1.x"
"when": "2.1.x",
"bcrypt": "~0.7.5"
},
"devDependencies": {
"grunt": "0.4.x",