Advertisement
Guest User

Untitled

a guest
Feb 18th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. import {inject} from 'aurelia-framework';
  2. import {HttpClient} from 'aurelia-fetch-client';
  3. import 'fetch';
  4. import {Router} from 'aurelia-router';
  5. import {AuthResult} from './authResult';
  6. import {Redirect} from 'aurelia-router';
  7.  
  8. @inject(HttpClient, Router)
  9. export class Auth {
  10. constructor(httpClient, router) {
  11. this.httpClient = httpClient;
  12. this.router = router;
  13. this.internalIsLoggedIn = false;
  14. }
  15.  
  16. login(username, password) {
  17.  
  18. if (username === "callum" && password === "password") {
  19. this.router.navigate('products');
  20.  
  21. this.internalIsLoggedIn = true;
  22. }
  23.  
  24. return new AuthResult("Unable to login.");
  25. }
  26.  
  27. get isLoggedIn() { return this.internalIsLoggedIn; }
  28. }
  29.  
  30. @inject(Auth)
  31. export class AuthRouterPipelineStep {
  32. constructor(auth) {
  33. this.auth = auth;
  34. }
  35.  
  36. run(navigationInstruction, next) {
  37. console.log("Navigating");
  38. if (navigationInstruction
  39. .getAllInstructions()
  40. .some(i => i.config.settings.roles.indexOf('public') === -1))
  41. {
  42. var isLoggedIn = this.auth.isLoggedIn();
  43. if (!isLoggedIn) {
  44. return next.cancel(new Redirect('welcome'));
  45. }
  46. }
  47.  
  48. return next();
  49. }
  50. }
  51.  
  52. import {Auth, AuthRouterPipelineStep} from './auth/auth';
  53. import {inject} from 'aurelia-framework';
  54. import {Redirect} from 'aurelia-router';
  55.  
  56. @inject(Auth)
  57. export class App {
  58. constructor(auth) {
  59. this.auth = auth;
  60. }
  61.  
  62. get isLoggedIn() { return this.auth.isLoggedIn; }
  63.  
  64. configureRouter(config, router) {
  65. config.title = 'Reaper';
  66. config.addPipelineStep('authorise', AuthRouterPipelineStep);
  67. config.map([
  68. { route: ['', 'welcome'], name: 'welcome', moduleId: 'welcome', nav: true, title: 'Home', settings: { icon: 'fa-home', roles: ['public'] } },
  69. { route: 'contacts', name: 'contacts', moduleId: './contacts/index', nav: true, title: 'Contacts', settings: { icon: 'fa-' } },
  70. { route: 'companies', name: 'companies', moduleId: './companies/index', nav: true, title: 'Companies', settings: { icon: 'fa-' } },
  71. { route: 'products', name: 'products', moduleId: './products/index', nav: true, title: 'Products', settings: { icon: 'fa-' } }
  72. ]);
  73.  
  74. this.router = router;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement