Back

Not able to logout from home screen pls review my code and suggest me the changes.

  • 1
  • Flutter
  • Accounts
D5
23 Sep, 2023, 10:41

Sorry, I understand. Those days there were so many people asking things so it's normal to miss your post or have a delay on answers

TL;DR
The user is unable to logout from the home screen in their code. They are using the `account.get` function to check if the user is logged in, but the function will always resolve to true. To delete the current session, they should use `deleteSession(sessionId: 'current')`. They mention using the provider package to get the current user info. After logging out and reopening the app, it still starts on the home screen. They are checking in the main function to determine if the user is logged in or not, but by default, they are not going to any home screen. They mention a logout error: `App
satyam1mishra
23 Sep, 2023, 10:42
TypeScript

void logout() async {
  try {
    final session = await widget.account.get();
    print("Current Session: $session");

    if (session != null) {
      await widget.account.deleteSession(sessionId: session.$id);
      print("Session Deleted");
    } else {
      print("No active session found.");
    }

    Navigator.of(context).popUntil((route) => route.isFirst);
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        builder: (context) => LoginScreen(
          client: widget.client,
          account: widget.account,
        ),
      ),
    );
  } catch (e) {
    print("Logout error: $e");
    // Handle any errors that may occur during logout
    if (e is AppwriteException && e.message == "user_session_not_found") {
      print("No active session found. Navigating to login page...");
      Navigator.of(context).popUntil((route) => route.isFirst);
      Navigator.of(context).pushReplacement(
        MaterialPageRoute(
          builder: (context) => LoginScreen(
            client: widget.client,
            account: widget.account,
          ),
        ),
      );
    } else {
     
    }
  }
}
D5
23 Sep, 2023, 10:43

Correct

D5
23 Sep, 2023, 10:44

In else you can handle other true errors, like connection issues or server issues

satyam1mishra
23 Sep, 2023, 10:46

Logout error: AppwriteException: general_unauthorized_scope, User (role: guests) missing scope (account) (401) dont knw whats happening still getting same app started with homescreen and when clicking on logout button showing error

satyam1mishra
23 Sep, 2023, 10:48

its working but what abt the exception πŸ˜…

D5
23 Sep, 2023, 10:49

That means too the user is already logged out

satyam1mishra
23 Sep, 2023, 10:54

but after logout when i reload the app its still showing the home screen page which should not be showing beacuse i have loged out of the screen right?

D5
23 Sep, 2023, 10:56

From what I understand, the code you sent redirects you when you click logout button?

D5
23 Sep, 2023, 10:57

If so, and if by default you go to home screen, the app will go to home screen

satyam1mishra
23 Sep, 2023, 11:15

But by default I'm not going to any homescreen

satyam1mishra
23 Sep, 2023, 11:16

I'm checking it in the main function if the user had logged in move it to the home screen other wise login screen

satyam1mishra
23 Sep, 2023, 11:17

But after logout and when I reopen the app again it starts with homescreen with no reason

VincentGe
23 Sep, 2023, 13:47

Is that just the default view configued in your Flutter project?

Drake
23 Sep, 2023, 15:36

Sorry I fell asleep πŸ˜… did you figure out what account.get is?

satyam1mishra
24 Sep, 2023, 08:00

No

satyam1mishra
24 Sep, 2023, 08:01

It will give us the current user info

satyam1mishra
24 Sep, 2023, 08:24

I thinking of to use provider package to get things sorted out isn't it?

VincentGe
24 Sep, 2023, 14:27

Then the user is still logged in

VincentGe
24 Sep, 2023, 14:27

πŸ€”

VincentGe
24 Sep, 2023, 14:27

btw

VincentGe
24 Sep, 2023, 14:28

you can in fact do: deleteSession(sessionId: 'current');

VincentGe
24 Sep, 2023, 14:28

to delete the current session

Drake
24 Sep, 2023, 17:11

So...account.get is a function. Calling the function will tell you if the user is logged in or not. The function itself will never be null

Drake
24 Sep, 2023, 17:12

account.get != null will always resolve to true

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