Back

Issue with OAuth Callback Parameters in Appwrite Flutter SDK

  • 0
  • Flutter
  • Auth
  • Cloud
marcosasu
8 Jan, 2025, 14:42

I'm using the createOAuth2Token() function and I have the following questions:

First, I would like to know if the redirect link is correct. Second, when I try to create the token, I get a 500 error saying that the key or secret is invalid. After investigating, I noticed that the webAuth function in the client_io.dart file looks for the key parameter, but from the debugger, I can see that the response only includes secret, userId, and projectId. This is my function

TypeScript
      {required SignInProviders signInProviders}) async {
    try {
      final f = await Dependencies.serverAPi.account.createOAuth2Token(
          provider: OAuthProvider.google,
          scopes: [],
          success:
              'appwrite-callback-6723890e00073730d5e5://cloud.appwrite.io/v1/account/sessions/oauth2/google/success?project=6723890e00073730d5e5',
          failure:
              'appwrite-callback-6723890e00073730d5e5://cloud.appwrite.io/v1/account/sessions/oauth2/google/failure?project=6723890e00073730d5e5');

      final f1 = await Dependencies.serverAPi.account
          .createSession(userId: f.userId, secret: f.secret);
      final user = await Dependencies.serverAPi.account.get();

      GlobalDataContainer.userId = user.$id;
      GlobalDataContainer.userEmail = user.email ?? "";
      GlobalDataContainer.userName = user.name ?? "";
      Dependencies.applicationDataSource.setUserId(user.$id);
      bool userDataExists =
          await Dependencies.applicationDataSource.checkIfUserModelExists();
      if (userDataExists) {
        await Dependencies.applicationDataSource.initializeMainDataSource();
      }
      return {
        "status": "SIGNED_IN",
        "userId": user.$id,
        "email": user.email,
        "userName": user.name,
        "userDataExists": userDataExists
      };
    }
  }```
TL;DR
Issue: The OAuth callback parameters in the Appwrite Flutter SDK are causing problems. Solution: The key parameter is missing when calling the webAuth function, leading to a 500 error. The function looks for key, but the response only includes secret, userId, and projectId. To fix this, ensure the key parameter is included in the response.
marcosasu
8 Jan, 2025, 14:42

and this is the webauth from client_io,dart

TypeScript
    return FlutterWebAuth2.authenticate(
      url: url.toString(),
      callbackUrlScheme: callbackUrlScheme != null && _customSchemeAllowed
          ? callbackUrlScheme
          : "appwrite-callback-" + config['project']!,
      options: const FlutterWebAuth2Options(
        intentFlags: ephemeralIntentFlags,
      ),
    ).then((value) async {
      Uri url = Uri.parse(value);
      final key = url.queryParameters['key'];
      final secret = url.queryParameters['secret'];
      if (key == null || secret == null) {
        throw AppwriteException(
            "Invalid OAuth2 Response. Key and Secret not available.", 500);
      }
      Cookie cookie = Cookie(key, secret);
      cookie.domain = Uri.parse(_endPoint).host;
      cookie.httpOnly = true;
      cookie.path = '/';
      List<Cookie> cookies = [cookie];
      await init();
      _cookieJar.saveFromResponse(Uri.parse(_endPoint), cookies);
    });
  }```                

Note: when i add the projectId in the callbacks urlm the projectid is also in the payload located inside the "then" block of the webauth function.


Thank you in advance to everyone who takes the time to assist me; your help is greatly appreciated.
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