make auth context now if auth is bypassed

This commit is contained in:
Mads Rasmussen
2023-11-10 11:42:46 +01:00
parent c83581c783
commit 58f4cf6be6
2 changed files with 12 additions and 10 deletions

View File

@@ -78,7 +78,7 @@ export class UmbAppElement extends UmbLitElement {
OpenAPI.BASE = this.serverUrl;
const redirectUrl = `${window.location.origin}${this.backofficePath}`;
this.#authContext = new UmbAuthContext(this, this.serverUrl, redirectUrl);
this.#authContext = new UmbAuthContext(this, this.serverUrl, redirectUrl, this.bypassAuth);
new UmbAppContext(this, { backofficePath: this.backofficePath, serverUrl: this.serverUrl });
// Try to initialise the auth flow and get the runtime status
@@ -207,14 +207,13 @@ export class UmbAppElement extends UmbLitElement {
}
}
#isAuthorized(): boolean {
if (!this.#authContext) return false;
return this.bypassAuth ? true : this.#authContext.isAuthorized();
}
#isAuthorizedGuard(): Guard {
return () => {
if (this.#isAuthorized()) {
if (!this.#authContext) {
throw new Error('[Fatal] AuthContext requested before it was initialized');
}
if (this.#authContext.isAuthorized()) {
return true;
}
@@ -222,7 +221,7 @@ export class UmbAppElement extends UmbLitElement {
window.sessionStorage.setItem('umb:auth:redirect', location.href);
// Make a request to the auth server to start the auth flow
this.#authContext!.login();
this.#authContext.login();
// Return false to prevent the route from being rendered
return false;

View File

@@ -8,10 +8,13 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
#isLoggedIn = new UmbBooleanState<boolean>(false);
readonly isLoggedIn = this.#isLoggedIn.asObservable();
isBypassed = false;
#authFlow;
constructor(host: UmbControllerHostElement, serverUrl: string, redirectUrl: string) {
constructor(host: UmbControllerHostElement, serverUrl: string, redirectUrl: string, isBypassed: boolean) {
super(host);
this.isBypassed = isBypassed;
this.#authFlow = new UmbAuthFlow(serverUrl, redirectUrl);
this.provideContext(UMB_AUTH_CONTEXT, this);
}
@@ -29,7 +32,7 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
}
isAuthorized() {
return this.#authFlow.isAuthorized();
return this.isBypassed ? true : this.#authFlow.isAuthorized();
}
setInitialState(): Promise<void> {