     url = url.replace(/#.*?$/, '')
     @_super(url)
 
-# TODO: don't reopen Ember.Route to add events, there should be
-#       a better way (like "parent" resource for everything inside map)
-Ember.Route.reopen
-  _actions:
+Travis.Route = Ember.Route.extend
+  afterSignIn: ->
+    if transition = Travis.auth.get('afterSignInTransition')
+      Travis.auth.set('afterSignInTransition', null)
+      transition.retry()
+    else
+      @transitionTo('index.current') if @constructor == Travis.AuthRoute || @constructor.superclass == Travis.AuthRoute
+
+  afterSignOut: ->
+    @transitionTo('index.current')
+
+  renderFirstSync: ->
+    @transitionTo 'first_sync'
+
+  beforeModel: (transition) ->
+    Travis.autoSignIn() unless @signedIn()
+
+    if !@signedIn() && @get('needsAuth')
+      Travis.auth.set('afterSignInTransition', transition)
+      Ember.RSVP.reject("needs-auth")
+    else
+      @_super.apply(this, arguments)
+
+  signedIn: ->
+    @controllerFor('currentUser').get('content')
+
+  redirect: ->
+    Travis.autoSignIn() unless @signedIn()
+
+    if @get('needsAuth')
+      @authorize(@router.location.getURL())
+    else
+      @_super.apply this, arguments
+
+  authorize: (path) ->
+    if !@signedIn()
+      Travis.storeAfterSignInPath(path)
+      @transitionTo('auth')
+
+Travis.ApplicationRoute = Travis.Route.extend
+  actions:
     renderDefaultTemplate: ->
       @renderDefaultTemplate() if @renderDefaultTemplate
 

Travis.PullRequestsRoute = Travis.AbstractBuildsRoute.extend(contentType: 'pull_requests')
Travis.BranchesRoute = Travis.AbstractBuildsRoute.extend(contentType: 'branches')

Travis.BuildRoute = Ember.Route.extend
  renderTemplate: ->
    @render 'build', outlet: 'pane', into: 'repo'


    @render 'flash', outlet: 'flash'
    @render 'profile'

Travis.ProfileIndexRoute = Travis.Route.extend
  setupController: ->
    @container.lookup('controller:profile').activate 'hooks'

  renderTemplate: ->
    @render 'hooks', outlet: 'pane', into: 'profile', controller: 'profile'

Travis.AccountRoute = Travis.Route.extend
  setupController: (controller, account) ->
    profileController = @container.lookup('controller:profile')
    profileController.activate 'hooks'
