1
1
import { OAuth as OAuthBase } from '@ringcentral-integration/widgets/modules/OAuth' ;
2
2
import { authMessages } from '@ringcentral-integration/commons/modules/Auth/authMessages' ;
3
3
import parseCallbackUri from '@ringcentral-integration/widgets/lib/parseCallbackUri' ;
4
+ import { loginStatus } from '@ringcentral-integration/commons/modules/Auth/loginStatus' ;
4
5
import { Module } from '@ringcentral-integration/commons/lib/di' ;
5
6
import { watch } from '@ringcentral-integration/core' ;
6
7
7
8
@Module ( {
8
9
name : 'OAuth' ,
9
- deps : [ ]
10
+ deps : [ 'Prefix' ]
10
11
} )
11
12
export class OAuth extends OAuthBase {
12
13
protected _authorizationCode ?: string ;
13
14
protected _authorizationCodeVerifier ?: string ;
14
15
protected _disableLoginPopup ?: boolean ;
15
16
protected _jwt ?: string ;
17
+ protected _userLogout = false ;
18
+ protected _autoLogged = false ;
16
19
17
20
constructor ( deps ) {
18
21
super ( deps ) ;
@@ -26,21 +29,53 @@ export class OAuth extends OAuthBase {
26
29
super . onInitOnce ( ) ;
27
30
watch (
28
31
this ,
29
- ( ) => this . ready ,
30
- async ( ) => {
32
+ ( ) => [
33
+ this . ready ,
34
+ this . _deps . auth . loginStatus ,
35
+ ] ,
36
+ async ( newValue , oldValue ) => {
31
37
if ( ! this . ready ) {
32
38
return ;
33
39
}
34
- if ( this . _deps . auth . loggedIn ) {
40
+ if ( this . _deps . auth . loginStatus === null ) {
35
41
return ;
36
42
}
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 ) {
39
67
return ;
40
68
}
41
- if ( this . _jwt ) {
69
+ this . _autoLogged = true ;
70
+ if ( this . _authorizationCode ) {
71
+ await this . _silentLoginWithCode ( ) ;
72
+ } else if ( this . _jwt ) {
42
73
await this . _deps . auth . jwtLogin ( this . _jwt ) ;
43
74
}
75
+ this . setExternalAuthId ( this . _deps . oAuthOptions . externalAuthId || '' ) ;
76
+ } ,
77
+ {
78
+ multiple : true ,
44
79
} ,
45
80
) ;
46
81
}
@@ -60,6 +95,22 @@ export class OAuth extends OAuthBase {
60
95
}
61
96
}
62
97
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
+
63
114
get oAuthUri ( ) {
64
115
const query = {
65
116
redirectUri : this . redirectUri ,
0 commit comments