Back

How to check if user is logged in.

  • 0
  • Users
  • Flutter
  • Accounts
plushteddy
11 Aug, 2023, 17:51

How can i check if a user is logged in?

TL;DR
The user wants to know how to check if a user is logged in and has encountered an error related to authorization. They have shared their code and are seeking assistance to fix the issue. One suggestion is to update the state to handle the error and refresh the UI accordingly. Additional tutorials and practice with Flutter state fundamentals are recommended. Here is a potential solution: - Update the `LoggedIn` widget to extend `StatelessWidget`. - Try calling `account().get()` and handle the exception by returning `NotLoggedIn()` if an error is thrown. - In the `Home` widget, add a logout button that calls `account().deleteSessions
plushteddy
11 Aug, 2023, 17:55

like that?

TypeScript
try {
account.get();
} catch(error) {
print("not logged in");
}```
Drake
11 Aug, 2023, 17:55

Yep! You can also add the language to enable syntax highlighting

plushteddy
11 Aug, 2023, 17:55

i actually did haha

Drake
11 Aug, 2023, 17:56

Oh sorry. Dart highlighting doesn't work on mobile 😑

plushteddy
11 Aug, 2023, 17:57

to logout i need to use account.deleteSessions()right?

plushteddy
11 Aug, 2023, 18:02

but then it will still show my logged in screen

Drake
11 Aug, 2023, 18:02

Depends on how you build your app...

plushteddy
11 Aug, 2023, 18:02

if this would be right it would show the not logged in screen

plushteddy
11 Aug, 2023, 18:03
TypeScript
class LoggedIn extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    try {
      account().get();

      return Home(title: 'Outfytr');
    } catch(error) {
      return NotLoggedIn();
    }
  }
}

class Home extends StatefulWidget {
  final String title;

  Home({required this.title});

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Align(
        alignment: Alignment.topRight,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            ElevatedButton(
              onPressed: () {
                account().deleteSessions();
              },
              child: Text("Logout"),
            ),
          ],
        ),
      ),
    );
  }
}
Drake
11 Aug, 2023, 18:05

You need to handle state and update the app accordingly...

plushteddy
11 Aug, 2023, 18:05

and how (sorry im new to flutter)

Drake
11 Aug, 2023, 18:06

It might be good to find some additional tutorials or so for flutter then

plushteddy
11 Aug, 2023, 19:11

i've watched some, but i dont get the mistake

Drake
11 Aug, 2023, 19:18

In order to refresh the UI, you need to update state

plushteddy
11 Aug, 2023, 19:20

So it should be a statusful widget?

Drake
11 Aug, 2023, 19:20

Yes

plushteddy
11 Aug, 2023, 19:44

but the authenticated checker still does not work

code:

TypeScript
class LoggedIn extends StatefulWidget {

  @override
  _LoggedInStatus createState() => _LoggedInStatus();

}

class _LoggedInStatus extends State<LoggedIn> {
  @override
  Widget build(BuildContext context) {

    try {

      account().get();
      return NotLoggedIn();
    } catch(error) {
      return Home();
    }
  }
}

class Home extends StatefulWidget {

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Outfytr"),
      ),
      body: Align(
        alignment: Alignment.topRight,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            ElevatedButton(
              onPressed: () {
                account().deleteSessions();
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(builder: (context) => NotLoggedIn()),
                );
              },
              child: Text("Logout"),
            ),
            Text(User.name().toString())
          ],
        ),
      ),
    );
  }
}```
Drake
11 Aug, 2023, 19:46

I think you need some more tutorials and practice with state fundamentals in flutter

plushteddy
11 Aug, 2023, 19:47

thats also a fix, somehow xD

plushteddy
11 Aug, 2023, 20:00

after some working I think the error that i get is not on the widget site, i think, that it's the way I check the logged in. Do you know the fix?

error: Error: AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401)

Drake
11 Aug, 2023, 20:04

If an exception is thrown they aren't logged in....so then you need to update state based on that

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