简体   繁体   English

从 ngOnInit() 内部调用身份验证失败后,ngOnInit() 被一次又一次地调用

[英]ngOnInit() is bing called again and again after authentication failure from a call from inside of ngOnInit()

I am working on a simple angular app.我正在开发一个简单的角度应用程序。 There are login.component.ts and home.component.ts.有 login.component.ts 和 home.component.ts。

On successful login, the home.component.ts is being called and it loads posts from post-service.成功登录后,将调用 home.component.ts 并从 post-service 加载帖子。

Unfortunately, due to some issues, (I am still working on) the call to post-service is not being authenticated.不幸的是,由于一些问题,(我仍在处理)对 post-service 的调用没有经过身份验证。 However, ngInit method in home.component.ts is being called again and again and browser is loading the home page repeatedly.但是,home.component.ts 中的 ngInit 方法被一次又一次地调用,浏览器反复加载主页。

Not sure what's going on.不知道发生了什么。

LoginComponent

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { first } from 'rxjs/operators';

import { AlertService, AuthenticationService } from '../_services';

@Component({templateUrl: 'login.component.html'})
export class LoginComponent implements OnInit {
    loginForm: FormGroup;
    loading = false;
    submitted = false;
    returnUrl: string;

    constructor(
        private formBuilder: FormBuilder,
        private route: ActivatedRoute,
        private router: Router,
        private authenticationService: AuthenticationService,
        private alertService: AlertService) {}

    ngOnInit() {
        // if(localStorage.getItem('currentUser')){
        //     console.log("User is logged in");
        //     this.router.navigate(['/home']);
        // }

        this.loginForm = this.formBuilder.group({
            email: ['', [Validators.required,   Validators.pattern(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,63})$/)]],
            password: ['', Validators.required]
        });

        // reset login status
        this.authenticationService.logout();

        // get return url from route parameters or default to '/'
        this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/home';
    }

    // convenience getter for easy access to form fields
    get f() { return this.loginForm.controls; }

    onSubmit() {

        this.submitted = true;

        // stop here if form is invalid
        if (this.loginForm.invalid) {
            return;
        }

        this.loading = true;
        this.authenticationService.login(this.f.email.value, this.f.password.value)
            .pipe(first())
            .subscribe(
                data => {
                    this.router.navigate(['/home']);
                },
                error => {
                    this.alertService.error(error);
                    this.loading = false;
                });
    }
}

home.component.ts home.component.ts

 import { Component, OnInit } from '@angular/core';
import { first } from 'rxjs/operators';
import { User } from '../_models';
import { UserService } from '../_services';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
// import { NgxSpinnerService } from 'ngx-spinner';
import { InfiniteScrollModule } from 'ngx-infinite-scroll';
import { error } from 'protractor';

@Component({templateUrl: 'home.component.html'})
export class HomeComponent implements OnInit {
    currentUser: User;
    users: User[] = [];
    allpost;
    notscrolly = true;
    notEmptyPost = true;



    constructor(private userService: UserService, private http: HttpClient, private router: Router,) {
        this.currentUser = JSON.parse(localStorage.getItem('currentUser'));

    }

    ngOnInit() {
        if(!this.currentUser){
          this.router.navigate(['/login']);
        }else{
         this.loadInitPost();
        console.log("Inside home component" + localStorage.getItem('currentUser'));
        }

    }

  loadInitPost() {
    const url = 'http://localhost:8080/user/questions';

    const emailId = JSON.parse(localStorage.getItem('currentUser'))['emailId'];
    console.log("email id  is  " + emailId);
    this.http.get(url,  {
      params : {
        'emailId' : JSON.parse(localStorage.getItem('currentUser'))['emailId']
      }

    }).subscribe(data => {
      // this.allpost = data;
      this.allpost = data;
    }, error => {return;});
  }

    deleteUser(id: number) {
        this.userService.delete(id).pipe(first()).subscribe(() => { 
            this.loadAllUsers() 
        });
    }

    private loadAllUsers() {
        this.userService.getAll().pipe(first()).subscribe(users => { 
            this.users = users; 
        });
    }

    onScroll() {
        if (this.notscrolly && this.notEmptyPost) {
        //   this.spinner.show();
          this.notscrolly = false;
          // this.loadNextPost();
       }
      // console.log("scrolled");
      // this.spinner.show();
      }




}
app.module.ts


import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule }    from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { InfiniteScrollModule } from 'ngx-infinite-scroll';
import { Injectable } from '@angular/core';
import {
    HttpEvent, HttpInterceptor, HttpHandler, HttpRequest
  } from '@angular/common/http';

// import { NgxSpinnerModule } from 'ngx-spinner';

// used to create fake backend
import { fakeBackendProvider } from './_helpers';

import { AppComponent }  from './app.component';
import { routing }        from './app.routing';

import { AlertComponent } from './_directives';
import { AuthGuard } from './_guards';
import { JwtInterceptor, ErrorInterceptor } from './_helpers';
import { AlertService, AuthenticationService, UserService } from './_services';
import { HomeComponent } from './home';
import { LoginComponent } from './login';
import { RegisterComponent } from './register';;
import { LogoutComponent } from './logout/logout.component'


@Injectable()
export class XhrInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler) {
    const xhr = req.clone({
      headers: req.headers.set('X-Requested-With', 'XMLHttpRequest')
    });
    return next.handle(xhr);
  }
}

@NgModule({
    imports: [
        BrowserModule,
        ReactiveFormsModule,
        HttpClientModule,
        routing,
        InfiniteScrollModule,
        // NgxSpinnerModule
    ],
    declarations: [
        AppComponent,
        AlertComponent,
        HomeComponent,
        LoginComponent,
        RegisterComponent,
        LogoutComponent
,
        LogoutComponent    ],
    providers: [
        AuthGuard,
        AlertService,
        AuthenticationService,
        UserService,
        { provide: HTTP_INTERCEPTORS, useClass: XhrInterceptor, multi: true },
        { provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }

        // provider used to create fake backend
        // fakeBackendProvider
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }

However, if I remove security from the backend service.但是,如果我从后端服务中删除安全性。 This repeated loading stops.这种重复加载停止。

stackblitz https://stackblitz.com/github/bjs007/angular stackblitz https://stackblitz.com/github/bjs007/angular

You have an ErrorInterceptor which reloads when there is 401.You said there is some authentication issue in post service, see if it returns 401 then your ErrorInterceptor causes page to reload again and again.您有一个ErrorInterceptor ,它会在出现 401 时重新加载。您说 post 服务中存在一些身份验证问题,看看它是否返回 401,然后您的ErrorInterceptor会导致页面一次又一次地重新加载。

 if (err.status === 401) {
            // auto logout if 401 response returned from api
            // this.authenticationService.logout();
            location.reload(true);
 }

you can navigate to login page when you get 401 this.router.navigate(['/login']);当您收到 401 this.router.navigate(['/login']);时,您可以导航到登录页面this.router.navigate(['/login']);

Instead of location.reload(true) Put following in your ErrorInterceptor而不是 location.reload(true) 将以下内容放入您的 ErrorInterceptor

 if (err.status === 401) {
            this.router.navigate(['/home']);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM