question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nebular Login not redirecting if already logged in

See original GitHub issue

Issue type

I’m submitting a … (check one with “x”)

  • bug report
  • feature request

Issue description

Current behavior: Login component not redirecting if already logged in. For example, you log in, you get redirected to some page, then if you go by url to Login again, you can login again, even if you did not log out and so on (the token is also still there)

Expected behavior: Redirect to success redirect specified page if the user is authenticated

Steps to reproduce: Login, and navigate again to login page and you will be able to login as many times as you wish

Related code:

NbAuthModule.forRoot({
      strategies: [
        NbPasswordAuthStrategy.setup({
          name: 'email',
          token: {
            class: NbAuthJWTToken,
            key: 'my_token'
          },
          baseEndpoint: environment.connectCoreUri,
          login: {
            endpoint: 'api/login',
            method: 'post',
            redirect: {
              success: 'auth/success',
              failure: null,
            },
          }
        }),
      ],
      forms: {},
    })

Other information:

npm, node, OS, Browser

Angular 6 all browsers

Angular, Nebular

Latest version of nebular

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

6reactions
biborncommented, Nov 29, 2018

@iosif-bancioiu You can definitely solve this by doing a check in your LoginComponent.

In my case, my LoginComponent is extending a class called NbLoginComponent. Thus, my check will be done in ngOnInit function. Here’s how I do it:

import { Component, OnInit } from '@angular/core';
import { NbLoginComponent, NbAuthJWTToken } from '@nebular/auth';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss'],
})
export class LoginComponent extends NbLoginComponent implements OnInit {
  // no constructor

  ngOnInit() {
    this.service.onTokenChange().subscribe((token: NbAuthJWTToken) => {
      if (token.isValid()) {
        this.router.navigate(['pages/dashboard']); // Your redirection goes here
      }
    });
  }
}

Note that I don’t have to inject anything in the constructor because the parent class (NbLoginComponent) already have everything I need. Hope this helps.

0reactions
iosif-bancioiucommented, May 8, 2019

@vikramsparamesh I found a workaround.

  1. Login Component
export class LoginComponent extends NbLoginComponent implements OnInit {

 ngOnInit() {
   this.service.onTokenChange().subscribe((token: NbAuthJWTToken) => {
     if (token.isValid()) {
       this.router.navigate(['/auth/success']);
     }
   });
 }

}
  1. Auth Module Routing
{
        path: 'success',
        resolve: {
          url: ExternalUrlProviderService,
          skipLocationChange: true
        },
        canActivate: [AuthGuardService], //here do the regular authenticated check
        component: AuthenticatedComponent, //empty component, (something to say redirecting maybe) you need a component here in order for the route to work
}

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
  providers:[
    {provide: ExternalUrlProviderService, //the service should exist but use value will define what to do in this case
      useValue: () => {
        const externalUrl = environment.URI; //the uri where to redirect if authenticated
        window.open(externalUrl, '_self'); //open in self or new tab
      },}
  ]
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nebular - Redirect after Login - GitHub Pages
Need some help or found an issue? Ask on Stack Overflow with tag `nebular` or post an issue on GitHub.
Read more >
ngx-admin NbDummyAuthStrategy redirection after login
I have found that when using NbPasswordAuthStrategy you can easily set a redirect on successful login, however when using NbDummyAuthStrategy it ...
Read more >
Configuring UI - Nebular
Auth module comes with a list of authentication components: ... delay before redirect after a successful login, while success message is shown to...
Read more >
[NEBULA] Facebook Login — Zyxel Community
When an user have been facebook login and then reconnect again to wifi, ... logged out and disconnected, meaning users will not have...
Read more >
Angular Authentication with OpenID Connect and Okta in 20 ...
Furthermore, when clicking the “Login” button on the home page, redirects straight to the callback component, without prompting a login page.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found