2

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.

asked Feb 13, 2025 at 0:47

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.