A
A
Adel Khalitov2019-03-10 19:21:08
Angular
Adel Khalitov, 2019-03-10 19:21:08

Why does it redirect to the root page after authentication check?

Created a canActivate file and method

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;
  }

Added it to the routing:
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] },
];

On the server, the usual check is:
router.get('/detect', function( req, res) {
    if ( req.isAuthenticated() ) {
        res.status(200).send({ detect: true })
    } else {
        res.status(401).send({ detect: false })
    }
})

When refreshing the page, it redirects to the root directory localhost:4200
What's the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alisher Aidarkhan, 2019-03-12
@alikenski

this._router.navigate(['/login']);
login without "/" try

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question