How to log in app users?

  • 1
  • Question
  • Updated 6 years ago
  • Answered
Hi guys,

we're thinking about developing apps that let users register (login? google+ login? facebook login? don't know how). Once logged, we would like to give them some bonuses on particular events occurred.

What's the best way to do this, using PGB? Which plugins, or whatever else, are we meant to look for?

ANY idea will be very very appreciated. Also a "hey guys, just google for this: xxx".
Photo of Massimiliano Borgatti

Massimiliano Borgatti

  • 69 Posts
  • 0 Reply Likes

Posted 6 years ago

  • 1
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
If you like to have an own login system:
http://community.phonegap.com/nitobi/...

(very brief explanation of the general idea).
Photo of Massimiliano Borgatti

Massimiliano Borgatti

  • 69 Posts
  • 0 Reply Likes
Fast!

And what about storing locally some informations, such login credentials? Or google+ or facebook or whatevere else in order to autologin the second time?
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Well, it depends on whether you want a PERSON to log in, or a DEVICE.
If you store login credentials on a device, you can use 'localStorage' instead of 'sessionStorage'. But that would log in anyone using the device, which may not be what you want. Also, the same person with another device would have to enter his data again, because that device may not have the credentials, or maybe they are outdated.
(That said: I am not a fan of keeping passwords stored anywhere locally; just userID, maybe).
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Addition: I have no experience with external logins (Google, Facebook, etc.). But if you want to go that direction, keep in mind that iOS users maybe have no Google user ID and plenty of users don't have a Facebook account, either. So you must have an own system, anyway - I think - at least as a fallback.
Photo of Massimiliano Borgatti

Massimiliano Borgatti

  • 69 Posts
  • 0 Reply Likes
Tank you really.

My brain had a BANG and than a light. If you can provide me any tutorial or plugin regarding logins and localstorage, i will forever grateful.
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
I'm sorry, I don't have such documentation handy, right now.
But may I suggest you take some keywords from this thread, like
"phonegap javascript php login localstorage" and google around a bit? I'm sure someone will have posted something.
Photo of Massimiliano Borgatti

Massimiliano Borgatti

  • 69 Posts
  • 0 Reply Likes
yes i'm sure too

thank you very much
Photo of WebTabPlayer.com

WebTabPlayer.com

  • 7 Posts
  • 0 Reply Likes
This is the kind of thing I use on my Angular Phonegap app:

authService.save = function()
{
if(authService.apiKey != '')
{
window.localStorage["username"] = authService.username;
window.localStorage["password"] = authService.password;
window.localStorage["apiKey"] = authService.apiKey;
window.localStorage["account"] = angular.toJson(authService.account);
window.localStorage["token"] = authService.token;
}
};

authService.load = function()
{
if(window.localStorage["username"] != undefined && window.localStorage["password"] != undefined && window.localStorage["apiKey"] != undefined)
{
authService.username = window.localStorage["username"];
authService.password = window.localStorage["password"];
authService.apiKey = window.localStorage["apiKey"];
authService.account = angular.fromJson(window.localStorage["account"]);
authService.token = window.localStorage["token"];
}
};

I basically use the localStorage instance to save/load data on app close/open
Photo of Massimiliano Borgatti

Massimiliano Borgatti

  • 69 Posts
  • 0 Reply Likes
wow! thank you!

i wll test this code for sure!
Photo of WebTabPlayer.com

WebTabPlayer.com

  • 7 Posts
  • 0 Reply Likes
Basically, I rely on the apiKey stored in the localStorage to check if my user is still logged in or not. If I find data in the localStorage, I consider the user logged. Of course, each time I send a request to the server, I check the status code that the ajax request send backs. My PHP server is configured to return a 301 error code if the user info sent with the request are not valid. If something like this happens, then it means the localStorage values are not valid anymore and I ask to user to log in again.

In one of my application, I wanted the user to be able to stay logged in for a maximum amount of 48hours. The most reliable solution I found was as follow:
- 1) the user tries to log in
- 2) he then receives an apiKey that will be valid for the next 48 hours
- 3) I store those apiKey, login and password in localStorage and I use those values with each ajax calls
- 4) when I end up receiving a 301, it means the apiKey is not valid anymore, so I empty the localStorage data and I ask for a new login
- 5) Back to step 1

I've tested this on many devices and it seems to be working smoothly enough.