Session management for web3auth in android

When asking for help in this category, please make sure to provide the following details:

  • SDK Version:34
  • Platform: Android Studio
  • Browser Console Screenshots:
  • If the issue is related to Custom Authentication, please include the following information (optional):
    • Verifier Name:
    • JWKS Endpoint:
    • Sample idToken (JWT):

Also, kindly provide the Web3Auth initialization and login code snippet below. This will help us better understand your issue and provide you with the necessary assistance.

I tried to handle session management for the web3auth.

private void checkAndHandlePreviousUserSession() {
web3Auth.initialize().whenComplete((unused, error) → {
if (error == null) {
if (web3Auth.getUserInfo() != null) {
checkInternetConPB.setVisibility(View.INVISIBLE);
startActivity(new Intent(this, DebugActivity.class));
} else {
startActivity(new Intent(this, LoginActivity.class));
}
} else {
showAlertSnackbarWithMessage(
getString(R.string.errorCheckingSession) + error.getMessage()
);
}
});
}
I used the given code in my CheckInternetConnectionActivity.java class to check and handle session of previous login . I tried to use the build in method of web3auth and not the shared preferences . i.e. if the user has already a session then , it should be redirected to DebugActivity.class and to LoginActivity.class otherwise. But “web3Auth.getUserInfo() != null” is not returning true in this case and that’s why it’s stuck right here(provided in the screenshot where the progress bar is keep being displayed)

Following is the full code of my CheckInternetConnectionActivity.java class:-
package org.opendroneid.android.auth;

import static com.web3auth.core.types.Web3AuthOptionsKt.getSdkUrl;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;
import com.web3auth.core.Web3Auth;
import com.web3auth.core.types.Network;
import com.web3auth.core.types.Web3AuthOptions;

import org.opendroneid.android.R;
import org.opendroneid.android.app.DebugActivity;

public class CheckInternetConnection extends AppCompatActivity {

private Web3Auth web3Auth;
private ProgressBar checkInternetConPB;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check_internet_connection);
    checkInternetConPB = findViewById(R.id.checkInternetConPB);
    checkInternetConPB.setVisibility(View.VISIBLE);
    removeInputViews();


    instantiateWeb3Auth();
    if(isConnected()){
        checkAndHandlePreviousUserSession();
    }else{
        checkInternetConPB.setVisibility(View.INVISIBLE);
        addInputViews();
    }
}
private void removeInputViews(){
    ImageView no_internet_png = findViewById(R.id.no_internet_png);
    TextView no_internet_text = findViewById(R.id.no_internet_text);
    TextView check_your_connection = findViewById(R.id.check_your_connection);

    no_internet_png.setVisibility(View.INVISIBLE);
    no_internet_text.setVisibility(View.INVISIBLE);
    check_your_connection.setVisibility(View.INVISIBLE);
}
private void addInputViews(){
    ImageView no_internet_png = findViewById(R.id.no_internet_png);
    TextView no_internet_text = findViewById(R.id.no_internet_text);
    TextView check_your_connection = findViewById(R.id.check_your_connection);

    no_internet_png.setVisibility(View.VISIBLE);
    no_internet_text.setVisibility(View.VISIBLE);
    check_your_connection.setVisibility(View.VISIBLE);
}


public boolean isConnected() {
    try {
        String command = "ping -c 1 google.com";
        return (Runtime.getRuntime().exec(command).waitFor() == 0);
    } catch (Exception e) {
        return false;
    }
}
private void checkAndHandlePreviousUserSession() {
    web3Auth.initialize().whenComplete((unused, error) -> {
        if (error == null) {
            if (web3Auth.getUserInfo() != null) {
                checkInternetConPB.setVisibility(View.INVISIBLE);
                startActivity(new Intent(this, DebugActivity.class));
            } else {
                startActivity(new Intent(this, LoginActivity.class));
            }
        } else {
            showAlertSnackbarWithMessage(
                    getString(R.string.errorCheckingSession) + error.getMessage()
            );
        }
    });
}

private void showAlertSnackbarWithMessage(@NonNull String message) {
    Snackbar.make(findViewById(R.id.internet_connection), message, Snackbar.LENGTH_SHORT).show();
}

private void instantiateWeb3Auth() {
    final Web3AuthOptions options = new Web3AuthOptions(
            this,
            getString(R.string.client_id),
            Network.SAPPHIRE_DEVNET,
            null,
            Uri.parse(getString(R.string.whitelist_domain)),
            getSdkUrl(null),
            null, null,
            null, null, null,
            null, null
    );

    web3Auth = new Web3Auth(options);
}

}

Hey @mayananand2, I went through the code snippets you have shared an found out that you are passing null in sessionTime. When you are passing null, you are override the default 1 Day session persistence for the SDK. If you want to keep user logged in for a single day, you can avoid passing anything. If more than 1 day, you can pass the value in seconds. For example, if 3 days, then it would be 259200 ( 32460*60). Please check out our Android quick start sample.

Hey @mayananand2,

I hope the insights @Ayush shared about the sessionTime parameter have addressed your issue. Could you let us know if this has resolved your concern? We’re here to help if you need further assistance. Otherwise, please let us know if we can mark this thread as resolved.

Okay , thanks for the answer. Solved

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.