#[SOLVED] OAuth2 and Email/Passwort is not working on iOS not working

16 messages · Page 1 of 1 (latest)

fringe blaze
#

Hello guys,
after updating to the latest Appwrite Flutter package (17.0.1) I am facing completely weird behavior.
None of my login ways are working. Whether the normal email/password:

Future<void> signIn(String email, String password) async {
    state = const AsyncValue.loading();
    try {
      await _wrapAppwriteCall(() async {
        final session = await _account.createEmailPasswordSession(
            email: email, password: password);
        final user = await _account.get(); <-- Fails here
      });
    } ...
  }

I get a valid session after the createEmailPasswordSession request, but the next request is failing with this error
AppwriteException (AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401))
No clue what I am doing wrong or what changed.
When I use the OAuth2 way then it is completely random behavior.

  Future<void> signInOAuth(String providerName) async {
    state = const AsyncValue.loading();
    try {
      await _wrapAppwriteCall(() async {
        final OAuthProvider provider;
        if (providerName == 'Google') {
          provider = OAuthProvider.google;
        } else {
          provider = OAuthProvider.apple;
        }
        await _account.createOAuth2Session(provider: provider);
        final session = await _account.getSession(sessionId: 'current');
        final user = await _account.get();
      });
    }....
  }

When I use google then it depends on the email account I select which error message I get.
For one user I get this error: AppwriteException (AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401))
For another user I get this error:Missing redirect URL
This is totally weird behavior.
Please give me a hint what could be wrong

north reef
fringe blaze
#

Correct

#

I think this is also important to mention. In the meantime you moved from the endpoint: https://cloud.appwrite.io/v1 to https://fra.cloud.appwrite.io/v1so i did this too and during some migrations you deleted my flutter ios App from the connected projects. Now it is connected again, but the login flow is not working 😕

north reef
fringe blaze
north reef
# fringe blaze Previously I used 13.0.0 - appwrite: 13.0.0 + appwrite: ^17.0.1

I just verified that the auth flow seems to work as expected, you use our playground for flutter as a reference - https://github.com/appwrite/playground-for-flutter

also note 17.0.0 was a major upgrade due to upgrade to flutter_web_auth_2, which is breaking. do check changelogs present here and for the package as well - https://github.com/appwrite/sdk-for-flutter/releases/tag/17.0.0

and lastly do let me know if downgrading fixes the problem 😅

GitHub

Simple examples that help you get started with Appwrite + Flutter (=❤️) - appwrite/playground-for-flutter

GitHub

What's Changed

Update flutter_web_auth_2 dependency to version 4.1.0
Update auth.html example in README.md to align with flutter_web_auth_2 documentation
Breaking changes:

Minimum iOS version...

fringe blaze
#

No the downgrade unfortunately did not fix the problem, now I get redirect URL is missing -.-

fringe blaze
#

@north reef than from what you are saying it looks like that on some downtimes my project got damaged. As I wrote, I am using this project about a year now with working oauth2 for google and apple. But now when I logged in again and checked why the apple login is not working I saw that my Flutter iOS app was not connected to the appwrite project. Is there a way to check wether everything is connected correctly?

fringe blaze
#

It looks like it works again.
What I did to fix it:

  • I connected the iOS again with Appwrite and sind a ping request.
  • Deactivated oAuth2 for Apple and activated it again
  • Deactivated oAuth2 for Google and activated it again
#

FIXED: OAuth2 and Email/Passwort is not working on iOS not working

#

[SOLVED] OAuth2 and Email/Passwort is not working on iOS not working

north reef
fringe blaze
#

@north reef I investigated further. I changed my code a little bit. So now I face the problem that I geht the 401: AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401) error.

To fix this problem I added a retry after a second delay

  Future<void> _refreshAuthStateWithRetry({int retryCount = 0}) async {
    try {
      final user = await _account.get();
      final session = await _account.getSession(sessionId: 'current');
      await _setupPushNotifications(user);
      final hasProfile = await _checkProfileStatus(user);
      state = AsyncValue.data(AuthState(
        user: user,
        hasProfile: hasProfile,
        provider: session.provider,
        status: AuthStatus.authenticated,
      ));
    } on AppwriteException catch (e) {
      if (e.code == 401 && retryCount == 0) {
        // Set loading state during retry
        state = AsyncValue.data(AuthState.loading());
        if (kDebugMode) {
          print('Retrying auth state refresh due to 401: $e');
        }
        // Wait for 1 second and retry once
        await Future.delayed(const Duration(seconds: 1));
        await _refreshAuthStateWithRetry(retryCount: 1);
      } else if (e.code == 401) {
        // Authentication failed after retry
        state = AsyncValue.data(AuthState.unauthenticated());
        if (kDebugMode) {
          print('Authentication failed after retry: $e');
        }
      } else {
        state = AsyncValue.data(AuthState.error('Failed to refresh auth state'));
      }
    }
  }

With this retry mechanism I can see that directly after a account.createOAuth2Session there is no valid session but if I wait and request again it works fine.
This are my logs:

flutter: Retrying auth state refresh due to 401: AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401)
[GoRouter] going to /
north reef
fringe blaze
#

This is the problem here:

// Current problematic implementation:
await _account.createOAuth2Session(provider: provider);
await _refreshAuthState(); // This executes sometimes too early

So I fixed it first with retrys but now I am working on implementing deep links for success and failure screens