Guest User

Untitled

a guest
Oct 18th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. async loadCurrentUser(): Promise<IUser> {
  2. return new Promise<IUser>(ok => {
  3. this.getCurrentUser()
  4. .subscribe(
  5. next => {
  6. alert('функция, которую нужно ждать')
  7. this._currentUserSource.next(next);
  8. ok(next)
  9. },
  10. error => {
  11. this._currentUserSource.next(null);
  12. this._logService.addError(error);
  13. ok(null);
  14. })
  15. });
  16. }
  17.  
  18. await this._accountService.loadCurrentUser();
  19. alert('функция компонента');
  20.  
  21. import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
  22. .....
  23. @NgModule({
  24. imports: [
  25. HttpClientModule,
  26. .....
  27. providers: [
  28. { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
  29.  
  30. const routes: Routes = [
  31. { path: '', pathMatch: 'full', redirectTo: 'home' },
  32. { path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
  33. { path: 'login', component: LoginComponent },
  34. { path: '', component: RegisterComponent },
  35.  
  36. // если есть админская часть можно прикрыть ее отдельным гардианом
  37. { path: 'admin', component: AdminComponent, canActivate: [AdminAuthGuard] },
  38.  
  39. { path: '**', redirectTo: 'home' }
  40. ];
  41.  
  42. import { BehaviorSubject } from 'rxjs';
  43. public currentUser: BehaviorSubject<IUser> = new BehaviorSubject(null);
  44.  
  45. usSubscription: any;
  46. constructor(private _us: UserService) {
  47. let user = this._us.currentUser.getValue(); // получаем текущего юзера
  48. this.usSubscription = this._us.currentUser.subscribe(
  49. user => consoler.log('user was changed:', user);
  50. ); // подписываемся на логин/логаут/изменение
  51. }
  52.  
  53. login() {
  54. this.clearFormErrors();
  55. if (!this.loginForm.valid) {
  56. this.showFormErrors(['Заполните все поля']);
  57. return;
  58. }
  59. this._accountService.authorizationStatus()
  60. .then(ok => {
  61. if (ok) {
  62. this.RedirectToHome()
  63. }
  64. else {
  65. let formData = this.loginForm.value;
  66. let login: ILogin = {
  67. userName: formData.username,
  68. password: formData.password,
  69. rememberMe: formData.rememberMe
  70. };
  71. }
  72. this._accountService.login(login).subscribe(
  73. result => {
  74. if (!result.isSucceeded) {
  75. this.showFormErrors(result.messages);
  76. return;
  77. } else {
  78. this._accountService.loadCurrentUser()
  79. .then(ok => this.RedirectToHome());
  80. }
  81. },
  82. error => {
  83. alert(<any>error);
  84. return;
  85. }
  86. );
  87. })
  88. .catch(reason => alert(<any>reason));
  89. }
  90.  
  91. errors: string[];
  92.  
  93. <div *ngIf="errors" class="formErrorLable red-text">
  94. <ng-template ngFor let-error [ngForOf]="errors">
  95. {{error}}<br />
  96. </ng-template>
  97. </div>
Add Comment
Please, Sign In to add comment