app.component.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { Component, OnInit } from '@angular/core';
  2. import { ComponentBase } from '@core/base/common-base';
  3. import { AuthService } from '@core/services/authentication/auth.service';
  4. import { ADD_PROFILE } from '@core/state/profile/profile.actions';
  5. import { takeUntil } from 'rxjs';
  6. import { Store } from '@ngrx/store';
  7. import { USER } from '@core/models/authenticcation.model';
  8. @Component({
  9. selector: 'app-root',
  10. templateUrl: './app.component.html',
  11. styleUrls: ['./app.component.scss'],
  12. })
  13. export class AppComponent extends ComponentBase implements OnInit {
  14. title = 'mt-works';
  15. constructor(
  16. private readonly authService: AuthService,
  17. private readonly store: Store<{ user: { profile: USER } }>
  18. ) {
  19. super();
  20. }
  21. ngOnInit(): void {
  22. if (this.authService.isAuthenticated()) {
  23. this.getProfile();
  24. }
  25. }
  26. getProfile(): void {
  27. this.authService
  28. .getUserData()
  29. .pipe(takeUntil(this.destroy$))
  30. .subscribe((res) => {
  31. this.store.dispatch(
  32. ADD_PROFILE({
  33. user: { userName: res.userName, email: res.email },
  34. })
  35. );
  36. });
  37. }
  38. }