@@ -53,6 +53,7 @@ import com.onesignal.user.internal.jwt.JwtTokenStore
5353import com.onesignal.user.internal.properties.PropertiesModelStore
5454import com.onesignal.user.internal.resolveAppId
5555import com.onesignal.user.internal.subscriptions.SubscriptionModelStore
56+ import kotlinx.coroutines.CancellationException
5657import kotlinx.coroutines.CompletableDeferred
5758import kotlinx.coroutines.CoroutineDispatcher
5859import kotlinx.coroutines.runBlocking
@@ -826,20 +827,21 @@ internal class OneSignalImp : IOneSignal,
826827 }
827828
828829 override suspend fun login (
829- externalId : String ,
830+ externalId : String? ,
830831 profile : OneSignalUserProfile ,
831832 jwtBearerToken : String? ,
832833 ): OneSignalResult <LoginData > =
833834 withContext(ioDispatcher) {
834835 Logging .log(LogLevel .DEBUG , " login(externalId: $externalId , profile: $profile , jwtBearerToken: ...${jwtBearerToken?.takeLast(8 )} )" )
835836
836837 profile.validationError()?.let {
838+ Logging .error(" login(externalId, profile) rejected: $it " )
837839 return @withContext OneSignalResult .failure(ErrorCode .INVALID_ARGUMENT , it)
838840 }
839841
840- suspendUntilInit(operationName = " login " )
842+ awaitInitForLogin()?. let { return @withContext it }
841843
842- if (isMissing(externalId, " login: externalId" )) {
844+ if (isMissing(externalId, " login: externalId" ) || externalId == null ) {
843845 return @withContext OneSignalResult .failure(
844846 ErrorCode .INVALID_ARGUMENT ,
845847 " login: externalId is required" ,
@@ -869,6 +871,20 @@ internal class OneSignalImp : IOneSignal,
869871 OneSignalResult .success(loginHelper.loginData(externalId, profile, fallbackOnesignalId = switched.onesignalId))
870872 }
871873
874+ /* * Waits for init; returns a NOT_INITIALIZED failure instead of throwing when it never started or failed. */
875+ internal suspend fun awaitInitForLogin (): OneSignalResult <LoginData >? =
876+ try {
877+ suspendUntilInit(operationName = " login" )
878+ null
879+ } catch (e: CancellationException ) {
880+ // A subclass of IllegalStateException, so it must be rethrown before the catch below.
881+ throw e
882+ } catch (e: IllegalStateException ) {
883+ // Callers can ignore the result, so the failure has to show up in the log.
884+ Logging .error(" login(externalId, profile) skipped: ${e.message} " , e)
885+ OneSignalResult .failure(ErrorCode .NOT_INITIALIZED , e.message, cause = e)
886+ }
887+
872888 private fun loginFailureMessage (wait : OperationWaitResult ): String =
873889 wait.httpStatusCode?.let { " Login did not complete (HTTP $it )." } ? : " Login did not complete."
874890
0 commit comments