Guest User

Untitled

a guest
May 20th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. checkLogin(authorities: string[], url: string): Promise<boolean> {
  2. var principal = this.principal;
  3.  
  4. return Promise.resolve(principal.identity().then((account) => {
  5.  
  6.  
  7.  
  8. ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
  9. TypeError: Cannot read property 'then' of undefined
  10. at AuthGuard.checkLogin (auth-guard.ts:27)
  11. at AuthGuard.canActivate (auth-guard.ts:21)
  12.  
  13. app.module
  14.  
  15. const routes: Routes = [
  16. {
  17. path: '**',
  18. component: DashboardComponent,
  19. canActivate: [AuthGuard],
  20. resolve: {
  21. data: DashboardService
  22. }
  23. }
  24. ];
  25.  
  26. @NgModule({
  27. declarations: [
  28. DashboardComponent
  29. ],
  30. imports: [
  31. RouterModule.forChild(routes),
  32. HttpClientModule,
  33. HttpModule,
  34. CdkTableModule,
  35. MatButtonModule,
  36. MatDividerModule,
  37. MatFormFieldModule,
  38. MatIconModule,
  39. MatMenuModule,
  40. MatSelectModule,
  41. MatSidenavModule,
  42. MatTableModule,
  43. MatTabsModule,
  44.  
  45. NgxChartsModule,
  46. ],
  47. providers: [
  48. ProjectDashboardService, AuthGuard, Principal, StateStorageService
  49. ]
  50. })
  51.  
  52. import { Injectable } from '@angular/core';
  53. import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
  54.  
  55. import { Principal } from './principal.service';
  56. import { StateStorageService } from './state-storage.service';
  57.  
  58. @Injectable()
  59. export class AuthGuard implements CanActivate {
  60.  
  61. constructor(private router: Router,
  62. private principal: Principal,
  63. private stateStorageService: StateStorageService) {
  64. }
  65.  
  66. canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Promise<boolean> {
  67.  
  68. const authorities = route.data['authorities'];
  69. // We need to call the checkLogin / and so the principal.identity() function, to ensure,
  70. // that the client has a principal too, if they already logged in by the server.
  71. // This could happen on a page refresh.
  72. return this.checkLogin(authorities, state.url);
  73. }
  74.  
  75. checkLogin(authorities: string[], url: string): Promise<boolean> {
  76. var principal = this.principal;
  77.  
  78. return Promise.resolve(principal.identity().then((account) => {
  79.  
  80. if (!authorities || authorities.length === 0) {
  81. return true;
  82. }
  83.  
  84. if (account) {
  85. return principal.hasAnyAuthority(authorities).then((response) => {
  86. if (response) {
  87. return true;
  88. }
  89. return false;
  90. });
  91. }
  92.  
  93. this.stateStorageService.storeUrl(url);
  94. //this.router.navigate(['accessdenied']);
  95. window.location.href = "http://stackoverflow.com";
  96. return false;
  97. }));
  98. }
  99. }
  100.  
  101. import { Injectable } from '@angular/core';
  102. import { Observable } from 'rxjs/Observable';
  103. import { Subject } from 'rxjs/Subject';
  104.  
  105.  
  106. @Injectable()
  107. export class Principal {
  108. private userIdentity: any;
  109. private authenticated = false;
  110. private authenticationState = new Subject<any>();
  111.  
  112. constructor(
  113.  
  114. ) { }
  115.  
  116. authenticate(identity) {
  117. this.userIdentity = identity;
  118. this.authenticated = identity !== null;
  119. this.authenticationState.next(this.userIdentity);
  120. }
  121.  
  122. hasAnyAuthority(authorities: string[]): Promise<boolean> {
  123. return Promise.resolve(this.hasAnyAuthorityDirect(authorities));
  124. }
  125.  
  126. hasAnyAuthorityDirect(authorities: string[]): boolean {
  127. if (!this.authenticated || !this.userIdentity || !this.userIdentity.authorities) {
  128. return false;
  129. }
  130.  
  131. for (let i = 0; i < authorities.length; i++) {
  132. if (this.userIdentity.authorities.includes(authorities[i])) {
  133. return true;
  134. }
  135. }
  136.  
  137. return false;
  138. }
  139.  
  140. hasAuthority(authority: string): Promise<boolean> {
  141. if (!this.authenticated) {
  142. return Promise.resolve(false);
  143. }
  144.  
  145. return this.identity().then((id) => {
  146. return Promise.resolve(id.authorities && id.authorities.includes(authority));
  147. }, () => {
  148. return Promise.resolve(false);
  149. });
  150. }
  151.  
  152. identity(force?: boolean): Promise<any> {
  153. if (force === true) {
  154. this.userIdentity = undefined;
  155. }
  156.  
  157. // check and see if we have retrieved the userIdentity data from the server.
  158. // if we have, reuse it by immediately resolving
  159. if (this.userIdentity) {
  160. return Promise.resolve(this.userIdentity);
  161. }
  162.  
  163. }
  164.  
  165. isAuthenticated(): boolean {
  166. return this.authenticated;
  167. }
  168.  
  169. isIdentityResolved(): boolean {
  170. return this.userIdentity !== undefined;
  171. }
  172.  
  173. getAuthenticationState(): Observable<any> {
  174. return this.authenticationState.asObservable();
  175. }
  176.  
  177. getImageUrl(): String {
  178. return this.isIdentityResolved() ? this.userIdentity.imageUrl : null;
  179. }
  180. }
Add Comment
Please, Sign In to add comment