Guest User

Untitled

a guest
Nov 24th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1. return new Redirect('login');
  2.  
  3. aurelia-logging-console.js:47 ERROR [app-router] Error: Expected router pipeline to return a navigation result, but got [{"url":"login","options":{"trigger":true,"replace":true},"shouldContinueProcessing":false}] instead.
  4. at processResult (aurelia-router.js:1761)
  5. at aurelia-router.js:1725
  6. at <anonymous>
  7.  
  8. import { Aurelia, PLATFORM, autoinject } from "aurelia-framework";
  9. import {
  10. Redirect,
  11. NavigationInstruction,
  12. Router,
  13. RouterConfiguration,
  14. Next
  15. } from "aurelia-router";
  16.  
  17. import { AuthService } from "../../auth/auth-service";
  18. //import { Clients } from '../../public/components/login/login'
  19. @autoinject
  20. export class App {
  21. public router: Router;
  22.  
  23. private TOKEN_KEY = "session";
  24.  
  25. configureRouter(config: RouterConfiguration, router: Router): void {
  26. this.router = router;
  27. config.title = "Aurelia";
  28. config.addAuthorizeStep(AuthorizeStep);
  29.  
  30. config.map([
  31. {
  32. route: ["", "scheduler"],
  33. name: "scheduler",
  34. settings: {
  35. icon: "scheduler",
  36. auth: true,
  37. roles: ["Employee", "Admin"]
  38. },
  39. moduleId: PLATFORM.moduleName("../components/scheduler/scheduler"),
  40. nav: true,
  41. title: "scheduler"
  42. },
  43. {
  44. route: "clients",
  45. name: "clients",
  46. moduleId: PLATFORM.moduleName(
  47. "../components/clients/clientList/clientList"
  48. ),
  49. title: "Clients",
  50. nav: true,
  51. settings: {
  52. nav: [
  53. { href: "#clients/clientsList", title: "Client List" },
  54. { href: "#clients/Create", title: "Create Client" }
  55. ],
  56. auth: true,
  57. roles: ["Employee", "Admin"],
  58. pos: "left"
  59. }
  60. },
  61. {
  62. route: "clients/ClientsList",
  63. name: "clientList",
  64. moduleId: PLATFORM.moduleName(
  65. "../components/clients/clientList/clientList"
  66. ),
  67. settings: {
  68. auth: true,
  69. roles: ["Employee", "Admin"]
  70. }
  71. },
  72. {
  73. route: "clients/create",
  74. name: "aboutTeam",
  75. moduleId: PLATFORM.moduleName(
  76. "../components/clients/clientCreate/clientCreate"
  77. ),
  78. settings: {
  79. auth: true,
  80. roles: ["Employee", "Admin"]
  81. }
  82. },
  83. {
  84. route: "logout",
  85. name: "logout",
  86. settings: {
  87. icon: "user",
  88. auth: true,
  89. roles: ["Employee", "Admin"],
  90. pos: "right"
  91. },
  92. moduleId: PLATFORM.moduleName("../components/auth/logout/logout"),
  93. nav: true,
  94. title: "Logout"
  95. },
  96. {
  97. route: "not-found",
  98. name: "not-found",
  99. settings: {
  100. auth: true,
  101. roles: ["Employee", "Admin"]
  102. },
  103. moduleId: PLATFORM.moduleName("../components/notFound/notFound"),
  104. nav: false,
  105. title: "Not Found"
  106. },
  107. {
  108. route: "login",
  109. name: "login",
  110. settings: {
  111. icon: "user",
  112. auth: true,
  113. roles: ["Employee", "Admin"],
  114. pos: "right"
  115. },
  116. moduleId: PLATFORM.moduleName("../../public/components/login/login"),
  117. nav: true,
  118. title: "login"
  119. }
  120. ]);
  121.  
  122. config.mapUnknownRoutes("not-found");
  123. }
  124. }
  125.  
  126. @autoinject
  127. class AuthorizeStep {
  128. private endDate: any;
  129. static loginFragment = '../../public/components/login/login';
  130.  
  131. constructor(
  132. private authService: AuthService,
  133. private router: Router,
  134. private aurelia: Aurelia
  135. ) { }
  136.  
  137.  
  138. run(navigationInstruction: NavigationInstruction, next: Next): Promise<any> {
  139. return Promise.resolve()
  140. .then(() => this.checkAuthentication(navigationInstruction, next))
  141. .then(result => result || this.checkAuthorization(navigationInstruction, next))
  142. .then(result => result || this.checkOrigin(navigationInstruction, next))
  143. .then(result => result || next());
  144. }
  145.  
  146. checkAuthentication(navigationInstruction, next) {
  147. // Do we have a JWT?
  148. const session = this.authService.getIdentity();
  149. if (!session) {
  150. this.forceReturnToPublic(next); // No JWT - back to the public root.
  151. }
  152. console.log("CHECKaUTHENTICATION: ", navigationInstruction.getAllInstructions().some(i => i.config.settings.auth) )
  153. if (navigationInstruction.getAllInstructions().some(i => i.config.settings.auth)) {
  154. // Is the token valid?
  155. if (this.authService.hasTokenExpired(session)) {
  156. const currentUrl = navigationInstruction.fragment + (navigationInstruction.queryString ? `?${navigationInstruction.queryString}` : '');
  157. console.log("FRAGMENT: ", navigationInstruction.fragment);
  158. console.log("NAVIGATE INSTRUCTION: ", navigationInstruction)
  159. console.log('currentURL: ', currentUrl);
  160.  
  161. localStorage.setItem('origin', currentUrl);
  162. console.log("AuthorizeStep.loginFragment", AuthorizeStep.loginFragment)
  163. next.cancel();
  164. console.log("and it gets here!");
  165. return new Redirect('login');
  166. }
  167. }
  168.  
  169. }
  170.  
  171. checkAuthorization(navigationInstruction, next) {
  172. var usersRole = this.authService.getUserRole();
  173.  
  174. let requiredRoles = navigationInstruction.getAllInstructions()
  175. .map(i => i.config.settings.roles)[0];
  176.  
  177. console.log("route Roles: ", requiredRoles);
  178.  
  179. let isUserPermited = requiredRoles ? requiredRoles.some(r => r === usersRole) : true;
  180.  
  181. console.log("isUserPermited: ", isUserPermited);
  182.  
  183. if (!isUserPermited) {
  184. this.forceReturnToPublic(next);
  185. }
  186.  
  187. }
  188.  
  189. checkOrigin(instruction, next) {
  190. const origin = localStorage.getItem('origin');
  191. // Check if we were not redirected to login page and have an origin
  192. if (instruction.fragment !== AuthorizeStep.loginFragment && origin) {
  193. localStorage.removeItem('origin');
  194. return next.cancel(new Redirect(origin));
  195. }
  196. }
  197.  
  198. forceReturnToPublic(next) {
  199. if (localStorage.getItem('origin')) {
  200. localStorage.removeItem('origin') // Just in case we had origin set.
  201. }
  202. next.cancel();
  203. this.authService.clearIdentity();
  204. this.router.navigate("/", { replace: true, trigger: false });
  205. this.router.reset();
  206. this.aurelia.setRoot("public/public/public");
  207. }
  208. }
Add Comment
Please, Sign In to add comment