Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {inject} from 'aurelia-framework';
- import {HttpClient} from 'aurelia-fetch-client';
- import 'fetch';
- import {Router} from 'aurelia-router';
- import {AuthResult} from './authResult';
- import {Redirect} from 'aurelia-router';
- @inject(HttpClient, Router)
- export class Auth {
- constructor(httpClient, router) {
- this.httpClient = httpClient;
- this.router = router;
- this.internalIsLoggedIn = false;
- }
- login(username, password) {
- if (username === "callum" && password === "password") {
- this.router.navigate('products');
- this.internalIsLoggedIn = true;
- }
- return new AuthResult("Unable to login.");
- }
- get isLoggedIn() { return this.internalIsLoggedIn; }
- }
- @inject(Auth)
- export class AuthRouterPipelineStep {
- constructor(auth) {
- this.auth = auth;
- }
- run(navigationInstruction, next) {
- console.log("Navigating");
- if (navigationInstruction
- .getAllInstructions()
- .some(i => i.config.settings.roles.indexOf('public') === -1))
- {
- var isLoggedIn = this.auth.isLoggedIn();
- if (!isLoggedIn) {
- return next.cancel(new Redirect('welcome'));
- }
- }
- return next();
- }
- }
- import {Auth, AuthRouterPipelineStep} from './auth/auth';
- import {inject} from 'aurelia-framework';
- import {Redirect} from 'aurelia-router';
- @inject(Auth)
- export class App {
- constructor(auth) {
- this.auth = auth;
- }
- get isLoggedIn() { return this.auth.isLoggedIn; }
- configureRouter(config, router) {
- config.title = 'Reaper';
- config.addPipelineStep('authorise', AuthRouterPipelineStep);
- config.map([
- { route: ['', 'welcome'], name: 'welcome', moduleId: 'welcome', nav: true, title: 'Home', settings: { icon: 'fa-home', roles: ['public'] } },
- { route: 'contacts', name: 'contacts', moduleId: './contacts/index', nav: true, title: 'Contacts', settings: { icon: 'fa-' } },
- { route: 'companies', name: 'companies', moduleId: './companies/index', nav: true, title: 'Companies', settings: { icon: 'fa-' } },
- { route: 'products', name: 'products', moduleId: './products/index', nav: true, title: 'Products', settings: { icon: 'fa-' } }
- ]);
- this.router = router;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement