From 3544d523d785a29c67c6ff8e5af16525458cf751 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 21 May 2014 14:24:44 +0200 Subject: [PATCH] Refactor auth code * don't use __container__ * register it as a factory on container and inject into routes and controller * avoid afterSignIn errors by checking on hooksTarget, ApplicationRoute should register itself as a hooksTarget * keep user info on auth --- assets/scripts/app/app.coffee | 20 ------ assets/scripts/app/auth.coffee | 63 ++++++++++++------- assets/scripts/app/controllers.coffee | 12 ---- .../app/controllers/current_user.coffee | 6 +- assets/scripts/app/routes.coffee | 29 +++++---- assets/scripts/app/templates/auth/signin.hbs | 4 +- assets/scripts/app/templates/layouts/top.hbs | 10 +-- assets/scripts/app/views/signin.coffee | 4 -- assets/scripts/app/views/top.coffee | 4 +- assets/scripts/spec/spec_helper.coffee | 8 ++- 10 files changed, 76 insertions(+), 84 deletions(-) diff --git a/assets/scripts/app/app.coffee b/assets/scripts/app/app.coffee index 20002ac0..c4608eb1 100644 --- a/assets/scripts/app/app.coffee +++ b/assets/scripts/app/app.coffee @@ -1,8 +1,6 @@ unless window.TravisApplication window.TravisApplication = Em.Application.extend(Ember.Evented, LOG_TRANSITIONS: true, - authState: Ember.computed.alias('auth.state') - signedIn: (-> @get('authState') == 'signed-in' ).property('authState') mappings: (-> broadcasts: Travis.Broadcast @@ -40,8 +38,6 @@ unless window.TravisApplication @tailing = new Travis.Tailing($(window), '#tail', '#log') @toTop = new Travis.ToTop($(window), '.to-top', '#log-container') - @set('auth', Travis.Auth.create(app: this, endpoint: Travis.config.api_endpoint)) - reset: -> @_super.apply(this, arguments) @get('modelClasses').forEach (klass) -> @@ -54,22 +50,6 @@ unless window.TravisApplication flash: (options) -> Travis.lookup('controller:flash').loadFlashes([options]) - storeAfterSignInPath: (path) -> - @get('auth').storeAfterSignInPath(path) - - autoSignIn: (path) -> - @get('auth').autoSignIn() - - signIn: -> - @get('auth').signIn() - - signOut: -> - @get('auth').signOut() - - signingIn: (-> - Travis.get('authState') == 'signing-in' - ).property('authState') - receive: (event, data) -> [name, type] = event.split(':') diff --git a/assets/scripts/app/auth.coffee b/assets/scripts/app/auth.coffee index 92ecbb25..364bc34b 100644 --- a/assets/scripts/app/auth.coffee +++ b/assets/scripts/app/auth.coffee @@ -1,32 +1,32 @@ -@Travis.Auth = Ember.Object.extend +Auth = Ember.Object.extend state: "signed-out" receivingEnd: "#{location.protocol}//#{location.host}" init: -> window.addEventListener('message', (e) => @receiveMessage(e)) + endpoint: (-> + @container.lookup('application:main').config.api_endpoint + ).property(), + signOut: -> Travis.storage.removeItem('travis.user') Travis.storage.removeItem('travis.token') Travis.sessionStorage.clear() @set('state', 'signed-out') @set('user', undefined) - if user = Travis.__container__.lookup('controller:currentUser').get('content') + if user = @get('currentUser') user.unload() - Travis.__container__.lookup('controller:currentUser').set('content', null) - if controller = Travis.__container__.lookup('controller:currentUser') - try - controller.send('afterSignOut') - catch e - throw e unless e.message =~ /There are no active handlers/ - + @set('currentUser', null) + if hooksTarget = @get('hooksTarget') + hooksTarget.afterSignOut() signIn: (data) -> if data @autoSignIn(data) else @set('state', 'signing-in') - url = "#{@endpoint}/auth/post_message?origin=#{@receivingEnd}" + url = "#{@get('endpoint')}/auth/post_message?origin=#{@receivingEnd}" $('