Back

When to load user's Data after auth

  • 0
  • Databases
  • Flutter
  • Accounts
djcali
23 Jul, 2023, 01:25

Hello All,

I have a question about the best practice on when to fetch some data related to the authenticated user. I have a user data collection which stores users address and some settings used through out the app. When the app loads i check for auth and then i need to get the data. I have a redirect set up in the router which will detect any changes in auth and go to the correct route.

TypeScript
  authenticated,
  unauthenticated,
}

final authController =
    StateNotifierProvider<AuthProvider, AuthStatus>((ref) => AuthProvider(ref));

class AuthProvider extends StateNotifier<AuthStatus> {
  AuthProvider(this.ref) : super(AuthStatus.unauthenticated);
  final Ref ref;

  late User _currentUser;

  User get currentUser => _currentUser;

  Future<AuthStatus> getUser() async {
    final account = ref.read(appwriteAccountProvider);

    try {
      final user = await account.get();
      _currentUser = user;
      state = AuthStatus.authenticated;

    ///// Should I get the data here?? ////

      return AuthStatus.authenticated;
    } on AppwriteException catch (e) {
      state = AuthStatus.unauthenticated;

      return AuthStatus.unauthenticated;
    }
  }
}```


```redirect: (context, state) async {
          final auth = await ref.read(authController.notifier).getUser();

          final bool loggingIn = state.matchedLocation == '/signin';


          final isGoingSignUp = state.matchedLocation == '/signup';



          if (auth == AuthStatus.unauthenticated) {
            return '/signin';
          }

          if (isGoingSignUp && auth == AuthStatus.authenticated) {
            return '/';
          }

          if (loggingIn) {
            return '/';
          }
          
          /// User is authenticated goto destination
          return null;
        }```
TL;DR
When should I fetch data related to the authenticated user? Should it be done in the `getUser` function in the `AuthProvider` class? You can fetch the user's data in the `getUser` function if it belongs to the user. If you are fetching some other data that also belongs to the user, it is recommended to do that elsewhere. Solution: Fetch the user's data in the `getUser` function if it is user-specific. For other data, fetch it in a separate function or class.
Drake
23 Jul, 2023, 02:10

Sure you can get the user data there since the authProvider is for the user. It contains data about the user and you're fetching data about the user so it makes sense.

If you're fetching some other data that belongs to the user, I would do that elsewhere.

djcali
23 Jul, 2023, 02:35

Perfect! thats what i thought. Thanks.

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