Skip to content

Commit bf85892

Browse files
authored
Merge pull request #79 from damienbod/copilot/fix-angular-login-issue
Fix Angular 21 login by adding APP_INITIALIZER for OIDC authentication
2 parents 36e6cbb + 118bc9d commit bf85892

5 files changed

Lines changed: 24 additions & 15 deletions

File tree

AngularCliCodeFlowPkce/src/app/app.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export class AppComponent implements OnInit {
2222
ngOnInit(): void {
2323
this.userData$ = this.oidcSecurityService.userData$;
2424

25-
this.oidcSecurityService.checkAuth().subscribe((isAuthenticated: any) => {
26-
this.isAuthenticated = isAuthenticated;
25+
this.oidcSecurityService.isAuthenticated$.subscribe((isAuthenticated) => {
26+
this.isAuthenticated = isAuthenticated.isAuthenticated;
2727
console.log('app authenticated', isAuthenticated);
2828
});
2929
}

AngularCliCodeFlowPkce/src/app/app.module.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import { DataEventRecordsModule } from './dataeventrecords/dataeventrecords.modu
88
import { ForbiddenComponent } from './forbidden/forbidden.component';
99
import { HomeComponent } from './home/home.component';
1010
import { UnauthorizedComponent } from './unauthorized/unauthorized.component';
11-
import { AuthModule, LogLevel } from 'angular-auth-oidc-client';
11+
import { AuthModule, LogLevel, OidcSecurityService } from 'angular-auth-oidc-client';
12+
import { firstValueFrom } from 'rxjs';
13+
14+
export function configureAuth(oidcSecurityService: OidcSecurityService) {
15+
return () => firstValueFrom(oidcSecurityService.checkAuth());
16+
}
1217

1318
@NgModule({ declarations: [
1419
AppComponent,
@@ -33,7 +38,15 @@ import { AuthModule, LogLevel } from 'angular-auth-oidc-client';
3338
useRefreshToken: true,
3439
logLevel: LogLevel.Debug,
3540
},
36-
})], providers: [provideHttpClient(withInterceptorsFromDi())] })
41+
})], providers: [
42+
provideHttpClient(withInterceptorsFromDi()),
43+
{
44+
provide: APP_INITIALIZER,
45+
useFactory: configureAuth,
46+
deps: [OidcSecurityService],
47+
multi: true,
48+
}
49+
] })
3750

3851
export class AppModule {
3952
constructor() {

AngularCliCodeFlowPkce/src/app/dataeventrecords/components/dataeventrecords-create.component.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Router } from '@angular/router';
33
import { Observable } from 'rxjs';
4-
import { map } from 'rxjs/operators';
54
import { OidcSecurityService, ConfigAuthenticatedResult, AuthenticatedResult } from 'angular-auth-oidc-client';
65

76
import { DataEventRecordsService } from '../dataeventrecords.service';
@@ -31,10 +30,9 @@ export class DataEventRecordsCreateComponent implements OnInit {
3130
}
3231

3332
ngOnInit() {
34-
this.isAuthenticated$.pipe(
35-
map((isAuthenticated: any) => {
36-
console.log('isAuthorized: ' + isAuthenticated);
37-
}));
33+
this.isAuthenticated$.subscribe((isAuthenticated: AuthenticatedResult) => {
34+
console.log('isAuthorized: ' + isAuthenticated.isAuthenticated);
35+
});
3836

3937
this.DataEventRecord = { id: 0, name: '', description: '', timestamp: '' };
4038
}

AngularCliCodeFlowPkce/src/app/dataeventrecords/components/dataeventrecords-edit.component.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Observable } from 'rxjs';
3-
import { map } from 'rxjs/operators';
43
import { Router, ActivatedRoute } from '@angular/router';
54
import { OidcSecurityService, AuthenticatedResult } from 'angular-auth-oidc-client';
65

@@ -37,10 +36,9 @@ export class DataEventRecordsEditComponent implements OnInit {
3736
}
3837

3938
ngOnInit() {
40-
this.isAuthenticated$.pipe(
41-
map((isAuthenticated: any) => {
42-
console.log('isAuthorized: ' + isAuthenticated);
43-
}));
39+
this.isAuthenticated$.subscribe((isAuthenticated: AuthenticatedResult) => {
40+
console.log('isAuthorized: ' + isAuthenticated.isAuthenticated);
41+
});
4442

4543
this._route.params.subscribe((params:any) => {
4644
const id = +params['id']; // (+) converts string 'id' to a number

AngularCliCodeFlowPkce/src/app/dataeventrecords/components/dataeventrecords-list.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class DataEventRecordsListComponent implements OnInit {
3030

3131
ngOnInit() {
3232
this.isAuthenticated$.pipe(
33-
switchMap((isAuthenticated: any) => this.getData(isAuthenticated))
33+
switchMap((isAuthenticated: AuthenticatedResult) => this.getData(isAuthenticated.isAuthenticated))
3434
).subscribe(
3535
(data: any) => this.DataEventRecords = data,
3636
() => console.log('getData Get all completed')

0 commit comments

Comments
 (0)