Skip to content

Instantly share code, notes, and snippets.

@cj3kim
Created May 5, 2015 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cj3kim/e9512711a83a57379b36 to your computer and use it in GitHub Desktop.
Save cj3kim/e9512711a83a57379b36 to your computer and use it in GitHub Desktop.
login: Promise.method(function (email, password) {
if (!email || !password) throw new Error("Email and password are both required.");
var storage = { user: null };
var fetchPromise = new this({ email: email.toLowerCase().trim() })
.bind(storage)
.fetch({ require: true })
.then(function (user) {
this.user = user;
return bcrypt.compareAsync(password, user.get('password'))
})
.then(function (samePassword) {
if (!samePassword) throw new Error("Invalid email or password");
return Promise.resolve(this.user);
})
return fetchPromise;
}),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment