Automatically log in to hybrid applications?

0

I made a login system that searches for user in bd, I am new to mobile development, and wanted a light (idea) on how to login automatically after first access. Same in whatsapp or face that you enter your login data once and then it logs in alone, you just put the data back up again if you log off. I'm doing in ionic, angularjs, php and I used login sessions.

grateful for help.

    
asked by anonymous 21.11.2016 / 16:38

2 answers

2

Well, this goal will depend a lot on how you are doing the authentication.

In general, an automatic login involves having some information about the user's credentials that allows authentication without a user interaction.

Assuming your authentication system uses tokens, let's make some considerations:

1) O usuário acessa seu aplicativo, informa o login/senha e seu sistema de autenticação cria um token de acesso para ele.
2) Este token fica armazenado localmente em seu aplicativo e representa o usuário logado.
3) O token só expira quando o usuário clicar em um botão de sair.

In your case, in a practical way, you could store the credentials of your authentication system in the LocalStorage of your application using some AngularJs service. Whenever your user accesses your app, you could check if the information exists there and log in automatically.

Again, it all depends on the form of authentication you have chosen. But roughly, it would be around.

    
21.11.2016 / 19:11
0

In my app, when I log in I save the user who logged into the browser's localstorage. When the user opens the application again, I only do a check to see if there are any users saved in this location, if I have the first screen of my app, otherwise I redirect to the login screen.

In terms of code it would look like this:

In your login controller

//você deve estar fazendo algo assim
$http.post("http://suaUrl", dadosDoUsuário).success(function(user)) {
    localStorage.setItem("user", user); //primeiro parâmetro é o nome desse localStorage, e o segundo é o objeto usuário.
}

In app.js, within .run, you do:

App.js

.run(function($ionicPlatform, $state) { //atente para adicionar o $state
   $ionicPlatform.ready(function() { 
      var user = localStorage.getItem("user");

          if(user !== "undefined" && user !== "null") {
             $state.go("suaPaginaPrincipal");
          } else {
                $state.go("suaPaginaDeLogin");
          }
   });
})
    
21.11.2016 / 20:23