-
-
Save LeZuse/3644830 to your computer and use it in GitHub Desktop.
Moon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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': | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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