question

GordonS-9701 avatar image
0 Votes"
GordonS-9701 asked JarvanZhang-MSFT commented

Xamarin Forms Logout - how to clear ALL data

I am developing an app and currently only login with the same account. I logged in with a different account yesterday ... and found that the data on some pages was still there from the previous login, which is not ideal!!

The logout process does this:

     // Clear the credentials store
     CredentialsStorage.ClearAll();
     SessionManager.Instance.EndTrackSession();

     await Shell.Current.GoToAsync("//LoginPage");

Which seems to work, the credentials are updated / changed when logging in again but the data on a previously visited page (e.g. statement) still exists from the previous login and gets added to for the new login.

So, I assume the instance of the statement page is still in existance and therefore its data. How do I clear all of that? Or do I need to change how / when I load data?

The statement page, for example, has a CollectionView bound (ItemSource) to a ViewModel. The ViewModel constructor creates a new ObservableRangeCollection(). In OnAppearing(), I call LoadItemsCommand in the ViewModel to get the first lot of data.

It's good that the data is still there, while a user is logged in and switching between screens (I think?), but obviously I need to clear it when the user logs out / a new user logs in.

BTW, it's perfectly possible that I'm not fully grasping how Xamarin Forms and the Page lifecycle works, so any help or pointers would be most welcome!


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

but the data on a previously visited page (e.g. statement) still exists from the previous login and gets added to for the new login

Hi, GordonS. How do you store the data and do you use the database? Try using the database to store the related info of each user table in the database. When a user logins in, retrive the data from the database and pupulate the data to the page.

Check the doc about using databases in Xamarin.Forms.
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/databases

Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Maybe there is something more fundamental going on:

  1. Logon as user A, taken to Home page, go to Statement page and see data for user A, logoff using flyout menu option, returns to Login page.

  2. Logon as user B ... taken to Statement page (not Home page) and can see user A data plus user B data.

So - first issue is that after Login, Home page should always be displayed first.
Second issue, Statement page, and any other pages, should not still have data from previous user.

I assume that even though user A has logged out and user B logged in, the pages that user A visited still exist with the data they had before. I either need to remove any existing pages (and therefore their data), or force data to be refreshed somehow.

When logging in as user B, the constructor for the Statement page ViewModel is not called - so things don't get initialised / reset!?

0 Votes 0 ·

Logon as user B ... taken to Statement page (not Home page) and can see user A data plus user B data

How did you navigate to the 'HomePage' after login? When the user logins in, you could reset the MainPage of the application?

public class App : Application
{
    public static bool IsUserLoggedIn { get; set; }

    public App()
    {
        if (!IsUserLoggedIn)
        {
            MainPage = new NavigationPage(new LoginPage());
        }
        else
        {
            MainPage = new NavigationPage(new LoginNavigation.MainPage());
        }
    }
}
1 Vote 1 ·

That is effectively what I did, but I am using Shell so I used:


     // Reset / clear all pages
     Application.Current.MainPage = new AppShell();


and it seems to be working

1 Vote 1 ·
Show more comments