Bring back afterSignIn and afterSignIn as actions sent to controller

It seems that there is more use cases to cover with this behaviour, for
example when someone enters /auth directly and logs in from there.
This commit is contained in:
Piotr Sarnacki 2014-05-28 08:46:24 +02:00
parent 9dd198ed75
commit 3b1cd4f5c4
4 changed files with 44 additions and 23 deletions

View File

@ -18,8 +18,7 @@ Auth = Ember.Object.extend
if user = @get('currentUser')
user.unload()
@set('currentUser', null)
if hooksTarget = @get('hooksTarget')
hooksTarget.afterSignOut()
@sendToApp('afterSignOut')
signIn: (data) ->
if data
@ -64,9 +63,7 @@ Auth = Ember.Object.extend
@set('state', 'signed-in')
Travis.trigger('user:signed_in', data.user)
# TODO: I would like to get rid of this dependency in the future
if hooksTarget = @get('hooksTarget')
hooksTarget.afterSignIn()
@sendToApp('afterSignIn')
refreshUserData: (user) ->
Travis.ajax.get "/users/#{user.id}", (data) =>
@ -113,6 +110,21 @@ Auth = Ember.Object.extend
endpoint = @get('endpoint')
if endpoint[0] == '/' then @receivingEnd else endpoint
sendToApp: (name) ->
# TODO: this is an ugly solution, we need to do one of 2 things:
# * find a way to check if we can already send an event to remove try/catch
# * remove afterSignIn and afterSignOut events by replacing them in a more
# straightforward code - we can do what's needed on a routes/controller level
# as a direct response to either manual sign in or autoSignIn (right now
# we treat both cases behave the same in terms of sent events which I think
# makes it more complicated than it should be).
controller = @container.lookup('controller:auth')
try
controller.send(name)
catch error
unless error.message =~ /Can't trigger action/
throw error
Ember.onLoad 'Ember.Application', (Application) ->
Application.initializer
name: "auth",

View File

@ -13,6 +13,18 @@ Travis.TopController = Em.Controller.extend
"#{location.protocol}//www.gravatar.com/avatar/#{@get('user.gravatarId')}?s=48&d=mm"
).property('user.gravatarId')
signedIn: (->
Travis.get('authState') == 'signed-in'
).property('Travis.authState')
signedOut: (->
Travis.get('authState') == 'signed-out'
).property('Travis.authState')
signingIn: (->
Travis.get('authState') == 'signing-in'
).property('Travis.authState')
Travis.ApplicationController = Em.Controller.extend
templateName: 'layouts/home'
@ -26,7 +38,7 @@ Travis.StatsLayoutController = Em.Controller.extend()
Travis.ProfileLayoutController = Em.Controller.extend()
Travis.AuthLayoutController = Em.Controller.extend()
Travis.AccountProfileController = Em.Controller.extend
Travis.ProfileInfoController = Em.Controller.extend
needs: ['currentUser', 'repos']
userBinding: 'controllers.currentUser'
@ -49,6 +61,7 @@ Travis.RepoSettingsController = Em.ObjectController.extend
Travis.flash(error: 'There was an error while saving settings. Please try again.')
require 'controllers/accounts'
require 'controllers/auth'
require 'controllers/build'
require 'controllers/builds'
require 'controllers/flash'
@ -59,6 +72,5 @@ require 'controllers/repos'
require 'controllers/repo'
require 'controllers/stats'
require 'controllers/current_user'
require 'controllers/account_index'
require 'controllers/request'
require 'controllers/requests'

View File

@ -0,0 +1 @@
Travis.AuthController = Travis.Controller.extend()

View File

@ -24,19 +24,6 @@ Travis.Route = Ember.Route.extend
@controllerFor('currentUser').get('content')
Travis.ApplicationRoute = Travis.Route.extend
init: ->
@_super.apply this, arguments
@auth.set('hooksTarget', this)
afterSignIn: ->
if transition = @auth.get('afterSignInTransition')
@auth.set('afterSignInTransition', null)
transition.retry()
afterSignOut: ->
@transitionTo('index.current')
actions:
redirectToGettingStarted: ->
# do nothing, we handle it only in index path
@ -46,7 +33,7 @@ Travis.ApplicationRoute = Travis.Route.extend
error: (error) ->
if error == 'needs-auth'
authController = @container.lookup('controller:auth') || @generateController('auth')
authController = @container.lookup('controller:auth')
authController.set('redirected', true)
@transitionTo('auth')
else
@ -55,6 +42,14 @@ Travis.ApplicationRoute = Travis.Route.extend
renderFirstSync: ->
@renderFirstSync()
afterSignIn: ->
if transition = @auth.get('afterSignInTransition')
@auth.set('afterSignInTransition', null)
transition.retry()
afterSignOut: ->
@transitionTo('index.current')
Travis.Router.map ->
@resource 'index', path: '/', ->
@resource 'getting_started'
@ -394,9 +389,10 @@ Travis.AuthRoute = Travis.Route.extend
deactivate: ->
@controllerFor('auth').set('redirected', false)
redirect: ->
if @auth.get('signedIn')
actions:
afterSignIn: ->
@transitionTo('index.current')
return true
Travis.RepoSettingsRoute = Travis.Route.extend
setupController: (controller, model) ->