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..a3e5f22a 100644 --- a/assets/scripts/app/auth.coffee +++ b/assets/scripts/app/auth.coffee @@ -1,32 +1,31 @@ -@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) + @sendToApp('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}" $('').hide().appendTo('body').attr('src', url) autoSignIn: (data) -> @@ -60,33 +59,34 @@ @storeData(data, Travis.sessionStorage) @storeData(data, Travis.storage) unless @userDataFrom(Travis.storage) user = @loadUser(data.user) - # TODO: we should not use __container__ directly, how to do it better? - # A good answer seems to do auth in context of controller. - Travis.__container__.lookup('controller:currentUser').set('content', user) + @set('currentUser', user) @set('state', 'signed-in') Travis.trigger('user:signed_in', data.user) - if controller = Travis.__container__.lookup('controller:currentUser') - Ember.run.next => - try - controller.send('afterSignIn') - catch e - throw e unless e =~ /There are no active handlers/ || e =~ /Can't trigger action "afterSignIn/ - @refreshUserData(data.user) + @sendToApp('afterSignIn') refreshUserData: (user) -> Travis.ajax.get "/users/#{user.id}", (data) => Travis.loadOrMerge(Travis.User, data.user) # if user is still signed in, update saved data - if @signedIn() + if @get('signedIn') data.user.token = user.token @storeData(data, Travis.sessionStorage) @storeData(data, Travis.storage) , (data, status, xhr) => @signOut() if xhr.status == 401 - signedIn: -> + signedIn: (-> @get('state') == 'signed-in' + ).property('state') + + signedOut: (-> + @get('state') == 'signed-out' + ).property('state') + + signingIn: (-> + @get('state') == 'signing-in' + ).property('state') storeData: (data, storage) -> storage.setItem('travis.token', data.token) if data.token @@ -101,10 +101,37 @@ receiveMessage: (event) -> if event.origin == @expectedOrigin() if event.data == 'redirect' - window.location = "#{@endpoint}/auth/handshake?redirect_uri=#{location}" + window.location = "#{@get('endpoint')}/auth/handshake?redirect_uri=#{location}" else if event.data.user? event.data.user.token = event.data.travis_token if event.data.travis_token @setData(event.data) expectedOrigin: -> - if @endpoint[0] == '/' then @receivingEnd else @endpoint + 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", + + initialize: (container, application) -> + application.register 'auth:main', Auth + + application.inject('route', 'auth', 'auth:main') + application.inject('controller', 'auth', 'auth:main') + application.inject('application', 'auth', 'auth:main') diff --git a/assets/scripts/app/controllers.coffee b/assets/scripts/app/controllers.coffee index 14b33440..a6adbfc0 100644 --- a/assets/scripts/app/controllers.coffee +++ b/assets/scripts/app/controllers.coffee @@ -38,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' @@ -61,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' @@ -71,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' diff --git a/assets/scripts/app/controllers/auth.coffee b/assets/scripts/app/controllers/auth.coffee new file mode 100644 index 00000000..cfaac6d0 --- /dev/null +++ b/assets/scripts/app/controllers/auth.coffee @@ -0,0 +1 @@ +Travis.AuthController = Travis.Controller.extend() diff --git a/assets/scripts/app/controllers/current_user.coffee b/assets/scripts/app/controllers/current_user.coffee index 7a29ca81..affccf9c 100644 --- a/assets/scripts/app/controllers/current_user.coffee +++ b/assets/scripts/app/controllers/current_user.coffee @@ -8,8 +8,12 @@ Travis.CurrentUserController = Em.ObjectController.extend sync: -> @get('content').sync() + content: (-> + @get('auth.currentUser') + ).property('auth.currentUser') + syncingDidChange: (-> if (user = @get('content')) && user.get('isSyncing') && !user.get('syncedAt') Ember.run.scheduleOnce 'routerTransitions', this, -> @container.lookup('router:main').send('renderFirstSync') - ).observes('isSyncing', 'content') \ No newline at end of file + ).observes('isSyncing', 'content') diff --git a/assets/scripts/app/routes.coffee b/assets/scripts/app/routes.coffee index 6ed126ba..af908324 100644 --- a/assets/scripts/app/routes.coffee +++ b/assets/scripts/app/routes.coffee @@ -12,10 +12,10 @@ Travis.Route = Ember.Route.extend @transitionTo 'first_sync' beforeModel: (transition) -> - Travis.autoSignIn() unless @signedIn() + @auth.autoSignIn() unless @signedIn() if !@signedIn() && @get('needsAuth') - Travis.auth.set('afterSignInTransition', transition) + @auth.set('afterSignInTransition', transition) Ember.RSVP.reject("needs-auth") else @_super.apply(this, arguments) @@ -23,19 +23,6 @@ Travis.Route = Ember.Route.extend 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: redirectToGettingStarted: -> @@ -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 @@ -56,8 +43,8 @@ Travis.ApplicationRoute = Travis.Route.extend @renderFirstSync() afterSignIn: -> - if transition = Travis.auth.get('afterSignInTransition') - Travis.auth.set('afterSignInTransition', null) + if transition = @auth.get('afterSignInTransition') + @auth.set('afterSignInTransition', null) transition.retry() afterSignOut: -> diff --git a/assets/scripts/app/templates/auth/signin.hbs b/assets/scripts/app/templates/auth/signin.hbs index 1a83cc2e..6dcef908 100644 --- a/assets/scripts/app/templates/auth/signin.hbs +++ b/assets/scripts/app/templates/auth/signin.hbs @@ -6,8 +6,8 @@
-
- {{#if Travis.signingIn}}
+
+ {{#if auth.signingIn}}
Signing in...
{{else}}
Sign in with GitHub
diff --git a/assets/scripts/app/templates/layouts/top.hbs b/assets/scripts/app/templates/layouts/top.hbs
index 69fd6a62..8090f0eb 100644
--- a/assets/scripts/app/templates/layouts/top.hbs
+++ b/assets/scripts/app/templates/layouts/top.hbs
@@ -31,13 +31,13 @@
- {{#if signedOut}}
- Sign in with GitHub
+ {{#if auth.signedOut}}
+ Sign in with GitHub
{{/if}}
- {{#if signedIn}}
+ {{#if auth.signedIn}}
{{#link-to "profile" class="signed-in"}}{{userName}}{{/link-to}}
{{/if}}
- {{#if signingIn}}
+ {{#if auth.signingIn}}
Signing In
{{/if}}