Hello Everyone,
i try to load Okta config and i get this error:
ERROR Error: Okta config is not provided
at Object.createOktaAuth [as useFactory] (okta-okta-angular.js:481:13)
at Object.factory (core.mjs:6263:38)
at core.mjs:6164:43
at runInInjectorProfilerContext (core.mjs:867:9)
at R3Injector.hydrate (core.mjs:6163:17)
at R3Injector.get (core.mjs:6033:33)
at injectInjectorOnly (core.mjs:911:40)
at ɵɵinject (core.mjs:917:42)
at Object.OktaAuthStateService_Factory [as factory] (okta-okta-angular.js:407:45)
at core.mjs:6164:43
Context:
@okta/okta-angular: 6.3.0
@okta/okta-auth-js: 7.4.3
Angular: 17.0.0
using standalone without NgModule.
app.component.ts
import { Component, Inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Router, RouterOutlet } from '@angular/router';
import {
OKTA_AUTH,
OKTA_CONFIG,
OktaAuthModule,
OktaAuthStateService,
} from '@okta/okta-angular';
import { OktaAuth, AuthState } from '@okta/okta-auth-js';
import config from './app.config';
import { Observable, filter, map } from 'rxjs';
const oktaConfig = new OktaAuth(config.oidc);
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet, OktaAuthModule],
providers: [
{ provide: OKTA_AUTH, useValue: oktaConfig },
{ provide: OKTA_CONFIG, useValue: oktaConfig },
],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
title = 'afx-afoms-frontend';
public isAuthenticated$!: Observable<boolean>;
constructor(
private _router: Router,
private _oktaStateService: OktaAuthStateService,
@Inject(OKTA_AUTH) private _oktaAuth: OktaAuth
) {}
public ngOnInit(): void {
this.isAuthenticated$ = this._oktaStateService.authState$.pipe(
filter((s: AuthState) => !!s),
map((s: AuthState) => s.isAuthenticated ?? false)
);
}
public async signIn(): Promise<void> {
await this._oktaAuth.signInWithRedirect();
}
public async signOut(): Promise<void> {
await this._oktaAuth.signOut();
}
}
app.component.html
<div class="toolbar" role="banner">
@if ((isAuthenticated$ | async) === false) {
<button (click)="signIn()"> Sign in </button>
}@else {
<button (click)="signOut()">Sign out</button>
}
</div>