Fix authentication

This commit is contained in:
Piotr Sarnacki 2013-02-26 07:05:40 +01:00
parent ba1eecec45
commit 211b2723f9
3 changed files with 48 additions and 4 deletions

View File

@ -13,6 +13,8 @@
Travis.setLocale Travis.default_locale
@set('state', 'signed-out')
@set('user', undefined)
Travis.__container__.lookup('controller:currentUser').set('content', null)
Travis.__container__.lookup('router:main').send('afterSignOut')
signIn: ->
@set('state', 'signing-in')
@ -51,12 +53,13 @@
@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('state', 'signed-in')
Travis.setLocale(data.user.locale || Travis.default_locale)
Travis.trigger('user:signed_in', data.user)
#@get('app.router').send('afterSignIn', @readAfterSignInPath())
Travis.__container__.lookup('router:main').send('afterSignIn', @readAfterSignInPath())
storeData: (data, storage) ->
storage.setItem('travis.token', data.token)

View File

@ -8,6 +8,36 @@ Ember.Router.reopen
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
events:
afterSignIn: (path) ->
@routeTo(path)
afterSignOut: ->
@routeTo('/')
routeTo: (path) ->
return unless path
@router.handleURL(path)
@router.location.setURL(path)
signedIn: ->
@controllerFor('currentUser').get('content')
redirect: ->
if @get('needsAuth')
@authorize(@router.location.getURL())
else
@_super.apply this, arguments
Travis.autoSignIn() unless @signedIn()
authorize: (path) ->
if !@signedIn()
Travis.storeAfterSignInPath(path)
@transitionTo('auth')
Travis.Router.map ->
@resource 'index', path: '/', ->
@route 'current', path: '/'
@ -20,6 +50,7 @@ Travis.Router.map ->
@resource 'branches', path: '/branches'
@route 'stats', path: '/stats'
@route 'auth', path: '/auth'
@resource 'profile', path: '/profile', ->
@route 'index', path: '/'
@ -150,6 +181,8 @@ Travis.StatsRoute = Ember.Route.extend
@container.lookup('controller:application').connectLayout('simple')
Travis.ProfileRoute = Ember.Route.extend
needsAuth: true
setupController: ->
@container.lookup('controller:application').connectLayout('profile')
@container.lookup('controller:accounts').set('content', Travis.Account.find())
@ -167,7 +200,7 @@ Travis.ProfileIndexRoute = Ember.Route.extend
@container.lookup('controller:profile').activate 'hooks'
renderTemplate: ->
@render 'hooks', outlet: 'pane', into: 'profile'
@render 'hooks', outlet: 'pane', into: 'profile', controller: 'profile'
Travis.AccountRoute = Ember.Route.extend
setupController: (controller, account) ->
@ -215,3 +248,13 @@ Travis.AccountProfileRoute = Ember.Route.extend
renderTemplate: ->
@render 'user', outlet: 'pane', into: 'profile'
Travis.AuthRoute = Ember.Route.extend
renderTemplate: ->
$('body').attr('id', 'auth')
@render 'top', outlet: 'top'
@render 'auth.signin'
setupController: ->
@container.lookup('controller:application').connectLayout('simple')

View File

@ -43,7 +43,6 @@ window.Travis = Em.Application.extend(Ember.Evented,
signOut: ->
@get('auth').signOut()
#@get('router').send('afterSignOut')
receive: ->
@store.receive.apply(@store, arguments)
@ -69,7 +68,6 @@ window.Travis = Em.Application.extend(Ember.Evented,
location.href = location.href.replace('#!/', '') if location.hash.slice(0, 2) == '#!'
I18n.fallbacks = true
@setLocale 'locale', @get('defaultLocale')
@autoSignIn() unless @get('signedIn')
).create()
Travis.deferReadiness()