Skip to content
Back

Check if user is logged in.

  • 1
  • Flutter
  • Accounts
plushteddy
9 Sep, 2023, 15:45

Hello, how can I check if a user is logged in?

TL;DR
The user wants to check if a user is logged in. One suggested solution is to use the `isUserLoggedIn` function which makes an API call to verify if the user is logged in. Another suggestion is to use `StatefulWidget` instead of `StatelessWidget` and call the function within `initState`. It is also advised to handle exceptions properly. The user provided some code but encountered an error `AppwriteException: general_unauthorized_scope`. The issue may be caused by not awaiting the API call or not handling the exception correctly.
otik
9 Sep, 2023, 15:58

I think that only way is to fetch accou, accout.get()

plushteddy
9 Sep, 2023, 15:58

I did it like that, but it always shows the logged in screen, and throws an error

code:

TypeScript
class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {

    bool isUserLoggedIn() {
      try {
        account().get();
        return true;
      } catch (error) {
        return false;
      }
    }

    Widget homeScreen = isUserLoggedIn() ? LoggedInHomeScreen() : NotLoggedInScreen();

    return MaterialApp(
      title: 'Outfytr',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: const Color.fromRGBO(113, 100, 231, 1)),
        useMaterial3: true,
      ),
      home: homeScreen,
    );
  }
}






class LoggedInHomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Logged In'),
      ),
      body: Center(
        child: Text('Logged In'),
      ),
    );
  }
}

class NotLoggedInScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Not Logged In'),
      ),
      body: Center(
        child: Text('Not Logged In'),
      ),
    );
  }
}```
plushteddy
9 Sep, 2023, 15:58

error:

TypeScript
Error: AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401)
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 288:49  throw_
packages/appwrite/src/client_mixin.dart 73:9                                  prepareResponse
packages/appwrite/src/client_browser.dart 214:14                              call
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50            <fn>
dart-sdk/lib/async/zone.dart 1661:54                                          runUnary
dart-sdk/lib/async/future_impl.dart 147:18                                    handleValue
dart-sdk/lib/async/future_impl.dart 784:44                                    handleValueCallback
dart-sdk/lib/async/future_impl.dart 813:13                                    _propagateToListeners
dart-sdk/lib/async/future_impl.dart 584:5                                     [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 657:7                                     callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                              _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                               _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 177:15           <fn>```
plushteddy
9 Sep, 2023, 16:10

pls help

hamed
9 Sep, 2023, 16:27

Yes, it's normal for it to throw an error if there is no logged-in user. You should handle the exception

plushteddy
9 Sep, 2023, 16:39

and how is that possible?

Drake
9 Sep, 2023, 16:45
  1. you need to await account.get()
  2. you shouldn't be calling that in the build step. perhaps you need to go through some more Flutter tutorials to learn how to structure an app
plushteddy
9 Sep, 2023, 16:47

in the second step you mean my bool function right?

Drake
9 Sep, 2023, 16:48

sort of. you can't/shouldn't be calling async methods in build

hamed
9 Sep, 2023, 17:13

Oh, I forgot about await. Anyway, I agree with Steven. Here is a brief sample, but you should use StatefulWidget instead and call your function within initState. (This is not a good practice. I recommend using a proper state management.)

TypeScript
Future<bool> isUserLoggedIn() async {
    //---------------------------------
    try {
      await appApi.getAccount.get();
      return true;
    } on AppwriteException catch (_) {
      return false;
    }
  }
Drake
9 Sep, 2023, 17:16

ya, and you'd need local state to update the UI accordingly with the result of isUserLoggedIn

plushteddy
9 Sep, 2023, 17:30

works thx!

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