Skip to content

Commit

Permalink
misc: support relogin when jwt token changed (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux authored Oct 9, 2024
1 parent de9c385 commit b8ef9ae
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/customize-authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Developers can login to RingCX Embeddable using the [JWT auth flow](https://deve
```js
(function() {
var rcs = document.createElement("script");
rcs.src = "https://ringcentral.github.io/engage-voice-embeddable/adapter.js?clientId=YOU_OWN_JWT_APP_CLIENT_ID&clientSecret=YOUR_OWN_JWT_APP_CLIENT_SECRET&jwt=JWT_TOKEN";
rcs.src = "https://ringcentral.github.io/engage-voice-embeddable/adapter.js?clientId=YOU_OWN_JWT_APP_CLIENT_ID&clientSecret=YOUR_OWN_JWT_APP_CLIENT_SECRET&jwt=JWT_TOKEN&jwtOwnerId=optional_owner_id";
var rcs0 = document.getElementsByTagName("script")[0];
rcs0.parentNode.insertBefore(rcs, rcs0);
})();
Expand Down
2 changes: 2 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
enablePopup,
popupPageUri,
jwt,
jwtOwnerId,
hideCallNote,
} = parseUri(paramsUri);

Expand All @@ -59,6 +60,7 @@ const appUri = `${appUrl}?${obj2uri({
fromAdapter: 1,
fromPopup,
jwt,
jwtOwnerId,
hideCallNote,
_t: Date.now(),
})}`;
Expand Down
2 changes: 2 additions & 0 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
disableLoginPopup,
fromPopup,
jwt,
jwtOwnerId,
} = pathParams;

if (clientId) {
Expand Down Expand Up @@ -58,6 +59,7 @@ const phone = createPhone({
disableLoginPopup: !!disableLoginPopup,
fromPopup,
jwt,
jwtOwnerId,
});

const store = createStore(phone.reducer);
Expand Down
5 changes: 3 additions & 2 deletions src/modules/EvAuth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const DEFAULT_COUNTRIES = ['USA', 'CAN'];
deps: [
'EvClient',
'Auth',
'OAuth',
'Block',
'Alert',
'Locale',
Expand Down Expand Up @@ -292,7 +293,8 @@ class EvAuth extends RcModuleV2<Deps> implements Auth {
this._deps.auth.loggedIn &&
this.loginStatus !== loginStatus.AUTH_SUCCESS &&
this.loginStatus !== loginStatus.LOGIN_SUCCESS &&
!this.connecting
!this.connecting &&
!this._deps.oAuth.jwtOwnerChanged
) {
this.connecting = true;
// when login make sure the logoutByOtherTab is false
Expand Down Expand Up @@ -324,7 +326,6 @@ class EvAuth extends RcModuleV2<Deps> implements Auth {
await this._deps.block.next(this._logout);

const logoutAgentResponse = await this.logoutAgent(agentId);

// TODO: error handle when logout fail
// TODO: when failed need tell other tab not logout => this._deps.tabManager.send(tabManagerEvents.LOGOUT);
if (!logoutAgentResponse.message || logoutAgentResponse.message !== 'OK') {
Expand Down
44 changes: 42 additions & 2 deletions src/modules/OAuth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import messageTypes from '../../enums/messageTypes';
@Module({
name: 'OAuth',
deps: [
{ dep: 'OAuthOptions', optional: true }
{ dep: 'OAuthOptions', optional: true },
{ dep: 'Prefix' },
]
})
export default class OAuth extends OAuthBase {
protected _userLogout = false;
protected _jwtLogged = false;

openOAuthPage() {
if (this._deps.oAuthOptions.disableLoginPopup) {
Expand Down Expand Up @@ -43,22 +45,60 @@ export default class OAuth extends OAuthBase {
if (this._userLogout) {
return;
}
if (this._jwtLogged) {
return;
}
if (
!this._deps.auth.notLoggedIn
!this._deps.auth.notLoggedIn && (
!this._deps.oAuthOptions.jwtOwnerId ||
this._deps.oAuthOptions.jwtOwnerId === this.jwtOwnerId
)
) {
return;
}
if (this._deps.oAuthOptions.jwt) {
if (!this._deps.auth.notLoggedIn) {
this._userLogout = true;
// logout before jwt login, hack for evAuth
await this._deps.auth.logout();
}
this._jwtLogged = true;
this._deps.auth.setLogin();
this._deps.client.service.platform().login({
jwt: this._deps.oAuthOptions.jwt,
});
if (this._deps.oAuthOptions.jwtOwnerId) {
this.setJwtOwnerId(this._deps.oAuthOptions.jwtOwnerId);
}
}
},
{
multiple: true,
},
);
}

get jwtOwnerId() {
// check localStorage api availability
if (!window.localStorage) {
return null;
}
return localStorage.getItem(`${this._deps.prefix}-jwt-owner-id`);
}

setJwtOwnerId(jwtOwnerId: string) {
// check localStorage api availability
if (!window.localStorage) {
return;
}
localStorage.setItem(`${this._deps.prefix}-jwt-owner-id`, jwtOwnerId);
}

get jwtOwnerChanged() {
return (
(!!this._deps.oAuthOptions.jwtOwnerId) &&
this._deps.oAuthOptions.jwtOwnerId !== this.jwtOwnerId
);
}
}

2 changes: 2 additions & 0 deletions src/modules/Phone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ export function createPhone({
redirectUri,
fromPopup,
jwt,
jwtOwnerId,
}) {
const appVersion = buildHash ? `${version} (${buildHash})` : version;
const usePKCE = sdkConfig.clientId && !sdkConfig.clientSecret;
Expand Down Expand Up @@ -467,6 +468,7 @@ export function createPhone({
disableLoginPopup,
redirectUri,
jwt,
jwtOwnerId,
},
},
{ provide: 'AuthOptions', useValue: { usePKCE } },
Expand Down

0 comments on commit b8ef9ae

Please sign in to comment.