Skip to content

Instantly share code, notes, and snippets.

@Pajn
Created May 20, 2014 18:47
Show Gist options
  • Save Pajn/fa49bbd82377aafef89d to your computer and use it in GitHub Desktop.
Save Pajn/fa49bbd82377aafef89d to your computer and use it in GitHub Desktop.
register(Request request) {
var user = new User.fromJson(request.json);
if (!user.emailIsValid || !user.hashIsValid) {
return new Response('invalid data', status: Status.ERROR);
}
return db.open().then((_) {
DbCollection users = db.collection('Users');
return users.findOne({'email': user.email}).then((dbUser) {
if (dbUser == null) {
user.strengthenHash();
return users.insert(user.toJson()).then((_) {
db.close();
return new Response('user created');
});
} else {
db.close();
return new Response('email exists', status: Status.ERROR);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment