I'm trying to write some logic where if a user is logged in (and hence a currentUser
variable is defined, more details in edit section below), he should not be able to access the root URL of the application, which is the log-in page. I'm trying to achieve this using a redirect to the users.show
page before/as soon as the page loads, but I can't seem to figure out how to do it.
Reading ember documentation, you can do redirects from the route or the controller. I've attempted both and neither have worked. In the route:
App.ApplicationRoute = Ember.Route.extend
redirect: ->
currentUser = @controllerFor('Application').get('currentUser') # this works
currentPath = @controllerFor('Application').get('currentPath') # this returns undefined
if currentUser && currentPath == '/'
@transitionTo('users.show', currentUser)
The problem here is that I can't read the currentPath for some reason, even though I can access it from within App.ApplicationController
.
In the controller, I've tried:
App.ApplicationController = Ember.Controller.extend
init: -> # never gets called
if @get('currentUser') && @get('currentPath') == '/'
@transitionToRoute('users.show', @get('currentUser'))
Except the init
never gets called when loading the page, and I can't figure out from the documentation which hook gets called on page load.
If there are any other problems with the code, feel free to point them out. How do I fix my code in order to achieve the redirect?
EDIT: it was suggested for the route implementation to change 'application'
to lowercase in @controllerFor('application')
. Tried this and it returns the error
Assertion failed: The controller named 'application' could not be found. Make sure that this route exists and has already been entered at least once. If you are accessing a controller not associated with a route, make sure the controller class is explicitly defined.
Not sure what's going on, as doing a @controllerFor
for other routes in all lowercase works, but not 'application'
.
The Current User variable is obtained via an injection in an ember initializer. I'm using Rails and Devise on my backend, and the way I get currentUser
into ember essentially more or less follows this tutorial. The general flow is Rails -> meta tag -> initializer -> injection.
SOLUTION: Inspired by the discussion following the accepted answer, I ended up doing it in ApplicationRoute
like so:
App.ApplicationRoute = Ember.Route.extend
beforeModel: (transition) ->
currentUser = @controllerFor('currentUser')
if currentUser && transition.targetName == 'index'
@transitionTo('users.show', currentUser)
Application
. I don't know about user created routes, but theapplication
route is definitely all lowercase. – GJK@controllerFor('application')
withinApp.ApplicationRoute
... – xph