Back

What should I use to check if user is logged after oAuth login

  • 0
  • Flutter
  • Accounts
pitcairn1987
10 May, 2023, 13:13

Hi,

I'm trying to do oAuth login by google in my Flutter app. And I still have a problem with logging on the application side. After logging in to google, I see in the Appwrite panel that an account and session have been created.

Should I then use the method: account.get() or account.getSession('current') to check if the user is actually logged in? and move to the appropriate screen.

When I use the account.getSession('current') method I get the right data, but when I use account.get() I get error 401 (missing scope).

TL;DR
The user is experiencing an issue with logging out after using Google OAuth for login in their Flutter app. Logout works fine on web but not on mobile. The user suspects that the issue may be related to stored data in the browser. There is a suggestion to use dev tools to inspect the network traffic and check the request headers. The user also mentions that they have the same code for web and mobile, with the only difference being the use of a success URL for OAuth in the web version. They provide additional code and screenshots for reference. There is confusion about what specifically doesn't work, and there is a suggestion to use `account.get()`
Drake
10 May, 2023, 17:47

That's weird...they both should work. account.get() is more ideal because it's faster (due to caching and not needing to find the matching session)

pitcairn1987
11 May, 2023, 16:06

Hi, I realized that only happens in the mobile version. In the web version, when in the createOAuth2Session method I have the appropriate page passed in the success parameter, everything seems to be ok - logging in and logging out. However, in the mobile version, after logging in, a page appears for a moment with information about the missing of redirect url, but then the get() method returns the appropriate data and I log in the user. On the other hand, the problem seems to occur the next time the get() method is called, before the user logs out. Both the deleteSession(sessionId: 'current') and get methods return me a 401 error as if there is no session created, but there is.

pitcairn1987
11 May, 2023, 16:07

Should I also use success parameter in Flutter for mobile?

Drake
11 May, 2023, 18:13

sorry what doesn't work in which?

Also, how are you instantiating AppwriteService and what's the code in that class?

pitcairn1987
11 May, 2023, 19:39

Sorry. Logout doesn't work. in 1st screen I marked result of the getAccoutInfo() method which is basically account.get() method. It's ok. But when I want to logout (2nd screen) I get error 401 from the same previous method. I don' know why. I send You 2 additional screen with AppwriteService class and Main function where I init it.

Drake
11 May, 2023, 19:42

What were you saying about web vs mobile?

pitcairn1987
11 May, 2023, 19:47

I have the same code for web and mobile. Only difference is using success url for oAuth method in Web version. I don't use it in mobile. In Web everything works fine - login and logout

Drake
11 May, 2023, 19:50

However, in the mobile version, after logging in, a page appears for a moment with information about the missing of redirect url,

And then the user is redirected into the app automatically?

pitcairn1987
11 May, 2023, 19:59

Yes. It shows for a moment and then is redirected automatically

Drake
11 May, 2023, 20:05

can you use the dev tools to inspect the network traffic and see what request headers are used in that 2nd account.get() call?

https://docs.flutter.dev/tools/devtools/android-studio

pitcairn1987
12 May, 2023, 10:06

I hope you meant this:)

pitcairn1987
12 May, 2023, 10:16

and this is whole log from the beginning. First login and check session- it's ok. And next accout.get() and delete session - not ok

pitcairn1987
12 May, 2023, 10:30

I think I know where the problem could be. I did the test again. But this time I only cleared the application data, deleted the account and session in the Appwrite panel, but did not clear the data of the Chrome browser on the phone, which had previously saved the login data. Now after logging again, logging out worked as well. It may be related to the fact that the data is stored in the browser?

Drake
12 May, 2023, 15:21

but you said everything was working fine in web. do you mean stored in the mobile app? If so, yes, the app could be re-using an old session cookie

pitcairn1987
12 May, 2023, 17:54

Yes. In web everything is ok. But problem is still with logout in mobile, but only when I login by google oAuth first time (when google credential are not yet remembered in Chrome). When I try to logout right after login I get an error 401. When I repeat the same action, but with previously remembered credential everything is ok. I can login and right after also logout

Drake
12 May, 2023, 18:21

your session in chrome shouldn't affect anything...i just tested on android oauth login and then log out worked fine

Drake
12 May, 2023, 18:21
Drake
12 May, 2023, 18:23

as long as im logging out in the app.

Drake
12 May, 2023, 18:24

i do agree that weird things can happen if you delete your session outside of the app

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