# Back button problem: Clear the login view from history

**URL:** <https://forum.ionicframework.com/t/back-button-problem-clear-the-login-view-from-history/11492>\
**Category:** ionic-v1\
**Created:** [October 21, 2014, 8:15am UTC](https://forum.ionicframework.com/t/back-button-problem-clear-the-login-view-from-history/11492 "2014-10-21T08:15:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![haibtdt](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@haibtdt](https://forum.ionicframework.com/u/haibtdt)\
**Post date:** [October 21, 2014, 8:15am UTC](https://forum.ionicframework.com/t/back-button-problem-clear-the-login-view-from-history/11492/1 "2014-10-21T08:15:36Z")

</div>

After strungling a long time, i still could not prevent the user from going to login page once they are already logged in

given here my desired state flow:  
login → dashboard —\> … —\> account (sign out) —\> login

//in loginViewController:  
$location.path(‘/dashboard’).replace();  
$state.go(“dashboard”)  
//does not work

By pressing the physical back button serveral times, users can go to login page with out signing out from account page. I also don’t want the users to be stuck by redirection. It should remove the login view from the history stack.

Continuing the discussion from [Remove page from history, so “back” will work properly](http://forum.ionicframework.com/t/remove-page-from-history-so-back-will-work-properly/11378):

> [@Remove page from history, so "back" will work properly](https://forum.ionicframework.com/t/remove-page-from-history-so-back-will-work-properly/11378/1):
>
> I have my app that you need to login to get in to the other pages.
> 
> so the first page is “login” and it checks if you are already logged, if so you will be redirected to the main page app, if not it will show you the login page.
> 
> now the problem is when the user is inside the logged page area, and he clicks back he will get to the “login” page and than redirected back to the main page, as he is logged in already.
> 
> So he is stuck in an infinite loop.
> 
> how can I remove the login page from the history.
> 
> just like in android “android remove activity from history stack”

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [October 21, 2014, 3:15pm UTC](https://forum.ionicframework.com/t/back-button-problem-clear-the-login-view-from-history/11492/2 "2014-10-21T15:15:59Z")

</div>

Hi @haibtdt,

In your loginViewController, right before the redirect to your dashboard, add the following code:

```
$ionicViewService.nextViewOptions({
    disableAnimate: true,
    disableBack: true
});

```

That will clear the current view (login) from the history. When the user is on the dashboard, and they hit the back button, the app will close since the dashboard is now the only item in the view history.

Add the same code to your account sign out before the redirect to the login view. This way after the user is signed out, hitting the back button from the log in screen will only close the app.

Let me know if any of this doesn’t make sense.

Regards,

---

<div class="post-metadata">

**Author:** ![haibtdt](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@haibtdt](https://forum.ionicframework.com/u/haibtdt)\
**Post date:** [October 22, 2014, 2:16am UTC](https://forum.ionicframework.com/t/back-button-problem-clear-the-login-view-from-history/11492/3 "2014-10-22T02:16:38Z")

</div>

It works great. Thank you very much for a very clear instruction. Btw, where can I learn more about that service? And is that the only way?

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [October 22, 2014, 1:46pm UTC](https://forum.ionicframework.com/t/back-button-problem-clear-the-login-view-from-history/11492/4 "2014-10-22T13:46:40Z")

</div>

You can add a nav-clear to your UI element, but I find it better to do it in code.

Unfortunately, I don’t have more documentation on the subject.

---

<div class="post-metadata">

**Author:** ![munene](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@munene](https://forum.ionicframework.com/u/munene)\
**Post date:** [July 13, 2015, 12:51pm UTC](https://forum.ionicframework.com/t/back-button-problem-clear-the-login-view-from-history/11492/5 "2015-07-13T12:51:47Z")

</div>

**UPDATE:** So apparently $ionicViewService.nextViewOptions is deprecated so just replace it with $ionicHistory.nextViewOptions

---

<div class="post-metadata">

**Author:** ![murtuzamac](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@murtuzamac](https://forum.ionicframework.com/u/murtuzamac)\
**Post date:** [August 8, 2016, 9:43am UTC](https://forum.ionicframework.com/t/back-button-problem-clear-the-login-view-from-history/11492/6 "2016-08-08T09:43:57Z")

</div>

Hi. I tried with putting the above code before redirecting to dashboard and it is working as expected. But once I am on dashboard, click log out from side menu, and redirect to login view again, the back button does nothing. Could you please suggest how to make the back button work with logging out process? Thanks.
