Back

[SOLVED] Reuse session after createOAuth2Session

  • 0
  • Self Hosted
  • Flutter
  • Accounts
THE-E
1 Dec, 2023, 21:06

I created a Flutter app where I use createOAuth2Session to create an OAuth2-Session (to Google) to authenticate the user. Everything work fine so far. Only problem I am facing is, that I would like to reuse the session so I don't need to rerun the createOAuth2Session.

When I click on run createOAuth2Session I don't need to reauthenticate using account and password, but I would like to create an auto-login once authenticated successfully using OAuth2.

Unfortunately createOAuth2Session does not return a session, which I could reuse.

How would I implement an auto-login for OAuth2?

TL;DR
The user had been incorrectly implementing authentication in their Flutter app. They discovered that using `account.get()` can be used to retrieve the user's data. They also mentioned using `updateSession` for email-session and saving the session ID. For OAuth2, they wanted to achieve an auto-login process without requiring the user to re-authenticate unless they log out. They questioned if they should use `updateSession` and how to find the session ID. They mentioned that calling `createOAuth2Session` again after logging in with email and password caused an issue and they wanted to avoid the pop-up screen that appears when running `createOAuth
Mosh Ontong
1 Dec, 2023, 23:41

I think the solution so far, you can use any local database like Hive database in flutter.

  1. When user login then save the password in local database to cache his/her password
  2. Whenever the user visit your app again , you must have logic to check first if the user still authenticated if not then check if the local database cache his/her password, if yes then createAuth, otherwise redirect to login page

Please note this must become a security issue when cache the password of your user. I think you can read this https://developerb2.medium.com/store-data-securely-with-hive-flutter-cbad35981880

To encrypt the local database

Mosh Ontong
1 Dec, 2023, 23:42

Use a local database that supports encryption

Drake
2 Dec, 2023, 00:13

The Appwrite SDK automatically persists the session 🧐

THE-E
2 Dec, 2023, 08:43

I have already implemented this, when it comes to login with username and password. But the problem I am referring to is regarding OAuth2 authentication.

THE-E
2 Dec, 2023, 08:56

Yes, it does handle the session automatically if I use createOAuth2session once logged in. But I need to know beforehand whether the login can use createOAuth2session with the right provider, otherwise I would redirect to the login page of the provider.

Besides that, there is always a pop-up screen appearing when running createOAuth2session (which isn't ideal if you want to run a non-disruptive auto-login process). So ideally I would like to check beforehand, whether a session with an OAuth2 provider could be established in the past and then create a new JWT, rather than running createOAuth2session. Is that possible?

THE-E
2 Dec, 2023, 09:14

I would like to use updateSession, but I don't know where to find a session ID of the OAuth2 session. https://appwrite.io/docs/references/1.4.x/client-flutter/account#updateSession

Drake
3 Dec, 2023, 00:48

That's because you're not supposed to call create oauth2 session again...are you calling create email session again to check if the user has logged in after logging in with email and password?

THE-E
3 Dec, 2023, 08:09

For Email-Session: I have saved the username and password in the secured storage. And I reload them, if the user reopens the app (after it was closed). While I am writing this, I guess, I should probably use Β updateSession and should just save the sessionID, isn't it?

For OAuth2: What would be the correct approach? What I want to achieve is: I want to allow login using username and password or OAuth2. Once the user has logged in the user does not need to re-authenticate unless he logs out. So on startup, there should be an auto-login process.

THE-E
3 Dec, 2023, 09:53

So I have been doing authentication in appwrite wrong the whole time. I didn't know, that account.get() can be used.

TypeScript
loadUser() async {
  try {
    final user = await _account.get();
    _authStatus = AuthStatus.authenticated;
    accountData = user;
  } catch (e) {
    _authStatus = AuthStatus.unauthenticated;
  } finally {
    notifyListeners();
  }
}

enum  AuthStatus {
  uninitialized,
  unauthenticated,
  authenticated,
}

I haven't implemented everything yet, but during debugging I could see that _account.get() restores the session automatically (if available obviously).

Mosh Ontong
3 Dec, 2023, 13:34

When you tried to hot restart your flutter app, does it give you unauthenticated? it will redirect to login?

Drake
3 Dec, 2023, 14:11

[SOLVED] Reuse session after createOAuth2Session

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