Merge branch 'main' into feature/enable-controllers-to-host-controllers

This commit is contained in:
Niels Lyngsø
2023-06-27 10:40:51 +02:00
committed by GitHub
3 changed files with 28 additions and 9 deletions

View File

@@ -92,17 +92,14 @@ export class UmbAppElement extends UmbLitElement {
// Get the current runtime level
await this.#setInitStatus();
if (this.bypassAuth === false) {
// Get service configuration from authentication server
await this.#authFlow.setInitialState();
// Instruct all requests to use the auth flow to get and use the access_token for all subsequent requests
OpenAPI.TOKEN = () => this.#authFlow!.performWithFreshTokens();
OpenAPI.WITH_CREDENTIALS = true;
// If the runtime level is "install" we should clear any cached tokens
// else we should try and set the auth status
if (this.#runtimeLevel === RuntimeLevelModel.INSTALL) {
await authContext.signOut();
} else {
await this.#setAuthStatus(authContext);
}
authContext.isLoggedIn.next(true);
// Initialise the router
this.#redirect();
} catch (error) {
@@ -158,6 +155,19 @@ export class UmbAppElement extends UmbLitElement {
this.#runtimeLevel = data?.serverStatus ?? RuntimeLevelModel.UNKNOWN;
}
async #setAuthStatus(authContext: UmbAuthContext) {
if (this.bypassAuth === false) {
// Get service configuration from authentication server
await authContext.setInitialState();
// Instruct all requests to use the auth flow to get and use the access_token for all subsequent requests
OpenAPI.TOKEN = () => this.#authFlow!.performWithFreshTokens();
OpenAPI.WITH_CREDENTIALS = true;
}
authContext.isLoggedIn.next(true);
}
#redirect() {
switch (this.#runtimeLevel) {
case RuntimeLevelModel.INSTALL:

View File

@@ -26,6 +26,10 @@ export class UmbAuthContext implements IUmbAuth {
});
}
setInitialState(): Promise<void> {
return this.#authFlow.setInitialState();
}
async fetchCurrentUser(): Promise<UmbLoggedInUser | undefined> {
const { data } = await tryExecuteAndNotify(this.#host, UserResource.getUserCurrent());

View File

@@ -2,6 +2,11 @@ import type { UmbLoggedInUser } from './types.js';
import type { Observable } from '@umbraco-cms/backoffice/external/rxjs';
export interface IUmbAuth {
/**
* Initialise the auth flow.
*/
setInitialState(): Promise<void>;
/**
* Get the current user's access token.
*