mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-26 04:08:01 +03:00
Added user authentication and registration
This commit is contained in:
parent
2e28a1a8c2
commit
ab8781d115
2
app.js
2
app.js
@ -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);
|
||||
|
@ -60,7 +60,8 @@
|
||||
client: 'sqlite3',
|
||||
connection: {
|
||||
filename: './core/shared/data/testdb.db'
|
||||
}
|
||||
},
|
||||
debug: true
|
||||
},
|
||||
|
||||
staging: {},
|
||||
|
@ -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');
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
11
core/admin/views/register.hbs
Normal file
11
core/admin/views/register.hbs
Normal 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>
|
@ -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: {}
|
||||
|
@ -63,5 +63,4 @@ module.exports = {
|
||||
"updated_by": 1
|
||||
}
|
||||
]
|
||||
|
||||
};
|
||||
|
@ -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');
|
||||
|
@ -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;
|
||||
}());
|
@ -60,15 +60,11 @@
|
||||
});
|
||||
|
||||
User = Bookshelf.Model.extend({
|
||||
|
||||
tableName: 'users',
|
||||
|
||||
hasTimestamps: true,
|
||||
|
||||
posts: function () {
|
||||
return this.hasMany(Posts, 'created_by');
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Setting = Bookshelf.Model.extend({
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user