refactor: refactor webui
This commit is contained in:
@@ -1,53 +1,15 @@
|
||||
import { AppAuthMethod, AuthMethodEnum } from '@/app/auth/config';
|
||||
import { injectInjector } from '@/infra/di/inject';
|
||||
import { Injectable, type Injector } from '@outposts/injection-js';
|
||||
import {
|
||||
CHECK_AUTH_RESULT_EVENT,
|
||||
type CheckAuthResultEventType,
|
||||
OidcSecurityService,
|
||||
} from 'oidc-client-rx';
|
||||
import { NEVER, type Observable, map, of } from 'rxjs';
|
||||
|
||||
const BASIC_AUTH_IS_AUTHENTICATED$ = of(true) as Observable<true>;
|
||||
|
||||
const BASIC_AUTH_USER_DATA$ = of({});
|
||||
import { AUTH_PROVIDER } from '@/infra/auth/auth.provider';
|
||||
import { Injectable, inject } from '@outposts/injection-js';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
private injector: Injector = injectInjector();
|
||||
oidcSecurityService: OidcSecurityService | undefined;
|
||||
checkAuthResultEvent$: Observable<CheckAuthResultEventType>;
|
||||
constructor() {
|
||||
if (AppAuthMethod === 'oidc') {
|
||||
this.oidcSecurityService = this.injector.get(OidcSecurityService);
|
||||
this.checkAuthResultEvent$ = this.injector.get(CHECK_AUTH_RESULT_EVENT);
|
||||
} else {
|
||||
this.checkAuthResultEvent$ = NEVER;
|
||||
}
|
||||
}
|
||||
private authProvider = inject(AUTH_PROVIDER);
|
||||
|
||||
isAuthenticated$ = this.authProvider.isAuthenticated$;
|
||||
userData$ = this.authProvider.userData$;
|
||||
checkAuthResultEvent$ = this.authProvider.checkAuthResultEvent$;
|
||||
|
||||
setup() {
|
||||
if (AppAuthMethod === AuthMethodEnum.OIDC) {
|
||||
this.oidcSecurityService!.checkAuth().subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
get isAuthenticated$() {
|
||||
return (
|
||||
this.oidcSecurityService?.isAuthenticated$.pipe(
|
||||
map((s) => s.isAuthenticated)
|
||||
) ?? BASIC_AUTH_IS_AUTHENTICATED$
|
||||
);
|
||||
}
|
||||
|
||||
get userData$() {
|
||||
return (
|
||||
this.oidcSecurityService?.userData$?.pipe(map((s) => s.userData)) ??
|
||||
BASIC_AUTH_USER_DATA$
|
||||
);
|
||||
}
|
||||
|
||||
getAccessToken(): Observable<string | undefined> {
|
||||
return this.oidcSecurityService?.getAccessToken() ?? of(undefined);
|
||||
this.authProvider.setup();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user