First of all, sorry about my English.

I’m in a loop with my login.

this is my root code, in app.component

  constructor(private oauthService: OAuthService, private route: Router) {
     if (!this.oauthService.hasValidAccessToken()) {
       this.route.navigate(['login'])
     }
  }

login.component

  ngOnInit(): void {
    this.oauthService.configure(authConfig);
    this.oauthService.tokenValidationHandler = new NullValidationHandler();

    this.oauthService.loadDiscoveryDocumentAndTryLogin().then(_ => {
      if (!this.oauthService.hasValidAccessToken()) {
        this.oauthService.initCodeFlow()
      } else {
        this.oauthService.setupAutomaticSilentRefresh();
      }
    })
  }

after execute these steps, i can login in my oauth server but, when i redirect to my local my page insists to login again. i’ve trying all code exemples in this repo and documentation, and none of them works to me.

Auth.guard

    if (!this.oauthService.hasValidAccessToken()) {
      this.route.navigate([AppRotasEnum.LOGIN]);
      return false;
    }
    return true;

routing.module

const routes: Routes = [
  {
    path: AppRotasEnum.PAINEL_PRINCIPAL,
    //remover lazy loading apos criar componente
    loadChildren: () =>
    import('./modules/painel-principal/painel-principal.module').then(
      (m) => m.PainelPrincipalModule
    ),
    canActivate: [AuthGuard],
  },
  {
    path: AppRotasEnum.ANALISE_MERITO,
    loadChildren: () =>
      import('./modules/analise-merito/analise-merito.module').then(
        (m) => m.AnaliseMeritoModule
      ),
     canActivate: [AuthGuard],
  },
  {
    path: AppRotasEnum.NONE,
    redirectTo: AppRotasEnum.PAINEL_PRINCIPAL,
    pathMatch: 'full',
  },
  
 {
    path: '**',
    component: LoginComponent,
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes, {
    useHash: true
  })],
  exports: [RouterModule],
})
export class AppRoutingModule {}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

9reactions
SeanCondoncommented, Feb 16, 2021

Try capturing the search argument from the redirect URL (containing the code and state), and passing this in options to TryLogin. For example:

            return await this.oauthService.loadDiscoveryDocumentAndLogin(
                {customHashFragment: window.location.search}
            );

The problem seems to be a race between the Angular route navigation and resolving the code from the redirected page URL. The navigation by Angular Router can happen right after the redirect from Authorization Server to your Angular App happens, but before it is processed. I noticed this more on Firefox (Chrome seems to leave the navigation until after request is processed)

In tryLoginCodeFlow() there is a section that can take the argument from options as customHashFragment (or if not set then, from window.location.search) https://github.com/manfredsteyer/angular-oauth2-oidc/blob/8d152c20ea1dab006a7eca02898260254872e79b/projects/lib/src/oauth-service.ts#L1620

The solution above takes this search value and passes it all the way down to tryLoginCodeFlow().

1reaction
madiyanacommented, Mar 29, 2021

@jeroenheijmans Thanks so much … really thanks … Yes I remove the ‘this.login()’ from my component and I use the guard to force login and it is nice … Thank …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinite Loop - Apple Store
Apple Store Infinite Loop store hours, contact information, and weekly calendar of events. ... Apple Infinite Loop. Open until 6:00 p.m.. Shop by...
Read more >
Infinite loop - Wikipedia
In computer programming, an infinite loop (or endless loop) is a sequence of instructions that, as written, will continue endlessly, unless an external ......
Read more >
Infinite Loop
Infinite Loop Music and Sound Production, Soundtracks, Sound Design, Recording Studio, Event Sound, Theatre Sound Design, Advertising, Commercials, ...
Read more >
What is infinite loop (endless loop)? | Definition from TechTarget
An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a functional exit so that it repeats...
Read more >
Infinity Loop: Puzzle game - Apps on Google Play
Play now one of the most addicting games that will challenge you and is a logic game at the same time. Infinity Loop...
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