Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async loadCurrentUser(): Promise<IUser> {
- return new Promise<IUser>(ok => {
- this.getCurrentUser()
- .subscribe(
- next => {
- alert('функция, которую нужно ждать')
- this._currentUserSource.next(next);
- ok(next)
- },
- error => {
- this._currentUserSource.next(null);
- this._logService.addError(error);
- ok(null);
- })
- });
- }
- await this._accountService.loadCurrentUser();
- alert('функция компонента');
- import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
- .....
- @NgModule({
- imports: [
- HttpClientModule,
- .....
- providers: [
- { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
- const routes: Routes = [
- { path: '', pathMatch: 'full', redirectTo: 'home' },
- { path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
- { path: 'login', component: LoginComponent },
- { path: '', component: RegisterComponent },
- // если есть админская часть можно прикрыть ее отдельным гардианом
- { path: 'admin', component: AdminComponent, canActivate: [AdminAuthGuard] },
- { path: '**', redirectTo: 'home' }
- ];
- import { BehaviorSubject } from 'rxjs';
- public currentUser: BehaviorSubject<IUser> = new BehaviorSubject(null);
- usSubscription: any;
- constructor(private _us: UserService) {
- let user = this._us.currentUser.getValue(); // получаем текущего юзера
- this.usSubscription = this._us.currentUser.subscribe(
- user => consoler.log('user was changed:', user);
- ); // подписываемся на логин/логаут/изменение
- }
- login() {
- this.clearFormErrors();
- if (!this.loginForm.valid) {
- this.showFormErrors(['Заполните все поля']);
- return;
- }
- this._accountService.authorizationStatus()
- .then(ok => {
- if (ok) {
- this.RedirectToHome()
- }
- else {
- let formData = this.loginForm.value;
- let login: ILogin = {
- userName: formData.username,
- password: formData.password,
- rememberMe: formData.rememberMe
- };
- }
- this._accountService.login(login).subscribe(
- result => {
- if (!result.isSucceeded) {
- this.showFormErrors(result.messages);
- return;
- } else {
- this._accountService.loadCurrentUser()
- .then(ok => this.RedirectToHome());
- }
- },
- error => {
- alert(<any>error);
- return;
- }
- );
- })
- .catch(reason => alert(<any>reason));
- }
- errors: string[];
- <div *ngIf="errors" class="formErrorLable red-text">
- <ng-template ngFor let-error [ngForOf]="errors">
- {{error}}<br />
- </ng-template>
- </div>
Add Comment
Please, Sign In to add comment