Skip to content

Instantly share code, notes, and snippets.

@LeZuse
Created September 5, 2012 21:11
Show Gist options
  • Select an option

  • Save LeZuse/3644830 to your computer and use it in GitHub Desktop.

Select an option

Save LeZuse/3644830 to your computer and use it in GitHub Desktop.
Moon
var ApplicationController = function (users) {
this.$scope.current_user = users.index('identity').one();
};
ApplicationController.prototype.logout = function () {
delete this.$scope.current_user;
};
var SigninFormController = function (authenticator, users) {
this.$root.on('submit', function () {
authenticator.auth(this.$scope.email, this.$scope.password, function (user) {
user['identity'] = 1;
users.save(user, { online: false });
this.redirectTo('/');
});
}, this);
};
var Authenticator = function (users, session) {
this.users = users;
this.session = session;
};
Authenticator.prototype.$serverside = true;
Authenticator.prototype.auth = function (email, password, callback) {
var user = this.users.index('email').one(email);
user.on('ready', function () {
if (user.password_hash === sha1(password)) {
this.session.user_id = user.id;
callback(user);
}
}, this);
};
// main
var Authenticator = require('./app/services/authenticator');
app.ioc.addService('users', new UserRepository());
app.ioc.addService('auth', new Authenticator());
app.router.routes = {
$layout: app.view('layout'),
'/': app.view('index'),
'/members': app.view('member/index'),
'/posts': app.view('post/index'),
'/api': {
'/posts': app.controller('api/PostController'),
'/posts/:id':
'/posts/:id/comments':
}
};
<html m:control="ApplicationController">
{if !current_user}
<form m:control="SigninFormController">
<input type="text" name="email">
<input type="password" name="password">
<button type="submit">Login!</button>
</form>
{else}
<p>Hi, {current_user.name} - <a m:click="logout()">Logout</a>
{/if}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment