@@ -12,7 +12,6 @@ import android.util.Patterns
1212import androidx.annotation.RequiresApi
1313import androidx.core.content.ContextCompat
1414import androidx.core.net.toUri
15- import androidx.core.view.WindowCompat
1615import androidx.credentials.CreatePasswordRequest
1716import androidx.credentials.CredentialManager
1817import androidx.credentials.GetCredentialRequest
@@ -34,8 +33,9 @@ import java.util.Base64
3433
3534class OauthEssentialsModule (reactContext : ReactApplicationContext ) :
3635 NativeOauthEssentialsSpec (reactContext) {
36+ private var appleSignInPromiseSettler: RCTPromiseSettler ? = null
3737
38- private val deepLinkReceiver = object : BroadcastReceiver () {
38+ private val receiver = object : BroadcastReceiver () {
3939 override fun onReceive (context : Context ? , intent : Intent ? ) {
4040 val url = intent?.getStringExtra(" url" )
4141 Log .d(LOG_TAG , " Receiving deep link ${url} " )
@@ -64,12 +64,6 @@ class OauthEssentialsModule(reactContext: ReactApplicationContext) :
6464 }
6565 }
6666
67- enum class CredentialError (val code : String ) {
68- NO_ACTIVITY_ERROR (" NO_ACTIVITY_ERROR" ),
69- INVALID_RESULT_ERROR (" INVALID_RESULT_ERROR" ),
70- NOT_SUPPORTED_ERROR (" NOT_SUPPORTED" )
71- }
72-
7367 @SuppressLint(" ObsoleteSdkInt" )
7468 override fun getTypedExportedConstants (): MutableMap <String , Boolean > {
7569 hasGooglePlayServices()
@@ -88,8 +82,7 @@ class OauthEssentialsModule(reactContext: ReactApplicationContext) :
8882
8983 private fun getAppleIdSupported (): Boolean = true
9084
91- private fun getCredentialManagerSupported (): Boolean =
92- Build .VERSION .SDK_INT >= Build .VERSION_CODES .S
85+ private fun getCredentialManagerSupported (): Boolean = true
9386
9487 @RequiresApi(Build .VERSION_CODES .O )
9588 override fun hybridSignIn (clientId : String , options : ReadableMap , promise : Promise ) {
@@ -222,79 +215,35 @@ class OauthEssentialsModule(reactContext: ReactApplicationContext) :
222215 return
223216 }
224217
225- if (getCredentialManagerSupported()) {
226- CoroutineScope (Dispatchers .IO ).launch {
227- try {
228- val manager = CredentialManager .create(activity)
229- val randomBytes = ByteArray (32 )
230- SecureRandom .getInstanceStrong().nextBytes(randomBytes)
231- val randomNonce = Base64 .getUrlEncoder().withoutPadding().encodeToString(randomBytes)
232-
233- val googleIdOption: GetGoogleIdOption = GetGoogleIdOption .Builder ()
234- .setNonce(randomNonce)
235- .setServerClientId(clientId)
236- .setFilterByAuthorizedAccounts(options.getBoolean(" authorizedAccounts" ))
237- .setAutoSelectEnabled(options.getBoolean(" autoSelectEnabled" ))
238- .build()
239-
240- val request: GetCredentialRequest = GetCredentialRequest .Builder ()
241- .addCredentialOption(googleIdOption)
242- .build()
243-
244-
245- resolveAndEmit(
246- CredentialFactory .fromCredentialResponse(
247- manager.getCredential(activity, request)
248- ), promise
249- )
250- } catch (e: GetCredentialCancellationException ) {
251- Log .d(LOG_TAG , e.toString())
252- resolveAndEmit(CredentialFactory .fromCancelled(), promise)
253- } catch (e: Throwable ) {
254- rejectAndEmitEvent(CredentialError .INVALID_RESULT_ERROR .code, e.toString(), promise)
255- }
256- }
257- } else {
258- CoroutineScope (Dispatchers .IO ).launch {
259- try {
260- Identity .getSignInClient(activity)
261- BeginSignInRequest .builder()
262- .setGoogleIdTokenRequestOptions(
263- BeginSignInRequest .GoogleIdTokenRequestOptions .builder()
264- .setSupported(true )
265- .setServerClientId(clientId)
266- .setFilterByAuthorizedAccounts(options.getBoolean(" authorizedAccounts" ))
267- .build()
268- )
269- .build()
270-
271- // suspendCancellableCoroutine { continuation ->
272- // oneTapClient.beginSignIn(signInRequest)
273- // .addOnSuccessListener { res -> continuation.resume(res) }
274- // .addOnFailureListener { e -> continuation.resumeWithException(e) }
275- // }
276-
277- // todo needs to be launched from a different activity
278- //
279- // resolveAndEmit(
280- // CredentialFactory.fromLegacyCredentialResponse(
281- // oneTapClient.getSignInCredentialFromIntent(intent)
282- // ),
283- // promise
284- // )
285- } catch (e: ApiException ) {
286- when (e.statusCode) {
287- CommonStatusCodes .CANCELED -> {
288- resolveAndEmit(CredentialFactory .fromCancelled(), promise)
289- }
218+ CoroutineScope (Dispatchers .IO ).launch {
219+ try {
220+ val manager = CredentialManager .create(activity)
221+ val randomBytes = ByteArray (32 )
222+ SecureRandom .getInstanceStrong().nextBytes(randomBytes)
223+ val randomNonce = Base64 .getUrlEncoder().withoutPadding().encodeToString(randomBytes)
290224
291- else -> {
292- rejectAndEmitEvent(CredentialError .INVALID_RESULT_ERROR .code, e.toString(), promise)
293- }
294- }
295- } catch (e: Exception ) {
296- rejectAndEmitEvent(CredentialError .INVALID_RESULT_ERROR .code, e.toString(), promise)
297- }
225+ val googleIdOption: GetGoogleIdOption = GetGoogleIdOption .Builder ()
226+ .setNonce(randomNonce)
227+ .setServerClientId(clientId)
228+ .setFilterByAuthorizedAccounts(options.getBoolean(" authorizedAccounts" ))
229+ .setAutoSelectEnabled(options.getBoolean(" autoSelectEnabled" ))
230+ .build()
231+
232+ val request: GetCredentialRequest = GetCredentialRequest .Builder ()
233+ .addCredentialOption(googleIdOption)
234+ .build()
235+
236+
237+ resolveAndEmit(
238+ CredentialFactory .fromCredentialResponse(
239+ manager.getCredential(activity, request)
240+ ), promise
241+ )
242+ } catch (e: GetCredentialCancellationException ) {
243+ Log .d(LOG_TAG , e.toString())
244+ resolveAndEmit(CredentialFactory .fromCancelled(), promise)
245+ } catch (e: Throwable ) {
246+ rejectAndEmitEvent(CredentialError .INVALID_RESULT_ERROR .code, e.toString(), promise)
298247 }
299248 }
300249 }
@@ -367,7 +316,6 @@ class OauthEssentialsModule(reactContext: ReactApplicationContext) :
367316 }
368317 }
369318
370-
371319 /* *
372320 * Gets if has google play services.
373321 */
@@ -382,22 +330,25 @@ class OauthEssentialsModule(reactContext: ReactApplicationContext) :
382330 super .initialize()
383331
384332 Log .d(LOG_TAG , " Registering deep link broadcast receiver" )
385- val filter = IntentFilter (WEB_APPLE_ID_BROADCAST_INTENT )
386- val filter = IntentFilter (DEEPLINK_WEB_APPLE_INTENT )
387333 ContextCompat .registerReceiver(
388334 reactApplicationContext.applicationContext,
389- deepLinkReceiver,
390- filter,
335+ receiver,
336+ IntentFilter ().apply {
337+ addAction(APPLE_SIGN_IN_SUCCESS_RECEIVED_INTENT )
338+ },
391339 ContextCompat .RECEIVER_NOT_EXPORTED
392340 )
393341 }
394342
395343 override fun invalidate () {
396344 super .invalidate()
397345 Log .d(LOG_TAG , " Unregistering deep link broadcast receiver" )
398- reactApplicationContext.unregisterReceiver(deepLinkReceiver )
346+ reactApplicationContext.unregisterReceiver(receiver )
399347 }
400348
349+ /* *
350+ * Gets parse web url.
351+ */
401352 private fun parseWebUrl (url : String? ): Uri {
402353 val url = url?.trim()
403354 ? : throw IllegalArgumentException (" URL cannot be null" )
@@ -454,7 +405,8 @@ class OauthEssentialsModule(reactContext: ReactApplicationContext) :
454405 const val NAME = NativeOauthEssentialsSpec .NAME
455406
456407 const val LOG_TAG = " NativeOauthEssentials"
457- const val DEEPLINK_WEB_APPLE_INTENT = " com.oauthessentials.WEB_APPLE_DEEP_LINK_INTENT"
408+ const val APPLE_SIGN_IN_SUCCESS_RECEIVED_INTENT =
409+ " com.oauthessentials.APPLE_SIGN_IN_SUCCESS_RECEIVED_INTENT"
458410 const val DISPLAY_CUSTOM_TABS_INTENT = " com.oauthessentials.DISPLAY_CUSTOM_TABS"
459411 }
460412}
0 commit comments