Skip to content

Instantly share code, notes, and snippets.

@ticking-clock
Created August 21, 2013 14:43
Show Gist options
  • Save ticking-clock/6295394 to your computer and use it in GitHub Desktop.
Save ticking-clock/6295394 to your computer and use it in GitHub Desktop.
Simple Auth for Ember.js
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script src="http://builds.emberjs.com/ember-latest.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<script type="text/x-handlebars" data-template-name="application">
<button {{action "logout"}}>Logout</
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="app">
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="photo">Photo</script>
<script type="text/x-handlebars" data-template-name="post">Post</script>
<script type="text/x-handlebars" data-template-name="login">
Name: {{input type="text" value=username}}
Password: {{input type="password" value=password}}
<button {{action 'login' username password}}>Log in</button>
</script>
</body>
</html>
App = Ember.Application.create();
App.Router.map(function() {
this.resource('app', { path: '/' }, function() {
this.resource('post');
this.resource('photo');
});
this.resource('login');
});
App.AppRoute = Ember.Route.extend({
redirect: function() {
if (!localStorage.auth_token) {
this.transitionTo('login');
}
}
});
App.ApplicationRoute = Ember.Route.extend({
events: {
login: function(username, pass) {
if (pass === "123") {
localStorage.auth_token = "YOU_ARE_AUTHED";
this.transitionTo('post');
}
},
logout: function() {
delete localStorage.auth_token;
this.transitionTo('login');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment