Skip to content

Instantly share code, notes, and snippets.

@ykaragol
Last active March 23, 2018 11:51
Show Gist options
  • Save ykaragol/785c3b9a11738c6e9e81ec161aa07d22 to your computer and use it in GitHub Desktop.
Save ykaragol/785c3b9a11738c6e9e81ec161aa07d22 to your computer and use it in GitHub Desktop.
auth-test
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
myService: Ember.inject.service(),
actions:{
login(){
this.get('myService').set('_isAuthenticated', true)
let lastTransition = this.get('myService.lastTransition')
if(!Ember.isBlank(lastTransition)){
lastTransition.retry();
this.set('myService.lastTransition', undefined)
}
},
logout(){
this.get('myService').set('_isAuthenticated', false)
this.transitionToRoute('index')
}
}
});
import Ember from 'ember';
export default Ember.Mixin.create({
myService: Ember.inject.service(),
beforeModel(transition){
if(!this.get('myService').isAuthenticated()){
this.get('myService').set('lastTransition', transition);
this.transitionTo('index')
return
}
}
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('my-route');
this.route('my-route1');
this.route('my-route2');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
});
import Ember from 'ember';
import Auth from '../mixins/auth';
export default Ember.Route.extend(Auth, {
});
import Ember from 'ember';
import Auth from '../mixins/auth';
export default Ember.Route.extend(Auth, {
});
import Ember from 'ember';
import Auth from '../mixins/auth';
export default Ember.Route.extend(Auth, {
});
import Ember from 'ember';
export default Ember.Service.extend({
_isAuthenticated: false,
isAuthenticated(){
return this.get('_isAuthenticated')
}
});
<h1>Welcome to {{appName}}</h1>
<button onclick={{action 'login'}}/>Login</button>
<button onclick={{action 'logout'}}/>Logout</button>
<hr>
<h4>{{if myService._isAuthenticated 'Logged In' 'Not Logged In'}}</h4>
<br>
<br>
{{outlet}}
<br>
<br>
{{link-to 'my-route' 'my-route'}}
{{link-to 'my-route1' 'my-route1'}}
{{link-to 'my-route2' 'my-route2'}}
{
"version": "0.13.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment