Ошибка Angular 6: причина Auth Guard в зацикливании - PullRequest
0 голосов
/ 23 октября 2018

У меня отделены интерфейс и бэкэнд, бэкэнд - это Ruby on Rails 5.1, а веб-интерфейс - Angular 6.

Я хочу, чтобы пользователи могли получить доступ только с определенными действиями после публикации во внешнем интерфейсе, например, после действий сих электронные письма, а затем проверить это в базе данных.Для этого мне нужно было защитить всю таблицу маршрутизации с помощью AuthGuard.Сейчас я отправляю пользователя с идентификатором 1, потому что я не знаю, как получить данные с помощью сообщения.

У меня вечный цикл с маршрутами, и я не знаю, как это исправить.

Это ошибка, которую я получаю: Throttling History state changes to prevent the browser from hanging

вот соответствующий код:

// Routing array in my routing module


const appRoutes: Routes = [
  { path: '', canActivate: [AuthGuard], children: [
    { path: '', redirectTo: '/courses', pathMatch: 'full' },
    { path: 'courses', component: CourseListComponent,  pathMatch: 'full'},
    { path: 'courses/:id', component: CourseDetailComponent, pathMatch: 'full' },
    { path: 'courses/:id/unit/:unitId', component: CoursePlayComponent,
      children: [
        { path: '', component: CourseListComponent },
        { path: 'lesson/:lessonId', component: CourseLessonComponent, data:{ type: 'lesson'} },
        { path: 'quiz/:quizId', component: CourseQuizComponent, data: {type: 'quiz'} }
      ]}
    ]},
  { path: '**', component: PageNotFoundComponent, pathMatch: 'full' }];

// AuthGuard, needs to add later post authentication

export class AuthGuard implements CanActivate {

    constructor(private authUserService: AuthUserService, private router: Router) { }

     canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean |
     Observable<boolean> | Promise<boolean> {

       // check if user not logged
       if (!this.authUserService.isLoggedIn()) {
         this.router.navigate(["**"]);
         return false;
       }
       
       // for now I'll get the first user
       this.authUserService.login(1);
       this.router.navigate(["/courses"]);
       return true;

      }
 }

// AuthUserService, which fetch data from user service. user service works fine

export class AuthUserService {

  currentUser: IUser;
  errorMessage: string;

  constructor(private userService: UserService) { }

  // check if there's a session
  isLoggedIn(): boolean {
    return this.currentUser;
  }

  // store the session and call http get
  login(id: number) {
    this.userService.getUser(id).subscribe(
      currentUser => {
        this.currentUser = currentUser;
        localStorage.setItem('this.currentUser', JSON.stringify(this.currentUser));
      },
      error  => this.errorMessage = <any>error
    );
  }

  // clear session
  logout() {
    localStorage.removeItem('currentUser');
  }
}

РЕДАКТИРОВАТЬ: теперь он даже не перезагружается, всегда в состоянии загрузки

...