I’m trying to use the codes in Blockstack Android SDK tutorial and example projects there in my app. But, the problem is the field “UserData” remains null when I run my app and sign-in using Blockstack. I couldn’t understand what the problem can be. I’m also sharing my code below:
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.Intent
import android.view.View
import android.widget.Toast
import android.util.Log
import kotlinx.android.synthetic.main.blockstack_activity.*
import org.blockstack.android.sdk.BlockstackSession
import org.blockstack.android.sdk.BlockstackConfig
import org.blockstack.android.sdk.Scope
import org.blockstack.android.sdk.UserData
import java.net.URI
class BlockstackActivity : AppCompatActivity() {
private var _blockstackSession: BlockstackSession? = null
private val TAG = BlockstackActivity::class.java.simpleName
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.blockstack_activity)
signInButton.isEnabled = false
val config = java.net.URI("https://flamboyant-darwin-d11c17.netlify.com").run {
BlockstackConfig(
this,
URI("${this}/redirect"),
URI("${this}/manifest.json"),
kotlin.arrayOf(Scope.StoreWrite))
}
_blockstackSession = BlockstackSession(this, config,
onLoadedCallback = {
// Wait until this callback fires before using any of the
// BlockstackSession API methods
if (intent?.action == Intent.ACTION_VIEW) {
handleAuthResponse(intent)
}
onLoaded()
})
signInButton.setOnClickListener { _: View ->
blockstackSession().redirectUserToSignIn { userDataResult ->
if (userDataResult.hasValue) {
Log.d(TAG, "signed in!")
runOnUiThread {
onSignIn(userDataResult.value!!)
}
} else {
Toast.makeText(this, "error: " + userDataResult.error, Toast.LENGTH_SHORT).show()
}
}
}
}
private fun onLoaded() {
signInButton.isEnabled = true
blockstackSession().isUserSignedIn { signedIn ->
if (signedIn) {
signInButton.visibility = View.GONE
} else {
signInButton.visibility = View.VISIBLE
}
}
}
private fun onSignIn(userData: UserData) {
userDataTextView.text = "Signed in as ${userData.decentralizedID}"
signInButton.isEnabled = false
var nextIntent: Intent? = Intent(this@BlockstackActivity, NextActivity::class.java)
if (nextIntent == null) {
nextIntent = Intent(this@BlockstackActivity, NextActivity::class.java)
}
startActivity(nextIntent)
finish()
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent?.action == Intent.ACTION_VIEW) {
handleAuthResponse(intent)
}
}
private fun handleAuthResponse(intent: Intent) {
val response = intent.dataString
if (response != null) {
val authResponseTokens = response.split(':')
if (authResponseTokens.size > 1) {
val authResponse = authResponseTokens[1]
blockstackSession().handlePendingSignIn(authResponse, { userData ->
if (userData.hasValue) {
// The user is now signed in!
runOnUiThread {
onSignIn(userData.value!!)
}
}
})
}
}
}
fun blockstackSession(): BlockstackSession {
val session = _blockstackSession
if (session != null) {
return session
} else {
throw IllegalStateException("No session.")
}
}
}
Is the problem about my Blockstack account or my code? Thanks in advance.