Skip to content

Commit ef0d850

Browse files
authored
misc: do not generate new token with jwt when logged (#1030)
* misc: do not generate new token with jwt when logged * fix: multiple tabs logout issue * misc: rename to externalAuthId * fix typo * misc: prefix issue
1 parent 143ba19 commit ef0d850

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

src/adapter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function init({
9696
authorizationCode,
9797
authorizationCodeVerifier,
9898
jwt,
99+
externalAuthId,
99100
defaultCallWith,
100101
enableFromNumberSetting,
101102
showMyLocationNumbers,
@@ -156,6 +157,7 @@ function init({
156157
authorizationCode,
157158
authorizationCodeVerifier,
158159
jwt,
160+
externalAuthId,
159161
defaultCallWith,
160162
enableFromNumberSetting,
161163
showMyLocationNumbers,

src/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const {
9191
authorizationCode,
9292
authorizationCodeVerifier,
9393
jwt,
94+
externalAuthId,
9495
defaultCallWith,
9596
enableFromNumberSetting,
9697
showMyLocationNumbers,
@@ -197,6 +198,7 @@ const phone = createPhone({
197198
enableLoadMoreCalls,
198199
isMainTab: mainTab === 'true',
199200
autoMainTab: typeof mainTab === 'undefined',
201+
externalAuthId,
200202
});
201203

202204
const store = createStore(phone.reducer);

src/modules/OAuth/index.ts

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { OAuth as OAuthBase } from '@ringcentral-integration/widgets/modules/OAuth';
22
import { authMessages } from '@ringcentral-integration/commons/modules/Auth/authMessages';
33
import parseCallbackUri from '@ringcentral-integration/widgets/lib/parseCallbackUri';
4+
import { loginStatus } from '@ringcentral-integration/commons/modules/Auth/loginStatus';
45
import { Module } from '@ringcentral-integration/commons/lib/di';
56
import { watch } from '@ringcentral-integration/core';
67

78
@Module({
89
name: 'OAuth',
9-
deps: []
10+
deps: ['Prefix']
1011
})
1112
export class OAuth extends OAuthBase {
1213
protected _authorizationCode?: string;
1314
protected _authorizationCodeVerifier?: string;
1415
protected _disableLoginPopup?: boolean;
1516
protected _jwt?: string;
17+
protected _userLogout = false;
18+
protected _autoLogged = false;
1619

1720
constructor(deps) {
1821
super(deps);
@@ -26,21 +29,53 @@ export class OAuth extends OAuthBase {
2629
super.onInitOnce();
2730
watch(
2831
this,
29-
() => this.ready,
30-
async () => {
32+
() => [
33+
this.ready,
34+
this._deps.auth.loginStatus,
35+
],
36+
async (newValue, oldValue) => {
3137
if (!this.ready) {
3238
return;
3339
}
34-
if (this._deps.auth.loggedIn) {
40+
if (this._deps.auth.loginStatus === null) {
3541
return;
3642
}
37-
if (this._authorizationCode) {
38-
await this._silentLoginWithCode();
43+
if (this._deps.auth.loginStatus === loginStatus.beforeLogout) {
44+
// Do not jwt login after logout
45+
this._userLogout = true;
46+
return;
47+
}
48+
if (oldValue[1] === loginStatus.loggedIn && newValue[1] === loginStatus.notLoggedIn) {
49+
this._userLogout = true;
50+
return;
51+
}
52+
if (this._userLogout) {
53+
return;
54+
}
55+
if (this._autoLogged) {
56+
return;
57+
}
58+
if (
59+
!this._deps.auth.notLoggedIn && (
60+
!this._deps.oAuthOptions.externalAuthId ||
61+
this._deps.oAuthOptions.externalAuthId === this.externalAuthId
62+
)
63+
) {
64+
return;
65+
}
66+
if (!this._authorizationCode && !this._jwt) {
3967
return;
4068
}
41-
if (this._jwt) {
69+
this._autoLogged = true;
70+
if (this._authorizationCode) {
71+
await this._silentLoginWithCode();
72+
} else if (this._jwt) {
4273
await this._deps.auth.jwtLogin(this._jwt);
4374
}
75+
this.setExternalAuthId(this._deps.oAuthOptions.externalAuthId || '');
76+
},
77+
{
78+
multiple: true,
4479
},
4580
);
4681
}
@@ -60,6 +95,22 @@ export class OAuth extends OAuthBase {
6095
}
6196
}
6297

98+
get externalAuthId() {
99+
// check localStorage api availability
100+
if (!window.localStorage) {
101+
return null;
102+
}
103+
return localStorage.getItem(`${this._deps.prefix}-external-auth-id`);
104+
}
105+
106+
setExternalAuthId(externalAuthId: string) {
107+
// check localStorage api availability
108+
if (!window.localStorage) {
109+
return;
110+
}
111+
localStorage.setItem(`${this._deps.prefix}-external-auth-id`, externalAuthId);
112+
}
113+
63114
get oAuthUri() {
64115
const query = {
65116
redirectUri: this.redirectUri,

src/modules/Phone/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ export function createPhone({
742742
authorizationCode,
743743
authorizationCodeVerifier,
744744
jwt,
745+
externalAuthId,
745746
defaultCallWith,
746747
enableFromNumberSetting,
747748
showMyLocationNumbers,
@@ -828,6 +829,7 @@ export function createPhone({
828829
authorizationCodeVerifier,
829830
disableLoginPopup,
830831
jwt,
832+
externalAuthId,
831833
},
832834
},
833835
{

0 commit comments

Comments
 (0)