SaveLolliPoP
@SaveLolliPoP
1 / 0 = ∞

Почему редиректит на корневую страницу после проверки аутентификации?

Создал canActivate файл и метод
detect: boolean;
canActivate(): boolean {
    this.myHttp.getHTTP('http://localhost:3000/detect')
      .subscribe( (data: any) => {
        this.detect = data.detect;
      }, err => {
        if (err instanceof HttpErrorResponse && err.status === 401) {
          this.detect = false;
        } 
      })
      if (this.detect === false) {
        this._router.navigate(['/login']);
        return this.detect;
      }
      return true;
  }


Добавил его к роутингу:
const routes: Routes = [
  { path: 'login', component: LoginComponent },
  { path: 'newlead', component: NewLeadComponent, canActivate: [AuthGuard] },
  { path: 'leadlist', component: LeadListComponent, canActivate: [AuthGuard] },
  { path: 'leadlist/:id', component: LeadPageComponent, canActivate: [AuthGuard] },
];

На сервере обычная проверка:
router.get('/detect', function( req, res) {
    if ( req.isAuthenticated() ) {
        res.status(200).send({ detect: true })
    } else {
        res.status(401).send({ detect: false })
    }
})


При обновлении страницы перебрасывает на корневую директорию localhost:4200
В чем проблема?
  • Вопрос задан
  • 45 просмотров
Пригласить эксперта
Ответы на вопрос 1
@alikenski
Fullstack Web Developer
this._router.navigate(['/login']);

login без "/" попробуйте
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы