Auto login


#1

Hello, im all new to using Ionic and so far i love.

My login works great, but i wish it would try to log in by auto when opening the app, if the fields are set already using the $localstoarge, are there any way to activate the $scope.doLogin from my controller?

.controller('loginCtrl', ['$scope', '$state', '$ionicHistory', '$localstorage', '$http', 'Auth', function ($scope, $state, $ionicHistory, $localstorage, $http, Auth) {
    // Form data for the login modal
    $scope.loginData = $localstorage.getObject("login");
    // Perform the login action when the user submits the login form
    $scope.doLogin = function () { .... }
}

Best regards
Mathias, a new developer


#2

Instead of auto completing the fields, could you not store the session of a successful login, then in your app.run() check for a user session and restore the required user details, bypassing the need to go through .doLogin?

You should move the login logic into a service and use the controller to call the methods exposed when required.


#3

@mathiasRobinson

Take a look at angular-http-auth => https://github.com/witoldsz/angular-http-auth

And, this post: http://robferguson.org/2015/07/27/authentication-for-ionic-apps/


#4

I need it to go though the login, because it would be getting some data back alongside if its logged in or not.
But ill try and make it into service.

Thank you


#5

By moving your login logic into a service you can re-use and utilise it elsewhere and in your case it’s appropriate to do so.


#6

ah okay. Thank you, ill be looking into that then

Seems to make my life much easier now.
Thanks for the help Delta