обработать ошибку при выходе angular и nodeJs API - PullRequest
0 голосов
/ 27 апреля 2020

Я создаю для своего приложения контроллер выхода с nodeJS:

 /**
 * @api {post} /auth/signout SignOut User
 * @apiGroup Auth
 * @apiHeader Authorization Bearer Token
 *
 * @apiSuccessExample {json} Success
 *    HTTP/1.1 204 No content

 * @apiErrorExample Not Authorized
 *    HTTP/1.1 401 Not Authorized
 * @apiErrorExample Bad Request
 *    HTTP/1.1 400 Bad Request
 * @apiErrorExample Internal Server Error
 *    HTTP/1.1 500 Internal Server Error
 */
export async function signOut(req, res) {
  req.user.tokens = req.user.tokens.filter((token) => {
    return token.token != req.session.token
  })
  await req.user.save()
  return res.status(204).end()
}

, а вот функция выхода из типизированного набора:

  logout() {
    this.http.post('/api/auth/signout', {}).subscribe(res => {
      console.log(res)
      this.avatarDropeddown = false
      localStorage.removeItem('currentUser');
      this.router.navigateByUrl('/login');
    });
  }

<li><a rel="nofollow" (click)="logout()" class="dropdown-item logout text-center"><i class="ti-power-off"></i></a>

когда нажимаете кнопку выхода из системы, появляется несколько ошибок перехватчика, как показано на изображении ниже

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request)
      .pipe(retry(1),
        catchError((error: HttpErrorResponse) => {
          if (error.status === 0) {
            this.showError('S\'il vous plaît assurez-vous que vous êtes connecté à internet!', 'Connection internet requise!');
          } else if (error.status === 500) {
            this.showError('Notre équipe va résoudre ce problème dès que possible.', 'Erreur Interne du Serveur!');
          } else if (error.status === 401) {
            // auto logout if 401 response returned from api
            this.authenticationService.logoutApi();
            this.showError('Vous n\'avez pas la permission pour accéder à ce site!', 'Autorisation requise!');
            setTimeout(() => {
              location.reload(true);
            }, 2000);
          }
          throw (error);
          // return throwError(error);
        }));
  }

  showError(msg, title) {
    this.toastr.error(msg, title);
  }

enter image description here

при выходе он перенаправляет меня зайдите на страницу входа и выполните refre sh одновременно, поэтому все эти ошибки появляются!

...