move redirect after login responsibility into auth context

This commit is contained in:
Mads Rasmussen
2023-11-10 14:27:32 +01:00
parent 1ecdf4a6f4
commit d39f8976c1
2 changed files with 23 additions and 7 deletions

View File

@@ -74,12 +74,11 @@ export class UmbAppElement extends UmbLitElement {
async #setup() {
if (this.serverUrl === undefined) throw new Error('No serverUrl provided');
const redirectUrl = `${window.location.origin}${this.backofficePath}`;
this.#serverConnection = new UmbServerConnection(this.serverUrl);
await this.#serverConnection.connect();
this.#authContext = new UmbAuthContext(this, this.serverUrl, redirectUrl, this.bypassAuth);
this.#authContext = new UmbAuthContext(this, this.serverUrl, this.backofficePath, this.bypassAuth);
new UmbAppContext(this, { backofficePath: this.backofficePath, serverUrl: this.serverUrl });
// Try to initialise the auth flow and get the runtime status

View File

@@ -8,14 +8,17 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
#isAuthorized = new UmbBooleanState<boolean>(false);
readonly isAuthorized = this.#isAuthorized.asObservable();
isBypassed = false;
#isBypassed = false;
#backofficePath: string;
#authFlow;
constructor(host: UmbControllerHostElement, serverUrl: string, redirectUrl: string, isBypassed: boolean) {
constructor(host: UmbControllerHostElement, serverUrl: string, backofficePath: string, isBypassed: boolean) {
super(host);
this.isBypassed = isBypassed;
this.#authFlow = new UmbAuthFlow(serverUrl, redirectUrl);
this.#isBypassed = isBypassed;
this.#backofficePath = backofficePath;
this.#authFlow = new UmbAuthFlow(serverUrl, this.#getRedirectUrl());
this.provideContext(UMB_AUTH_CONTEXT, this);
}
@@ -26,8 +29,12 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
return this.#authFlow.makeAuthorizationRequest();
}
/**
* Checks if the user is authorized. If Authorization is bypassed, the user is always authorized.
* @returns {boolean} True if the user is authorized, otherwise false.
*/
getIsAuthorized() {
if (this.isBypassed) {
if (this.#isBypassed) {
this.#isAuthorized.next(true);
return true;
} else {
@@ -37,6 +44,10 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
}
}
/**
* Sets the initial state of the auth flow.
* @returns {Promise<void>}
*/
setInitialState(): Promise<void> {
return this.#authFlow.setInitialState();
}
@@ -55,8 +66,14 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuth {
/**
* Signs the user out by removing any tokens from the browser.
* @return {*} {Promise<void>}
* @memberof UmbAuthContext
*/
signOut(): Promise<void> {
return this.#authFlow.signOut();
}
#getRedirectUrl() {
return `${window.location.origin}${this.#backofficePath}`;
}
}