Commit 06a2a3f3 authored by Merzough Münker's avatar Merzough Münker
Browse files

feat: partial remove the isAuthenticated$ concept

parent a9569991
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -29,11 +29,14 @@ export class LoadingComponent implements OnInit, OnDestroy {
  ) {}

  public ngOnInit(): void {
    // TODO : remove the concept of isAuthenticated$
    if (this.authentication.isAuthenticated$) {
      this.subscription = this.authentication.isAuthenticated$.pipe(
        filter(isAuthenticated => isAuthenticated !== null),
        tap(() => this.router.navigate([ '/', 'authentication', 'login' ]))
      ).subscribe();
    }
  }

  public ngOnDestroy(): void {
    this.subscription?.unsubscribe();
+41 −47
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild {
  public canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<UrlTree | boolean> | boolean {
  ): Promise<UrlTree | boolean> {
    console.debug('[RxapAuthenticationGuard] can activate', state.url);
    return this.checkAuthStatus(route, state);
  }
@@ -46,27 +46,22 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild {
  public canActivateChild(
    childRoute: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<UrlTree | boolean> | boolean {
  ): Promise<UrlTree | boolean> {
    console.debug('[RxapAuthenticationGuard] can activate child', state.url);
    return this.checkAuthStatus(childRoute, state);
  }

  public isAuthenticated(): Observable<boolean | null> {
    return this.authentication.isAuthenticated$;
  }

  private checkAuthStatus(
  private async checkAuthStatus(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ) {
  ): Promise<UrlTree | boolean> {

    if (this.deactivated) {
      return true;
    }

    return this.isAuthenticated().pipe(
      take(1),
      map(authenticated => {
    const authenticated = await this.authentication.isAuthenticated();

    if (authenticated === null) {
      if (!this.lastUrl || (state.url !== '/' && !state.url.match(/authentication\/login/))) {
        this.lastUrl = state.url;
@@ -104,8 +99,7 @@ export class RxapAuthenticationGuard implements CanActivate, CanActivateChild {
        return this.router.createUrlTree([ '/', 'authentication', 'login' ]);
      }
    }
      })
    );

  }

  private createUrlTreeToLastUrl(lastUrl: string): UrlTree {
+4 −8
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ export interface IAuthenticationService {

  signInWithEmailAndPassword(email: string, password: string, remember: boolean): Promise<boolean>;

  signOut(): Promise<boolean>;
  signOut(): Promise<void>;

}

@@ -87,15 +87,11 @@ export class RxapAuthenticationService implements IAuthenticationService {

  }

  public async signOut(): Promise<boolean> {
  public async signOut(): Promise<void> {
    console.warn('The default RxapAuthenticationService implementation should only be used in a development environment!');
    const logout = true;
    this.isAuthenticated$.next(!logout);
    if (logout) {
    this.isAuthenticated$.next(false);
    localStorage.removeItem(RXAP_AUTHENTICATION_LOCAL_STORAGE_KEY);
  }
    return logout;
  }

  public signInWithProvider(provider: string): Promise<boolean> {
    console.warn('The default RxapAuthenticationService implementation should only be used in a development environment!');
+7 −5
Original line number Diff line number Diff line
@@ -21,11 +21,13 @@ export class SignOutDirective {
  constructor(private readonly auth: RxapAuthenticationService) { }

  @HostListener('click')
  public onClick() {
    this.auth
        .signOut()
        .then(() => this.successful.emit())
        .catch(error => this.failure.emit(error));
  public async onClick() {
    try {
      await this.auth.signOut();
      this.successful.emit();
    } catch (e) {
      this.failure.emit(e);
    }
  }

}