Skip to content

Commit ae0dfc5

Browse files
committed
fix: minor fixes
1 parent c702d22 commit ae0dfc5

16 files changed

Lines changed: 277 additions & 223 deletions

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ dependencies {
6868
// Needed oauth libraries
6969
implementation 'androidx.browser:browser:1.9.0'
7070
implementation 'androidx.credentials:credentials:1.5.0'
71-
implementation 'androidx.credentials:credentials-play-services-auth:1.5.0'
71+
implementation("androidx.credentials:credentials-play-services-auth:1.6.0-rc01")
7272
implementation 'com.google.android.libraries.identity.googleid:googleid:1.2.0'
7373
implementation 'com.google.android.gms:play-services-auth:21.5.0'
7474
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.oauthessentials
2+
3+
enum class CredentialError(val code: String) {
4+
NO_ACTIVITY_ERROR("NO_ACTIVITY_ERROR"),
5+
INVALID_RESULT_ERROR("INVALID_RESULT_ERROR"),
6+
NOT_SUPPORTED_ERROR("NOT_SUPPORTED")
7+
}

android/src/main/java/com/oauthessentials/CustomTabsActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import android.content.Intent.FLAG_ACTIVITY_NO_HISTORY
66
import android.os.Bundle
77
import android.util.Log
88
import androidx.browser.customtabs.CustomTabsIntent
9-
import com.oauthessentials.OauthEssentialsModule.Companion.DEEPLINK_WEB_APPLE_INTENT
9+
import com.oauthessentials.OauthEssentialsModule.Companion.APPLE_SIGN_IN_SUCCESS_RECEIVED_INTENT
1010
import com.oauthessentials.OauthEssentialsModule.Companion.DISPLAY_CUSTOM_TABS_INTENT
1111
import com.oauthessentials.OauthEssentialsModule.Companion.LOG_TAG
1212

@@ -76,7 +76,7 @@ class CustomTabsActivity : Activity() {
7676
Log.d(LOG_TAG, "Received new incoming intent")
7777
intent.data?.let { uri ->
7878
sendBroadcast(
79-
Intent(DEEPLINK_WEB_APPLE_INTENT).apply {
79+
Intent(APPLE_SIGN_IN_SUCCESS_RECEIVED_INTENT).apply {
8080
setPackage(applicationContext.packageName)
8181
putExtra("url", uri.toString())
8282
}

android/src/main/java/com/oauthessentials/OauthEssentialsModule.kt

Lines changed: 41 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import android.util.Patterns
1212
import androidx.annotation.RequiresApi
1313
import androidx.core.content.ContextCompat
1414
import androidx.core.net.toUri
15-
import androidx.core.view.WindowCompat
1615
import androidx.credentials.CreatePasswordRequest
1716
import androidx.credentials.CredentialManager
1817
import androidx.credentials.GetCredentialRequest
@@ -34,8 +33,9 @@ import java.util.Base64
3433

3534
class 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
}

ios/AppleIdRetriever.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ public class AppleIdRetriever: RCTPromiseSettler,
77

88
override init(
99
resolve: @escaping RCTPromiseResolveBlock,
10-
reject: @escaping RCTPromiseRejectBlock) {
11-
super.init(resolve: resolve, reject: reject)
10+
reject: @escaping RCTPromiseRejectBlock,
11+
emitEvent: ((String, NSDictionary) -> Void)?
12+
) {
13+
super.init(resolve: resolve, reject: reject, emitEvent: emitEvent)
1214

1315
let controller = ASAuthorizationController(authorizationRequests: [
1416
ASAuthorizationAppleIDProvider().createRequest(),
@@ -60,6 +62,7 @@ public class AppleIdRetriever: RCTPromiseSettler,
6062
]
6163

6264
self.resolve?(result)
65+
self.emitEvent?("onCredentialSuccess", result as NSDictionary)
6366
}
6467
}
6568

@@ -69,5 +72,9 @@ public class AppleIdRetriever: RCTPromiseSettler,
6972
error.localizedDescription,
7073
error
7174
)
75+
self.emitEvent?("onCredentialFailure", [
76+
"code": CredentialError.invalidResult.rawValue,
77+
"message": error.localizedDescription
78+
])
7279
}
7380
}

ios/CredentialError.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public enum CredentialError: String {
2+
case noSignInAvailable = "NO_SIGN_IN_AVAILABLE"
3+
case invalidResult = "INVALID_RESULT_ERROR"
4+
case simultaneousCallError = "SIMULTANEOUS_CALL_ERROR"
5+
case notSupportedError = "NOT_SUPPORTED_ERROR"
6+
}

0 commit comments

Comments
 (0)