@@ -22,16 +22,16 @@ public class PassportImpl
22
22
{
23
23
private const string TAG = "[Passport Implementation]" ;
24
24
public readonly IBrowserCommunicationsManager communicationsManager ;
25
- private PassportAnalytics analytics = new PassportAnalytics ( ) ;
25
+ private PassportAnalytics analytics = new ( ) ;
26
26
27
27
// Used for device code auth
28
- private DeviceConnectResponse deviceConnectResponse ;
28
+ private DeviceConnectResponse ? deviceConnectResponse ;
29
29
30
30
// Used for PKCE
31
- private bool pkceLoginOnly = false ; // Used to differentiate between a login and connect
32
- private UniTaskCompletionSource < bool > pkceCompletionSource ;
33
- private string redirectUri = null ;
34
- private string logoutRedirectUri = null ;
31
+ private bool pkceLoginOnly ; // Used to differentiate between a login and connect
32
+ private UniTaskCompletionSource < bool > ? pkceCompletionSource ;
33
+ private string ? redirectUri ;
34
+ private string ? logoutRedirectUri ;
35
35
36
36
#if UNITY_ANDROID
37
37
// Used for the PKCE callback
@@ -42,15 +42,15 @@ public class PassportImpl
42
42
// Used to prevent calling login/connect functions multiple times
43
43
private bool isLoggedIn = false ;
44
44
45
- public event OnAuthEventDelegate OnAuthEvent ;
45
+ public event OnAuthEventDelegate ? OnAuthEvent ;
46
46
47
47
public PassportImpl ( IBrowserCommunicationsManager communicationsManager )
48
48
{
49
49
this . communicationsManager = communicationsManager ;
50
50
}
51
51
52
- public async UniTask Init ( string clientId , string environment , string redirectUri = null ,
53
- string logoutRedirectUri = null , string deeplink = null )
52
+ public async UniTask Init ( string clientId , string environment , string ? redirectUri = null ,
53
+ string ? logoutRedirectUri = null , string ? deeplink = null )
54
54
{
55
55
this . redirectUri = redirectUri ;
56
56
this . logoutRedirectUri = logoutRedirectUri ;
@@ -85,7 +85,7 @@ public async UniTask Init(string clientId, string environment, string redirectUr
85
85
}
86
86
else
87
87
{
88
- InitRequest request = new InitRequest ( )
88
+ InitRequest request = new InitRequest
89
89
{
90
90
clientId = clientId ,
91
91
environment = environment ,
@@ -113,34 +113,32 @@ public async UniTask Init(string clientId, string environment, string redirectUr
113
113
114
114
public async UniTask < bool > Login ( bool useCachedSession = false , Nullable < long > timeoutMs = null )
115
115
{
116
- string functionName = "Login" ;
117
116
if ( useCachedSession )
118
117
{
119
118
return await Relogin ( ) ;
120
119
}
121
- else
120
+
121
+ try
122
122
{
123
- try
124
- {
125
- Track ( PassportAnalytics . EventName . START_LOGIN ) ;
126
- SendAuthEvent ( PassportAuthEvent . LoggingIn ) ;
127
-
128
- await InitialiseDeviceCodeAuth ( functionName ) ;
129
- await ConfirmCode (
130
- PassportAuthEvent . LoginOpeningBrowser , PassportAuthEvent . PendingBrowserLogin , functionName ,
131
- PassportFunction . LOGIN_CONFIRM_CODE , timeoutMs ) ;
132
-
133
- Track ( PassportAnalytics . EventName . COMPLETE_LOGIN , success : true ) ;
134
- SendAuthEvent ( PassportAuthEvent . LoginSuccess ) ;
135
- isLoggedIn = true ;
136
- return true ;
137
- }
138
- catch ( Exception ex )
139
- {
140
- Track ( PassportAnalytics . EventName . COMPLETE_LOGIN , success : false ) ;
141
- SendAuthEvent ( PassportAuthEvent . LoginFailed ) ;
142
- throw ex ;
143
- }
123
+ const string functionName = "Login" ;
124
+ Track ( PassportAnalytics . EventName . START_LOGIN ) ;
125
+ SendAuthEvent ( PassportAuthEvent . LoggingIn ) ;
126
+
127
+ await InitialiseDeviceCodeAuth ( functionName ) ;
128
+ await ConfirmCode (
129
+ PassportAuthEvent . LoginOpeningBrowser , PassportAuthEvent . PendingBrowserLogin , functionName ,
130
+ PassportFunction . LOGIN_CONFIRM_CODE , timeoutMs ) ;
131
+
132
+ Track ( PassportAnalytics . EventName . COMPLETE_LOGIN , success : true ) ;
133
+ SendAuthEvent ( PassportAuthEvent . LoginSuccess ) ;
134
+ isLoggedIn = true ;
135
+ return true ;
136
+ }
137
+ catch ( Exception ex )
138
+ {
139
+ Track ( PassportAnalytics . EventName . COMPLETE_LOGIN , success : false ) ;
140
+ SendAuthEvent ( PassportAuthEvent . LoginFailed ) ;
141
+ throw ex ;
144
142
}
145
143
}
146
144
@@ -176,49 +174,47 @@ private async UniTask<bool> Relogin()
176
174
return false ;
177
175
}
178
176
179
- public async UniTask < bool > ConnectImx ( bool useCachedSession = false , Nullable < long > timeoutMs = null )
177
+ public async UniTask < bool > ConnectImx ( bool useCachedSession = false , long ? timeoutMs = null )
180
178
{
181
- string functionName = "ConnectImx" ;
182
179
if ( useCachedSession )
183
180
{
184
181
return await Reconnect ( ) ;
185
182
}
186
- else
183
+
184
+ // If the user called Login before and then ConnectImx, there is no point triggering device flow again
185
+ bool hasCredsSaved = await HasCredentialsSaved ( ) ;
186
+ if ( hasCredsSaved )
187
187
{
188
- // If the user called Login before and then ConnectImx, there is no point triggering device flow again
189
- bool hasCredsSaved = await HasCredentialsSaved ( ) ;
190
- if ( hasCredsSaved )
188
+ bool reconnected = await Reconnect ( ) ;
189
+ if ( reconnected )
191
190
{
192
- bool reconnected = await Reconnect ( ) ;
193
- if ( reconnected )
194
- {
195
- // Successfully reconnected
196
- return reconnected ;
197
- }
198
- // Otherwise fallback to device code flow
191
+ // Successfully reconnected
192
+ return reconnected ;
199
193
}
194
+ // Otherwise fallback to device code flow
195
+ }
200
196
201
- try
202
- {
203
- Track ( PassportAnalytics . EventName . START_CONNECT_IMX ) ;
204
- SendAuthEvent ( PassportAuthEvent . ConnectingImx ) ;
205
-
206
- await InitialiseDeviceCodeAuth ( functionName ) ;
207
- await ConfirmCode (
208
- PassportAuthEvent . ConnectImxOpeningBrowser , PassportAuthEvent . PendingBrowserLoginAndProviderSetup ,
209
- functionName , PassportFunction . CONNECT_CONFIRM_CODE , timeoutMs ) ;
210
-
211
- Track ( PassportAnalytics . EventName . COMPLETE_CONNECT_IMX , success : true ) ;
212
- SendAuthEvent ( PassportAuthEvent . ConnectImxSuccess ) ;
213
- isLoggedIn = true ;
214
- return true ;
215
- }
216
- catch ( Exception ex )
217
- {
218
- Track ( PassportAnalytics . EventName . COMPLETE_CONNECT_IMX , success : false ) ;
219
- SendAuthEvent ( PassportAuthEvent . ConnectImxFailed ) ;
220
- throw ex ;
221
- }
197
+ try
198
+ {
199
+ const string functionName = "ConnectImx" ;
200
+ Track ( PassportAnalytics . EventName . START_CONNECT_IMX ) ;
201
+ SendAuthEvent ( PassportAuthEvent . ConnectingImx ) ;
202
+
203
+ await InitialiseDeviceCodeAuth ( functionName ) ;
204
+ await ConfirmCode (
205
+ PassportAuthEvent . ConnectImxOpeningBrowser , PassportAuthEvent . PendingBrowserLoginAndProviderSetup ,
206
+ functionName , PassportFunction . CONNECT_CONFIRM_CODE , timeoutMs ) ;
207
+
208
+ Track ( PassportAnalytics . EventName . COMPLETE_CONNECT_IMX , success : true ) ;
209
+ SendAuthEvent ( PassportAuthEvent . ConnectImxSuccess ) ;
210
+ isLoggedIn = true ;
211
+ return true ;
212
+ }
213
+ catch ( Exception ex )
214
+ {
215
+ Track ( PassportAnalytics . EventName . COMPLETE_CONNECT_IMX , success : false ) ;
216
+ SendAuthEvent ( PassportAuthEvent . ConnectImxFailed ) ;
217
+ throw ex ;
222
218
}
223
219
}
224
220
@@ -274,7 +270,7 @@ private async UniTask<ConnectResponse> InitialiseDeviceCodeAuth(string callingFu
274
270
275
271
private async UniTask ConfirmCode (
276
272
PassportAuthEvent openingBrowserAuthEvent , PassportAuthEvent pendingAuthEvent ,
277
- string callingFunction , string functionToCall , Nullable < long > timeoutMs = null )
273
+ string callingFunction , string functionToCall , long ? timeoutMs )
278
274
{
279
275
if ( deviceConnectResponse != null )
280
276
{
@@ -553,9 +549,9 @@ public async UniTask Logout(bool hardLogout = true)
553
549
{
554
550
SendAuthEvent ( PassportAuthEvent . LoggingOut ) ;
555
551
556
- string logoutUrl = await GetLogoutUrl ( ) ;
557
552
if ( hardLogout )
558
553
{
554
+ var logoutUrl = await GetLogoutUrl ( ) ;
559
555
OpenUrl ( logoutUrl ) ;
560
556
}
561
557
@@ -859,10 +855,7 @@ private void TrySetPKCECanceled()
859
855
private void SendAuthEvent ( PassportAuthEvent authEvent )
860
856
{
861
857
PassportLogger . Debug ( $ "{ TAG } Send auth event: { authEvent } ") ;
862
- if ( OnAuthEvent != null )
863
- {
864
- OnAuthEvent . Invoke ( authEvent ) ;
865
- }
858
+ OnAuthEvent ? . Invoke ( authEvent ) ;
866
859
}
867
860
868
861
protected virtual void OpenUrl ( string url )
0 commit comments