Not logged in even if the authorized activity gets launched

I run the login in the LoginActivity here (NB: that’s Kotlin!):

class LoginActivity: Activity() {

private val TAG = "LoginActivity"

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val mOktaAuth = OktaAppAuth.getInstance(this)
    if (mOktaAuth.isUserLoggedIn) {
        Log.i(TAG, "User is already authenticated, proceeding to token activity")
        startActivity(Intent(this, MainActivity::class.java))
        finish()
        return
    }
    mOktaAuth.init(this,
            object : OktaAppAuth.OktaAuthListener {
                override fun onSuccess() {
                    login(mOktaAuth)
                }

                override fun onTokenFailure(ex: AuthorizationException) {
                    Log.d(TAG, ex.error)
                }
            })
}

private fun login(mOktaAuth: OktaAppAuth) {
    val login = mOktaAuth.isUserLoggedIn
    if (!login) {
        val completionIntent = Intent(this@LoginActivity, MainActivity::class.java)
        val cancelIntent = Intent(this@LoginActivity, LoginActivity::class.java)
        cancelIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP

        mOktaAuth.login(this@LoginActivity,
                PendingIntent.getActivity(this@LoginActivity, 0, completionIntent, 0),
                PendingIntent.getActivity(this@LoginActivity, 0, cancelIntent, 0)
        )
    }
}

}

I correctly get redirected to MainActivity, but whenever i call performAuthorizedRequest NullPointerException is thrown:

02-21 00:49:09.703 14496-14496/it.projector.lamba.projector E/AndroidRuntime: 
FATAL EXCEPTION: main
  Process: it.projector.lamba.projector, PID: 14496
  java.lang.RuntimeException: Unable to start activity ComponentInfo{it.projector.lamba.projector/it.projector.lamba.projector.activities.MainActivity}: java.lang.NullPointerException: service cannot be null
	  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
	  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
	  at android.app.ActivityThread.-wrap11(Unknown Source:0)
	  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
	  at android.os.Handler.dispatchMessage(Handler.java:106)
	  at android.os.Looper.loop(Looper.java:164)
	  at android.app.ActivityThread.main(ActivityThread.java:6494)
	  at java.lang.reflect.Method.invoke(Native Method)
	  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
	  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
   Caused by: java.lang.NullPointerException: service cannot be null
	  at net.openid.appauth.Preconditions.checkNotNull(Preconditions.java:55)
	  at net.openid.appauth.AuthState.performActionWithFreshTokens(AuthState.java:523)
	  at net.openid.appauth.AuthState.performActionWithFreshTokens(AuthState.java:452)
	  at com.okta.appauth.android.OktaAppAuth.performAuthorizedRequest(OktaAppAuth.java:416)
	  at it.projector.lamba.projector.BackendService$Factory.getCurrentUser(BackendService.kt:37)
	  at it.projector.lamba.projector.activities.MainActivity.onCreate(MainActivity.kt:45)
	  at android.app.Activity.performCreate(Activity.java:7009)
	  at android.app.Activity.performCreate(Activity.java:7000)
	  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
	  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
	  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
	  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
	  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
	  at android.os.Handler.dispatchMessage(Handler.java:106) 
	  at android.os.Looper.loop(Looper.java:164) 
	  at android.app.ActivityThread.main(ActivityThread.java:6494) 
	  at java.lang.reflect.Method.invoke(Native Method) 
	  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
	  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

I Have no Idea why :confused: Furthermore, adding "offline_access" to the scopes in the JSON file makes Okta refuse to start the login and the authentication process fails so i had to remove it.

@jmelberg any ideas here?

Hey @Lamba92!

Regarding the offline_access scope - did you specify the “Refresh Token” grant type when setting up your Okta app?

After I wrote to you, deep down in another forum of yours I found the same solution and it worked, but I had as well to move the super.onCreate() method in the AuthorizedActivity (that i called MainActivity) after invoking the val mOktaAuth = OktaAppAuth.getInstance(this) (Kotlin here!), super weird!

Again in the MainActivity in your guides for Android you do not explicitly specify that you have to call mOktaAuth.init() but i noticed that things get unpredictable if you don’t do it.

Oh, and there’s that issue as well

Furthermore right now if I login with username and password the chrome tab do not get closed but I restart the app I am logged in and get correctly redirected to the MainActivity. I suspect that this happens because I had to hardcode in your hosted widget my other Angular application ID. Look here for more details