0

I am new to MVVM and would like to ask if this "layout" is good MVVM. For the start, the app (I use a cloud api for signin a user in and to receive some data) should automatically log a user in, in case he has already entered once the password. And then display other persons(which I receive) on the MainPage. Of course there is also a Login screen for first time users or if he logged out.

How should I handle the auto login?

MainView <--> MainViewModel <---PersonModel

LoginView <--> LoginViewModel

AymenDaoudi
  • 6,237
  • 7
  • 43
  • 78
Michael
  • 727
  • 2
  • 10
  • 22

2 Answers2

1

If user already entered the password and you want to Auto-login that user then you can do it on your "MainViewModel". If user is coming first time then based on that you can invoke LoginView. From the LoginViewModel you can create a token object (ex UserInfo) that you can use in your application throughout.

Mukesh Rawat
  • 1,832
  • 14
  • 29
  • can you explain the token part a little more? – Michael Feb 03 '14 at 16:38
  • Extra info: I receive a AuthenticatedUser object which includes properties like name, email and so on. But no propertychanged so its hard to bind to this object how can I handle this? – Michael Feb 03 '14 at 16:40
  • token can be anything like GUID or custom class which can be used throughout in your application to validate/check user before doing any operations. It will be like a SecurityToken in which you can have userId, permissions, etc which you can send along with your AuthenticatedUser object. – Mukesh Rawat Feb 03 '14 at 16:45
  • How should i bind for example "name" which is part of the AuthenticatedUser to a textblock? It does not include propertychanged(its from the sdk) and is of course fetched from the web which takes time, so not available at the app start – Michael Feb 03 '14 at 16:48
  • I guess you are getting "AuthenticatedUser" object from your Authentication services, so you will not have propertychanged event in that. Best way to use name, email info is to create property in your viewmodel and bind it to View. Fill ViewModel property from AuthenticatedUser object on your ViewModel load (inside the constructor). – Mukesh Rawat Feb 03 '14 at 16:51
  • Thanks for your answer! – Michael Feb 03 '14 at 17:01
0

Your question is little bit vague, think about updating it and explain more, but still, from what I understand, both Views MainView and LoginView, will need to know about how the person is, hence it would be :

MainView <--> MainViewModel <---PersonModel (since the MainView may show data about people (Persons))

LoginView <--> LoginViewModel <---PersonModel (since the LoginView must interact with person, UserName, Email, Password ... )

For your question :

Where should I check for the autologin for example

I think it should be in the LoginViewModel Logic.

AymenDaoudi
  • 6,237
  • 7
  • 43
  • 78
  • your answer helped me already a lot...but the wp8 app starts in the MainView so I have to check there? – Michael Feb 03 '14 at 16:36
  • `but the wp8 app starts in the MainView` No not necessarily, you can set which Page/View the App should fire first by going to the App menifest and change the start page to the page you want, than perform a navigation to the MainView after your login logic finished with sucess, if you worry about passing info of which user is logged in from the LoginView to MainView, think about using Messanger Class to send information. – AymenDaoudi Feb 03 '14 at 16:39
  • But I don't want to show the login page....if you get what I mean. I just want to log a user in. – Michael Feb 03 '14 at 16:41
  • In that case, your problem is something else, you can use for that the Resource file to save the Login information when loggin the first time (if you want something more complex check this thread http://stackoverflow.com/questions/12657792/how-to-securely-save-username-password-local). then you would check int the App.cs file (mostly in the Launching event) every time you launch the App the values in the Resource file to decide where to navigate : the LoginView if no data stored, the MainView if someone already logged in. I hope this helps – AymenDaoudi Feb 03 '14 at 16:53