#[CLOSED] Check if user is logged in.

19 messages · Page 1 of 1 (latest)

obsidian bough
#

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

quasi dragon
#

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

obsidian bough
# quasi dragon I think that only way is to fetch accou, accout.get()

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

code:

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'),
      ),
    );
  }
}```
#

error:

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>```
#

pls help

trim ravine
#

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

sand owl
obsidian bough
sand owl
trim ravine
# obsidian bough and how is that possible?

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.)

Future<bool> isUserLoggedIn() async {
    //---------------------------------
    try {
      await appApi.getAccount.get();
      return true;
    } on AppwriteException catch (_) {
      return false;
    }
  }
sand owl
obsidian bough
#

works thx!

wind garden
reef belfry
wind garden
# reef belfry in case of an error, an exception will be thrown, thats by design & is not limit...

Yeah, but if you take a step back you might ask why this is throwing an error in the first place? Nothing went wrong with the request. "There is no active account" shouldn't be communicated through an error. It's just information we should be able to fetch normally. The account API needs an endpoint that doesn't require authorization for this so that we can have intuitive implementations for it. The error is based on a 401 or 403 because there isn't a valid user making the request right?

#

I think I read about the 401 somewhere, or saw it while debugging. It just makes you think there is something wrong in your own code 😛

#

Would be happy to patch it myself honestly, but if it's consistent across all the SDKs it's a very big design change that would require coordination it sounds like...

sand owl
#

[SOLVED] Check if user is logged in.