Skip to content

Commit

Permalink
Simplify login workflow (#3960)
Browse files Browse the repository at this point in the history
* Extract user manager from class

* Do not rely on code being present in url

* Simplify login workflow

* Fix linting issues
  • Loading branch information
carlesarnal authored Nov 10, 2023
1 parent a7ee32d commit f0d839e
Showing 1 changed file with 29 additions and 39 deletions.
68 changes: 29 additions & 39 deletions ui/ui-app/src/services/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@ export class AuthService implements Service {

private enabled: boolean = false;
private userManager: UserManager | undefined;
private oidcUser: User | undefined;
private user: User | undefined;

public init = () => {
if (this.config?.authType() === "oidc") {
this.userManager = new UserManager(this.getClientSettings());
}
};

public authenticateUsingOidc = (): Promise<any> => {
return this.userManager?.getUser().then((authenticatedUser) => {
if (authenticatedUser) {
this.oidcUser = authenticatedUser;
this.userManager?.startSilentRenew();
return Promise.resolve(authenticatedUser);
} else {
console.warn("Not authenticated, call doLogin!");
public async authenticate(): Promise<any> {
if (this.config?.authType() === "oidc") {
this.enabled = true;
return this.userManager?.signinRedirectCallback().then(user => {
this.user = user;
return Promise.resolve(user);
}).catch(() => {
return this.doLogin();
}
}) || Promise.reject(new Error("(authenticateUsingOidc) User manager is undefined."));
};
});

} else {
this.enabled = false;
return Promise.resolve("Authentication not enabled.");
}
}

public getClientSettings(): UserManagerSettings {
const configOptions: any = this.config?.authOptions();
Expand All @@ -59,28 +62,33 @@ export class AuthService implements Service {
filterProtocolClaims: true,
includeIdTokenInSilentRenew: true,
includeIdTokenInSilentSignout: true,
loadUserInfo: true
loadUserInfo: true,
automaticSilentRenew: true
};
}

public isAuthenticated(): boolean {
return this.userManager != null && this.oidcUser != null && !this.oidcUser.expired;
return this.userManager != null && this.user != null && !this.user.expired;
}

public doLogin = (): Promise<any> => {
return this.userManager?.signinRedirect().then(() => {
this.userManager?.startSilentRenew();
return this.userManager?.signinRedirectCallback();
}) || Promise.reject("(doLogin) User manager is undefined.");
return this.userManager?.getUser().then((authenticatedUser): Promise<any> => {
if (authenticatedUser) {
return Promise.resolve(authenticatedUser);
} else {
console.warn("Not authenticated, call doLogin!");
return this.userManager?.signinRedirect() || Promise.reject("(doLogin) User manager is undefined.");
}
}) || Promise.reject(new Error("(authenticateUsingOidc) User manager is undefined."));
};

public doLogout = () => {
this.userManager?.signoutRedirect({ post_logout_redirect_uri: window.location.href });
};

public getOidcToken = () => {
return this.oidcUser?.id_token;
};
public getOidcToken() {
return this.user?.id_token;
}

public isAuthenticationEnabled(): boolean {
return this.enabled;
Expand Down Expand Up @@ -127,24 +135,6 @@ export class AuthService implements Service {
return this.users?.currentUser().username === userId;
}

public authenticate(): Promise<any> {
if (this.config?.authType() === "oidc") {
this.enabled = true;
const url = new URL(window.location.href);
if (url.searchParams.get("state") || url.searchParams.get("code")) {
return this.userManager?.signinRedirectCallback().then(user => {
this.oidcUser = user;
return Promise.resolve(user);
}) || Promise.reject(new Error("User manager undefined."));
} else {
return this.authenticateUsingOidc();
}
} else {
this.enabled = false;
return Promise.resolve("Authentication not enabled.");
}
}

public getAuthInterceptor(): (config: AxiosRequestConfig) => Promise<any> {
/* eslint-disable @typescript-eslint/no-this-alias */
const self: AuthService = this;
Expand Down

0 comments on commit f0d839e

Please sign in to comment.