Login user Authentication and localstorage


#1

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


When i exit the app using navigator.app.exit on condition with out Logout,Again when i opened the app my account is going to login page?
#2

put this in your app.js

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

#3

Hi Sjerd

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

Ashique


#4

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

#5

Hi sjerd

Thanks a lot for your reply.

I am getting error if i add ionicconfigprovider inside function


#6

which version of the ionic library do you use?


#7

Hi Djerd

I am using V1.1.13.

Ash


#8

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


#9

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


#10

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');
            }
        });
})

#11

Hi Pavei

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

Ashique


#12

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


Problem with Auth state and redirect
#13

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


#14

Hey vmaliko

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

Ashique


#15

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


#16

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


#17

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

VM.


#18

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?


#19

Hi @sjerd


#20

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