Skip to content

Commit c740bff

Browse files
authored
Merge pull request #1132 from umbraco/feature/signout-on-server
Sign out on server
2 parents b8e2235 + dbcf3cd commit c740bff

File tree

5 files changed

+56
-27
lines changed

5 files changed

+56
-27
lines changed

src/apps/app/app.element.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ export class UmbAppElement extends UmbLitElement {
7777
async #setup() {
7878
if (this.serverUrl === undefined) throw new Error('No serverUrl provided');
7979

80-
/* All requests to the server requires the base URL to be set.
81-
We make sure it happens before we get the server status.
80+
/* All requests to the server requires the base URL to be set.
81+
We make sure it happens before we get the server status.
8282
TODO: find the right place to set this
8383
*/
8484
OpenAPI.BASE = this.serverUrl;
@@ -93,7 +93,7 @@ export class UmbAppElement extends UmbLitElement {
9393
// If the runtime level is "install" we should clear any cached tokens
9494
// else we should try and set the auth status
9595
if (this.#serverConnection.getStatus() === RuntimeLevelModel.INSTALL) {
96-
await this.#authContext.signOut();
96+
await this.#authContext.clearTokenStorage();
9797
} else {
9898
await this.#setAuthStatus();
9999
}

src/packages/user/current-user/modals/current-user/current-user-modal.element.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ export class UmbCurrentUserModalElement extends UmbLitElement {
5454

5555
private async _logout() {
5656
if (!this.#authContext) return;
57-
await this.#authContext.signOut();
58-
let newUrl = this.#appContext ? `${this.#appContext.getBackofficePath()}/login` : '/';
59-
newUrl = newUrl.replace(/\/\//g, '/');
60-
location.href = newUrl;
57+
this.#authContext.signOut();
6158
}
6259

6360
render() {

src/shared/auth/auth-flow.ts

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -223,38 +223,57 @@ export class UmbAuthFlow {
223223
return !!this.#accessTokenResponse && this.#accessTokenResponse.isValid();
224224
}
225225

226+
/**
227+
* Forget all cached token state
228+
*/
229+
async clearTokenStorage() {
230+
await this.#storageBackend.removeItem(TOKEN_RESPONSE_NAME);
231+
232+
// clear the internal state
233+
this.#accessTokenResponse = undefined;
234+
this.#refreshToken = undefined;
235+
}
236+
226237
/**
227238
* This method will sign the user out of the application.
228239
*/
229240
async signOut() {
230-
// forget all cached token state
231-
await this.#storageBackend.removeItem(TOKEN_RESPONSE_NAME);
241+
const signOutPromises: Promise<unknown>[] = [];
232242

243+
// revoke the access token if it exists
233244
if (this.#accessTokenResponse) {
234-
// TODO: Enable this when the server supports it
235-
// const tokenRevokeRequest = new RevokeTokenRequest({
236-
// token: this.#accessTokenResponse.accessToken,
237-
// client_id: this.#clientId,
238-
// token_type_hint: 'access_token',
239-
// });
240-
241-
// await this.#tokenHandler.performRevokeTokenRequest(this.#configuration, tokenRevokeRequest);
245+
const tokenRevokeRequest = new RevokeTokenRequest({
246+
token: this.#accessTokenResponse.accessToken,
247+
client_id: this.#clientId,
248+
token_type_hint: 'access_token',
249+
});
242250

243-
this.#accessTokenResponse = undefined;
251+
signOutPromises.push(this.#tokenHandler.performRevokeTokenRequest(this.#configuration, tokenRevokeRequest));
244252
}
245253

254+
// revoke the refresh token if it exists
246255
if (this.#refreshToken) {
247-
// TODO: Enable this when the server supports it
248-
// const tokenRevokeRequest = new RevokeTokenRequest({
249-
// token: this.#refreshToken,
250-
// client_id: this.#clientId,
251-
// token_type_hint: 'refresh_token',
252-
// });
253-
254-
// await this.#tokenHandler.performRevokeTokenRequest(this.#configuration, tokenRevokeRequest);
256+
const tokenRevokeRequest = new RevokeTokenRequest({
257+
token: this.#refreshToken,
258+
client_id: this.#clientId,
259+
token_type_hint: 'refresh_token',
260+
});
255261

256-
this.#refreshToken = undefined;
262+
signOutPromises.push(this.#tokenHandler.performRevokeTokenRequest(this.#configuration, tokenRevokeRequest));
257263
}
264+
265+
// clear the internal token state
266+
signOutPromises.push(this.clearTokenStorage());
267+
268+
// wait for all promises to settle before continuing
269+
await Promise.allSettled(signOutPromises);
270+
271+
// clear the session on the server as well
272+
// this will redirect the user to the end session endpoint of the server
273+
// which will redirect the user back to the client
274+
// and the client will then try and log in again (if the user is not logged in)
275+
// which will redirect the user to the login page
276+
location.href = `${this.#configuration.endSessionEndpoint}?post_logout_redirect_uri=${this.#redirectUri}`;
258277
}
259278

260279
/**

src/shared/auth/auth.context.interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export interface IUmbAuthContext {
3434
*/
3535
getLatestToken(): Promise<string>;
3636

37+
/**
38+
* Clears the token storage.
39+
*/
40+
clearTokenStorage(): Promise<void>;
41+
3742
/**
3843
* Signs the user out by removing any tokens from the browser.
3944
*/

src/shared/auth/auth.context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ export class UmbAuthContext extends UmbBaseController implements IUmbAuthContext
6565
return this.#authFlow.performWithFreshTokens();
6666
}
6767

68+
/**
69+
* Clears the token storage.
70+
* @memberof UmbAuthContext
71+
*/
72+
clearTokenStorage() {
73+
return this.#authFlow.clearTokenStorage();
74+
}
75+
6876
/**
6977
* Signs the user out by removing any tokens from the browser.
7078
* @return {*} {Promise<void>}

0 commit comments

Comments
 (0)