Navigation redirection example

Hey!

I’m working on a project that has private and public user profiles. It’s something like a twitter client in which you have a list of cards for content published by users, each containing a header with the user’s avatar, name, and so on. Clicking on the header you go to that user’s profile page at /profile/:uuid but if the publication is yours (the author is the logged in user) you end on your own account page at /account instead.

First I was solving this by duplicating the link part in the DOM of the header with ng-if directives but there was a lot of repetition and I was feeling that the controller or the view weren’t the right places to handle this responsibility.

Server side, you’d solve this easily by implementing a request filter, but with angular-ui-router and ionic it is not so straightforward to achieve.

If you do a conditional location change in the user profile controller you break navigation history because you won’t be able to go further back in the history beyond that point (you keep being forwarded to the account).

Therefore, the only solution (that I could think of) is to config $urlRouterProvider to handle /profile/:uuid routes and change the navigation destination to one or the other view, before anything gets recorded in the view history.

You can check a live example in this codepen: http://codepen.io/ggalmazor/pen/jvJkh

It would be great to have some feedback from you on this topic.

Thanks!

1 Like

Looks nice, great job!