Login user Authentication and localstorage

Hi

i have implemented login system on my app.i am storing auth token on local storage.everything work fine once login is ok.
but every time i close the app and opens, login page pops up.how do i bypass login page when the user token is already exist on local storage.

my app structure goes like this:loginpage–>welcome page–>home page

$urlRouterProvider.otherwise(’/loginpage’);

if users token exists on local storage,i want to forward the page to welcome page.how do i implement this.

can someone please help me on this?

Ashique

2 Likes

put this in your app.js

if(window.localStorage['auth']) {
	$urlRouterProvider.otherwise('/welcome');
}else{
	$urlRouterProvider.otherwise('/login');
}
1 Like

Hi Sjerd

It didnt work.it is throwing error.where should i put this code…run or .config

Ashique

At the config before you define your states with $stateprovider

starter.config(function($stateProvider, $urlRouterProvider,$httpProvider,	$ionicConfigProvider) {

$ionicConfigProvider.prefetchTemplates(false);

if(window.localStorage['auth']) {
	$urlRouterProvider.otherwise('/tab/alerts');
}else{
	$urlRouterProvider.otherwise('/intro');
}

$stateProvider

Hi sjerd

Thanks a lot for your reply.

I am getting error if i add ionicconfigprovider inside function

which version of the ionic library do you use?

Hi Djerd

I am using V1.1.13.

Ash

try removing the $ionicConfigProvider parameter And the $ionicConfigProvider.prefetchtempaltes(false) statement from your code, it isn’t neccesaary for this to run.

Hi sjerd

Didnt work out bro :frowning: .Is this works for you.infact i cannot even add any parameters on .config function.everything is showing as error as showed you on image.

Ash

Try this on your app.js

.run(function ($ionicPlatform, $rootScope, User, $location) {
    $ionicPlatform.ready(function () {

    $rootScope.$on('$stateChangeStart',
        function(event, toState, toParams, fromState, fromParams){
            if (User.getCurrentUser() == null) {
                $location.path('/app/login');
            }
        });
})
2 Likes

Hi Pavei

Works Great!!!.Thank you thank you for your help.

Ashique

1 Like

hey @ashique can you share your login system? got a project that needs one. Thanks.

1 Like

check this out : apk and test it.

If this is what you’re looking for you can checkout both the client side code at : https://github.com/malikov/Authenticate.me-client-cordova-ionic

And the server side code at : https://github.com/malikov/Authenticate.me-Node-Server

Best of luck

Hey vmaliko

This is what exactly i was looking for…Thanks a lot bro.

Ashique

Easiest way to do this would be to use https://github.com/gsklee/ngStorage to store the token in your model. Take a look at this project https://github.com/sathishc/localreads for a token based authentication solution. The Ionic app can be found at https://play.google.com/store/apps/details?id=com.squareprism.localreads.ver1

1 Like

Ye works great for me ashique i can do all kinds of stuff with my config :o

you’re welcome, let me know how this works out for you or if you have questions.

VM.

If the run block I need to use a service (which makes an http.get), that is correct operation, the response of the service must come before continue with the routing.
As implemented using this scheme stateChangeStart?

I try to use the service outside in run since achieved with use at startup, not on each subsequent routing. But without the service response, continuous. That is a problem of asynchronism between the service and routing.

Alternatives? Some example of login at an external server ($ http) not only localStorage?

Hi @sjerd image

hiii guys, i am aswin … i am also facing a same trouble… i created a app and launched in playstore… user felt struggle to give numerous login when close my application… now i blind to solve this problem… Give some ideas and suggestion to fix this problem