0

I have an issue with a SPA app written in angularJS. The application consist of thre views - login view, main view and log out view.

  • myapp/#/login
  • myapp/#/main
  • myapp/#/logout

my route provider:

function Router($routeProvider) {
    $routeProvider
        .when('/login', {
            templateUrl: 'app/components/login/login.tmpl.html',
            controller: 'login.ctrl',
            controllerAs: 'vm'
        })
        .when('/main', {
            templateUrl: 'app/components/dashboard/main.tmpl.html',
            controller: 'dashboard.ctrl',
            controllerAs: 'vm'
        })
        .when('/logout', {
            templateUrl: 'app/components/logout/logout.tmpl.html',
            controller: 'logout.ctrl',
            controllerAs: 'vm'
        })
        .otherwise({
            redirectTo: '/login'
        });
}

Use case: 1) user logs in. 2) user sees the content in the main view 3) user logs out. 4) user is redirected to the logout view for confirmation 5) user is redirected to the login view

If I press the back browser button after step 5 I am redirected to the main view. Is there a way to change the behavior of the back button when I am in the login view? I believe it is something simple and I apologize if the question is duplicated. Thank you for your time and responses!

3

1 Answer 1

1

Please see working example: http://plnkr.co/edit/46O0znC5HFDE4cYXSm5h?p=preview

Stored data in cookies in login function as follows,

$cookies.userinfo = {
    'id': 1,
    'name': 'pqr'
};

And on logout remove that data -

delete $cookies.userinfo;

then check for 'angular.isDefined($cookies.userinfo)' (userinfo is cookie name which given at the time of storing data in it) if find then redirect it to your page which you want to see after login. i.e

app.run(function ($cookies) {
      $rootScope.$on("$routeChangeStart", function () {
        if (angular.isDefined($cookies.userinfo)) {
            $location.path("/pathname");
        }

      });
});
Sign up to request clarification or add additional context in comments.

5 Comments

how can I get the cookie for the specific user :?
Then use $cookies and apply the same. For 1st question answer is - Store user information while user is login(i.e in login controller using put method of cookies) in cookies and use that information in above code.
here is my suggestion: function login(user) { $cookies.put('1', 'userinfo'); } But for some reason it produces an error.
Please check I have updated my answer and also created example wor you. Try back button click on plunker I have created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.