Skip to content

Instantly share code, notes, and snippets.

@zewa666
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zewa666/a6bc9ab8d7b0c927ffca to your computer and use it in GitHub Desktop.
Save zewa666/a6bc9ab8d7b0c927ffca to your computer and use it in GitHub Desktop.
import {Router} from 'aurelia-router';
import bootstrap from 'bootstrap';
import {EventAggregator} from 'aurelia-event-aggregator';
export class App {
static inject() { return [ Router, EventAggregator ];}
constructor(router, ea) {
this.router = router;
this.isLoggedIn = false;
this.ea = ea;
var test = "Welcome";
this.router.configure(config => {
config.title = 'Aurelia';
config.addPipelineStep('authorize', AuthorizeStep);
config.map([
{ route: ['','welcome'], moduleId: './welcome', nav: true, title:test },
{ route: 'flickr', moduleId: './flickr', auth: true, nav: true },
{ route: 'login', moduleId: './login', nav: false },
{ route: 'child-router', moduleId: './child-router', nav: true, title:'Child Router' }
]);
});
this.ea.subscribe('login', (credentials) => {
if(credentials.username === 'test' &&
credentials.pass === 'pass')
this.isLoggedIn = true;
this.router.navigate('flickr');
});
}
}
class AuthorizeStep {
static inject(){
return [App];
}
constructor(app){
this.app = app;
}
run( routingContext, next ) {
if( routingContext.nextInstructions.some( i => i.config.auth ) ) {
if( !this.app.isLoggedIn ) {
routingContext.router.navigate('login');
return next.cancel();
}
return next();
} else {
return next();
}
}
}
import {EventAggregator} from 'aurelia-event-aggregator';
export class Login {
static inject() { return [EventAggregator]; }
constructor(ea) {
this.ea = ea;
}
doLogin() {
this.ea.publish('login', { username: 'test', pass: 'pass' });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment