Saving State after logging in


#1

Hello,

When logging in a user in my app, would I use the application-settings module to store a “Logged In” key that I could check to make sure the user is logged in?

Perhaps something like this in the pageLoaded function in the app.js so it is global?

if (appSettings.hasKey(“user_token”)===false){
//send the user to the login page
}

Thanks!

Randy


#2

hi, Randy,

That’s a valid solution. We do this in the Groceries codebase, which you can sift through here: https://github.com/NativeScript/sample-Groceries - the specific code to manage the token key is here: https://github.com/NativeScript/sample-Groceries/blob/master/app/shared/backend.service.ts


#3

Ok cool, yes I have been through that tutorial.

What is the best method for protecting all views that should be password protected? Adding code to view’s js file?

Randy


#4

hi, are you using Anguar? If so, you can use an ‘auth-guard’ service to ensure that a particular route is protected.


#5

No, using plain Javascript.


#6

Hm, dredging memory here. I think you might have, on each page, a test as the page loads, to ensure login (otherwise send user back to login). @bradwaynemartin is there a more solid way?


#7

@randy the code you gave for the app.js and using application-settings module to store a key is what I do. Then in the app just use that key/value to manipulate the UI and hooking into the pages life cycle events (navigatedTo fires early so that’s a good place).


#8

You could store the value anywhere and use it really but what you mentioned is a decent approach. Just don’t store sensitive info in application-settings it’s not an encrypted storage. So no passwords or anything :slight_smile:


#9

@bradwaynemartin ,

One last question and I think I will be good to go.

in app.js in the navigatedTo function how would I make sure not to transfer the user to the login page if they are already on the login page or if they are on the login page?

I tried doing some research before posting , but I couldn’t find anything to get me going.

Thank You,

Randy


#10

In the app.js you could check the key/value where you stored it. Just do this before calling the start() method in app.js and have a simple if/else to check if the user has signed in before and send to the correct page.

Hope that helps :slight_smile:


#11

Lets say I am not logged in and I open the app. How would I make sure that when the login page loads it doesn’t keep trying to transfer me to the login page over and over?

I am not sure if endless loops would happen in NativeScript or not like they would in a web based app.

Randy


#12

The app.js is your entry point so they won’t even make it to the login page
(your first module you set) until you call start() in the app.js telling
which module to load. That help?


#13

I think so. app.js is going to fire when the app is first opened only. So if the variable doesn’t exist I send them to the login page and then from there I am good to go. Correct?


#14

The app.js runs anytime the app is launched (not on resumes from the background though, there are specific events for resuming apps but not what you’re looking for). So when the app starts just check if you have them signed in and send them to the page you want for authenticated users and if they haven’t signed in route to the login. It’s simple, if I wasn’t on my phone id copy the exact code I use :slight_smile: in other words though don’t over think this. It’s just an if/else in app.js :thumbsup:


#15

That makes sense, I am good to go. Thank You.