Edit Axha
Edit Axha

Reputation: 73

Angular 19 + Oauth2 have problem when I try to login

I'm working on an Angular 19.1.0 application where I want to implement login using Oauth2 (angular-oauth2-oidc version 19.0.0). My application is not working when I call login function from auth.service.ts, I can get just discovery_document_loaded event and not any other. But when my DevTool is open, It works fine and I can get all the events from 'this.oauthService.events' subscription. My application is standalone component based and I have these configuration on the project. Can someone help me on where the error should be, or how I can debug this problem. It is really weird that it works(not all the time) when the DevTool is open.

app.config.ts


function storageFactory(): OAuthStorage {
  return localStorage;
}
export const appConfig: ApplicationConfig = {
  providers: [
    provideZoneChangeDetection({ eventCoalescing: true }),
    provideRouter(routes, withHashLocation()),
    provideHttpClient(withInterceptors([authorizationInterceptor])),
    provideHttpClient(),
    provideOAuthClient(),
    OAuthService,
    { provide: AuthConfig, useValue: authCodeFlowConfig },
    { provide: OAuthStorage, useFactory: storageFactory },
  ]
};

auth.service.ts

@Injectable({
    providedIn: 'root'
})
export class AuthService {
    constructor(private oauthService: OAuthService
    ) {
        this.configureOAuth();
    }
    private configureOAuth() {
        this.oauthService.loadDiscoveryDocumentAndTryLogin();
        this.oauthService.events
            .pipe(filter((e) => {
                console.log(e.type);
                return e.type === 'token_received';
            }))
            .subscribe((_) => {
             //do something
            });
        
    }
    login() {
        this.oauthService.initImplicitFlow();
    }
    logout() {
        this.oauthService.logOut();
    }
    checkToken() {
        console.log(this.oauthService.getAccessToken());
    }
}

I really appreciate any help you can provide. Thank you in advance.

Upvotes: 1

Views: 70

Answers (0)

Related Questions