witoldsz / angular-http-auth

MIT License
2.38k stars 417 forks source link

Take user to requested page after successful login #46

Closed xmlking closed 10 years ago

xmlking commented 10 years ago

Currently I am loading fixed route after successful log-in. Is there a way to load requested route after user successful logged in?


    $scope.$on('event:auth-loginConfirmed', function() {
        growl.addSuccessMessage("You are Logged in!");
        $state.go('dashboard');
    });
    $scope.$on('event:auth-loginCancelled', function() {
        growl.addWarnMessage("login Cancelled!");
        $state.go('home');
    });
witoldsz commented 10 years ago

Hi, my advise is: in general, do not make such the actions on login request or login success, because it actually defeat the purpose of the solution we solve using this module. When you do this, you ruin user work flow.

I guess the valid case when you are in some... default place of an application. In such a controller, which does nothing but waits for login confirm event, you can use $location or something else to move user somewhere else.

I don't know what growl.... or $state.go(...) is, but your code snippet it looks correct, doesn't it?

witoldsz commented 10 years ago

One more thing: there is a problem with the approach you provide: if we have such a code in controller and user enters the page controlled by it while they are logged in already, no action is going to happen.

I would suggest writing a service for the specific purpose: providing a default route for current user. I would return a promise which would resolve once we know who the user is. Resolved promises are great, you can keep using them and they always work the same: applying a "then" action on it will trigger even though it was resolved already.

Notice: user can be logged in before you start your application (scenario: load app, login, reload page), so you have to check some state instead of waiting for "logged confirmed" event, which can never fire.

xmlking commented 10 years ago

witoldsz , first I want to thank you for contribution of this grate module.

I am using angular-growl , angular-ui : UI-Router module along with http-auth-interceptor.
I posted my code @ https://gist.github.com/xmlking/7253831 to give some context and better explain the solution I am looking for. I think I should also post this request to UI-Router forum as this also depends on it.

I have a partial header [header.html] which is included in all pages where I am referencing to AuthCtrl to get the scoped models. login dialog will popup when user click login link from header or when requested view resolver receive 401 from backend.

To enforce security, I am redirecting user to home page [anonymous user view] when [event:auth-loginCancelled] received . When server side session expired due to timeout and if same user or some one else click secured link, we don't want to show current view left on the screen.

My question is, If I don't do $state.go('dashboard') after event:auth-loginConfirmed , will the user automatically redirected to the view that is requested before 401 error fired?

witoldsz commented 10 years ago

I can't really help you more than what I said already. It does not seem to be angular-http-auth issue. If you ask if the module redirects anywhere - no, it does not, why should it... Check the source code, it's actually nothing more than few methods around $http interceptor.

alonisser commented 10 years ago

@xmlking No, It doesn't redirect automatically but there is a solution for that (been in that path too). you can listen to angular.ui $onChangeStart event and "record" each event arguments(t needing only toState, toParams) and "remove" the "recording" on $stateChangeSuccess. something like:

$scope.$on('$onChangeStart',function(ev,toState,toParams, fromState,fromParams){
   $scope.recordedState = {toState:toState, toParams:toParams}
})
$scope.$on('$onChangeSuccess', function(){
   $scope.recordedState = false;
})
then you can listen to 'event:auth-loginConfirmed' and if there is a state change that didn't finish, continue it with something like:

$scope.$on('event:auth-loginConfirmed', function(){
   if($scope.recordedState){
      $state.transitionTo($scope.recordedState.toState.name, $scope.recordedState.toParams) //lower level of state.go. needed here for simplicity of parsing the recorded state objects. I don't remember the exact object names. but something around this..
    }
})

profit!
xmlking commented 10 years ago

@alonisser thanks for the cool solution. This makes the user experience more natural with automatic redirection to target view after successful login.

alonisser commented 10 years ago

Thanks! i'll probably post this somewhere, since I believe we two are not the only people that bumped into this problem

Twitter:@alonisser https://twitter.com/alonisser LinkedIn Profile http://www.linkedin.com/in/alonisser Facebook https://www.facebook.com/alonisser _Tech blog:_4p-tech.co.il/blog _Personal Blog:_degeladom.wordpress.com Tel:972-54-6734469

On Sat, Jan 4, 2014 at 8:50 AM, Sumo notifications@github.com wrote:

@alonisser https://github.com/alonisser thanks for the cool solution. This makes the user experience more natural with automatic redirection to target view after successful login.

— Reply to this email directly or view it on GitHubhttps://github.com/witoldsz/angular-http-auth/issues/46#issuecomment-31573042 .