Auto-sign in to hybrid apps?

Asked

Viewed 905 times

0

I made a login system that searches user in bd,I’m new in mobile development, and wanted a light (idea) how to login automatically after the first access. Just like the Whatsapp or face you enter with your login data once and then it logs in yourself, you only put the data back in again if you log out. I am doing in Ionic, Angularjs, php and used sessions for login.

grateful for the help.

2 answers

4

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

Generally speaking, an automatic login involves having some information about the user credentials that allows authentication without a user interaction.

Assuming your authentication system makes use of 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 your authentication system credentials in your application’s Localstorage using some Angularjs service. Every time your user accesses your application, you could check if the information exists there and log in automatically.

Again, it all depends on the form of authentication chosen. But roughly speaking, that would be it.

1

In my app, when I log in I save the user who logged in to the browser’s localstorage. When the user opens the application again, I just do a check to see if there are any users saved in these localstorage, if I direct to the first screen of my app, otherwise, I redirect to login screen.

In terms of code that would be:

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 the app.js, inside the . 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");
          }
   });
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.