Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var checkit = require('checkit');
- var Promise = require('bluebird');
- var bcrypt = Promise.promisifyAll(require('bcrypt'));
- var Customer = bookshelf.Model.extend({
- initialize: function() {
- this.on('saving', this.validateSave);
- },
- validateSave: function() {
- return checkit(rules).run(this.attributes);
- },
- account: function() {
- return this.belongsTo(Account);
- },
- }, {
- login: Promise.method(function(email, password) {
- if (!email || !password) throw new Error('Email and password are both required');
- return new this({email: email.toLowerCase().trim()}).fetch({require: true}).tap(function(customer) {
- return bcrypt.compareAsync(customer.get('password'), password)
- .then(function(res) {
- if (!res) throw new Error('Invalid password');
- });
- });
- })
- });
- Customer.login(email, password)
- .then(function(customer) {
- res.json(customer.omit('password'));
- }).catch(Customer.NotFoundError, function() {
- res.json(400, {error: email + ' not found'});
- }).catch(function(err) {
- console.error(err);
- });
Advertisement
Add Comment
Please, Sign In to add comment