Back

Permissions not updating for user

  • 0
  • Flutter
  • Auth
Stargate
6 May, 2024, 07:18

I have an app that starts with users signing up and verifying their account. For some reason, only on the first "sign up" by a new user after the app is installed the permissions do not update after calling the updateVerification and getting the account. I have an appwrite function that adds a new user to a team on the verification event. I know this has something to do with permissions because I have changed the permissions through the appwrite console to allow guests to read and at step 3 I can get the documents.

A simplified outline of the process:

  1. App installed
  2. User signs up and verifies account
  3. Appwrite function adds new user to team
  4. AppwriteException: document_not_found
  5. Delete user from appwrite from console
  6. Sign up again and verify account
  7. Appwrite function adds new user to team
  8. Can read documents from step 3 (as should be with permissions set)

Here is the code (Flutter)

TypeScript
Future<void> setUser() async {
  try {
    this._currentUser.value = await this.account.get();
    this._currentUser.notifyListeners();
    authState.value = AuthState.authenticated;
  }
  catch (e) {
    this._currentUser.value = null;
    authState.value = AuthState.unauthenticated;
  }
  authState.notifyListeners();
}

Future<bool> confirmEmailVerification({required String userId, required String secret, required String dbid}) async {
  try{
    await this.account.updateVerification(userId: userId, secret: secret);
    await this.setUser();
    if (this._currentUser.value == null){
      throw Exception("No current user");
    }
    return true;
  }
  catch (e) {
    print("Error updating verification: " + e.toString());
    return false;
  }
}

Appwrite function

TypeScript
# Create client (endpoint, project, apikey)...

teams = Teams(client)

teams.create_membership(
    team_id = os.environ['TEAM_ID'],
    roles = [os.environ['USER_ROLE']],
    user_id = context.req.body[os.environ['USER_ID']]
)

Any insights/thoughts are greatly appreciated

TL;DR
Developers are facing an issue where permissions are not updating correctly for users after the initial sign-up in their app. Despite changing permissions through the appwrite console, an 'AppwriteException: document_not_found' occurs on the first sign-up. Deleting and re-signing up fixes the issue. **Solution**: The problem might be due to the timing of how the user object is being fetched after the verification process. Ensure that the user object is successfully retrieved and updated with permissions before performing any actions that require those permissions, such as adding a user to a team.
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