0

I have [AuthGuard] for /profile/personal-data which watching if the user has logged or not . And after login() I do this.router.navigates['profile/persnal-data'] and it works correct. But when I'm not logged in and try to put in browser /profile/person then app redirects me to ?path=%2Fprofile%2Fpersonal-data. I don't know what am I doing wrong ?

route.config

const routes: Routes = [
  {
    path: '',
    component: ProfileComponent,
    children: [
      { path: '', redirectTo: 'personal-data', pathMatch: 'full' },
      { path: 'personal-data', component: PersonalDataComponent, canActivate: [AuthGuard] },
    ]
  }

];

Gurard.ts

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';

import { AuthentificationService } from '../services/authentification.service';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

  constructor(
    private router: Router,
    private authentificationService: AuthentificationService,
  ) { }

  isEmpty(obj): boolean {
    for (const prop in obj) {
      if (obj.hasOwnProperty(prop)) {
        return false;
      }
    }
    return JSON.stringify(obj) === JSON.stringify({});
  }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    return Observable.create(observer => {
      if (
        localStorage.getItem('access-token-1cb-portal') != null &&
        localStorage.getItem('expires-at-access-1cb-portal') != null &&
        localStorage.getItem('refresh-token-1cb-portal') != null &&
        localStorage.getItem('expires-at-refresh-1cb-portal') != null
      ) {
        observer.next(true);
        observer.complete();
        return;
      } else {
        this.router.navigate(['/'], {
          queryParams: {
            path: state.url
          }
        });
        observer.next(false);
        observer.complete();
        return;
      }
    });
  }
}

UPD 1

I have updated my Guard to simple

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request);
  }

It still redirect's me to main page with strange url http://localhost:4200/?path=%2Fprofile%2Fpersonal-data but when I remove canActivate: [AuthGuard] from my route.config for profile route ,and it workes and redirect to the right page. What is going wrong , why the guard make huge sense for this behaviour ?

4
  • Try this.router.navigates['/profile','persnal-data'] Commented Mar 10, 2021 at 12:14
  • What is the desired behaviour? Commented Mar 10, 2021 at 12:27
  • redirect is working well, the desired result is : when I manually put /profile/personal-data the angular will not redirecting me to main page Commented Mar 10, 2021 at 12:43
  • the main question in which cases angular converts url to path=%2Fprofile%2Fpersonal-data Commented Mar 10, 2021 at 12:43

2 Answers 2

1

I guess that the pathMatch: 'full' (in the routes constant) prevents you to go to /profile/personal-data when typing /profile.person. Try adding { path: "**", redirectTo: "personal-data" } at the bottom.

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried , but it didnt help. Just when I remove canActivate: [AuthGuard] it works . But my GUard do not do anything special
0

Sorry guyes , it was my stupid mistake my Guard was redirecting to weird string due to code that I have written in my guard a long time ago . Sorry I have missed this code

return Observable.create(observer => {
      if (
        localStorage.getItem('access-token-1cb-portal') != null &&
        localStorage.getItem('expires-at-access-1cb-portal') != null &&
        localStorage.getItem('refresh-token-1cb-portal') != null &&
        localStorage.getItem('expires-at-refresh-1cb-portal') != null
      ) {
        observer.next(true);
        observer.complete();
        return;
      } else {
        this.router.navigate(['/'], {
          queryParams: {
            path: state.url // that is the reason
          }
        });
        observer.next(false);
        observer.complete();
        return;
      }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.