Back

Automatic Login Current Session

  • 0
  • Android
  • Accounts
  • Users
asapsonter
22 May, 2023, 08:35

I have tried the approach below to automatically login current User. But onDestroy, User still has to login

TypeScript
public class LoginActivity extends AppCompatActivity {
    private Client client;
    private Account account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        client = new Client();
        client.setEndpoint("https://[HOSTNAME_OR_IP]/v1");
        client.setProject("[PROJECT_ID]");
        client.setKey("[API_KEY]");

        account = new Account(client);
    }

    @Override
    protected void onResume() {
        super.onResume();

        try {
            account.get(new CoroutineCallback<>(
                    (result, error) -> {
                        if (error != null)
                            error.printStackTrace();
                        return;
                    }
            ));
            // User is logged in
        } catch (AppwriteException e) {
            // User is not logged in
        }
    }
}
TL;DR
The user is experiencing a delay before resuming the active session. They have shared a code snippet that shows the `onResume()` method and a `startActivity()` call. It seems that the login activity appears before navigating to the main fragment, causing a delay of at least 1.5 seconds. The user has also shared another code snippet that demonstrates an attempt to automatically log in the current user, but they mention that the user still has to log in onDestroy. There is no solution provided in the thread.
Drake
22 May, 2023, 14:57

What exactly happens when this is executed? It doesn't look like you're doing anything with the result of account.get() sooooo 🧐

asapsonter
1 Jun, 2023, 09:52
TypeScript
@Override
    protected void onResume() {
        super.onResume();
        AppwriteClient appwriteClient = new AppwriteClient(getApplicationContext());
        Account account = appwriteClient.getAccount();
        try {
            account.get(new CoroutineCallback<>(
                    (result, error) -> {
                        if (result != null)
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        Log.d("appwrite", "onResume:result " + result);

                    }
            ));
        } catch (AppwriteException e) {
            e.printStackTrace();
            finish();
        }

    }
asapsonter
1 Jun, 2023, 09:53

@Steven sorry for late reply. I have added a navigation so mainFragment. But i still get the same result. It lunches the loginActivity before navigating

Drake
1 Jun, 2023, 13:10

So the onResume is being logged?

asapsonter
2 Jun, 2023, 00:52

Yes, When they is an active Session, But the problem is when onResume loginActivity appears for atleast 1.5 sec before navigating to Mainfragment

Drake
2 Jun, 2023, 00:59

sorry i don't understand. also, what is the result that's logged?

asapsonter
2 Jun, 2023, 01:08

The log shows the user in active session. But my concern is the delay before resuming active session

Drake
2 Jun, 2023, 01:28

What do you mean?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more