refactor app/auth stuff
This commit is contained in:
parent
e647a7527d
commit
d1255d9230
|
@ -1,4 +1,5 @@
|
||||||
require 'travis' # hrm.
|
require 'travis' # hrm.
|
||||||
|
require 'auth'
|
||||||
require 'controllers'
|
require 'controllers'
|
||||||
require 'helpers'
|
require 'helpers'
|
||||||
require 'models'
|
require 'models'
|
||||||
|
@ -11,7 +12,6 @@ require 'views'
|
||||||
|
|
||||||
require 'config/locales'
|
require 'config/locales'
|
||||||
require 'data/sponsors'
|
require 'data/sponsors'
|
||||||
require 'travis/auth'
|
|
||||||
|
|
||||||
# $.mockjaxSettings.log = false
|
# $.mockjaxSettings.log = false
|
||||||
# Ember.LOG_BINDINGS = true
|
# Ember.LOG_BINDINGS = true
|
||||||
|
@ -20,6 +20,10 @@ require 'travis/auth'
|
||||||
|
|
||||||
Travis.reopen
|
Travis.reopen
|
||||||
App: Em.Application.extend
|
App: Em.Application.extend
|
||||||
|
currentUserBinding: 'auth.user'
|
||||||
|
accessTokenBinding: 'auth.user.token'
|
||||||
|
authStateBinding: 'auth.state'
|
||||||
|
|
||||||
init: ->
|
init: ->
|
||||||
@_super()
|
@_super()
|
||||||
@connect()
|
@connect()
|
||||||
|
@ -31,36 +35,13 @@ Travis.reopen
|
||||||
@pusher = new Travis.Pusher()
|
@pusher = new Travis.Pusher()
|
||||||
@tailing = new Travis.Tailing()
|
@tailing = new Travis.Tailing()
|
||||||
|
|
||||||
@loadUser()
|
@set('auth', Travis.Auth.create(store: @store, endpoint: Travis.config.api_endpoint))
|
||||||
|
|
||||||
loadUser: ->
|
|
||||||
user = sessionStorage?.getItem("travisUser")
|
|
||||||
if user
|
|
||||||
@setCurrentUser JSON.parse(user)
|
|
||||||
else if localStorage?.getItem("travisTrySignIn")
|
|
||||||
Travis.Auth.trySignIn()
|
|
||||||
|
|
||||||
signIn: ->
|
signIn: ->
|
||||||
@set('signingIn', true)
|
@get('auth').signIn()
|
||||||
Travis.Auth.signIn()
|
|
||||||
# TODO: this has to mov, no?
|
|
||||||
@render.apply(this, @get('returnTo') || ['home', 'index'])
|
|
||||||
|
|
||||||
signOut: ->
|
signOut: ->
|
||||||
Travis.config.access_token = null
|
@get('auth').signOut()
|
||||||
localStorage?.clear()
|
|
||||||
sessionStorage?.clear()
|
|
||||||
@setCurrentUser()
|
|
||||||
|
|
||||||
setCurrentUser: (data) ->
|
|
||||||
data = JSON.parse(data) if typeof data == 'string'
|
|
||||||
if data
|
|
||||||
localStorage?.setItem("travisTrySignIn", "true")
|
|
||||||
sessionStorage?.setItem("travisUser", JSON.stringify(data))
|
|
||||||
@store.load(Travis.User, data.user)
|
|
||||||
@store.loadMany(Travis.Account, data.accounts)
|
|
||||||
@set('currentUser', if data then Travis.User.find(data.user.id) else undefined)
|
|
||||||
@set('signingIn', false)
|
|
||||||
|
|
||||||
render: (name, action, params) ->
|
render: (name, action, params) ->
|
||||||
layout = @connectLayout(name)
|
layout = @connectLayout(name)
|
||||||
|
|
64
assets/javascripts/app/auth.coffee
Normal file
64
assets/javascripts/app/auth.coffee
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
@Travis.Auth = Ember.Object.extend
|
||||||
|
iframe: $('<iframe id="auth-frame" />').hide()
|
||||||
|
timeout: 5000
|
||||||
|
state: 'signed-out'
|
||||||
|
|
||||||
|
init: ->
|
||||||
|
@iframe.appendTo('body')
|
||||||
|
window.addEventListener('message', (e) => @receiveMessage(e))
|
||||||
|
Ember.run.next(this, @loadUser)
|
||||||
|
|
||||||
|
# if the user is in the session storage, we're using it. if we have a flag
|
||||||
|
# for auto signin then we're trying to sign in.
|
||||||
|
loadUser: ->
|
||||||
|
if user = sessionStorage?.getItem('travis.user')
|
||||||
|
@setUser(user)
|
||||||
|
else if localStorage?.getItem('travis.auto_signin')
|
||||||
|
@trySignIn()
|
||||||
|
|
||||||
|
# try signing in, but check later in case we have a timeout
|
||||||
|
signIn: ->
|
||||||
|
@trySignIn()
|
||||||
|
Ember.run.later(this, @checkSignIn.bind(this), @timeout)
|
||||||
|
|
||||||
|
trySignIn: ->
|
||||||
|
@set('state', 'signing-in')
|
||||||
|
@iframe.attr('src', "#{@endpoint}/auth/post_message")
|
||||||
|
|
||||||
|
checkSignIn: ->
|
||||||
|
@forceSignIn() if @get('state') == 'signing-in'
|
||||||
|
|
||||||
|
forceSignIn: ->
|
||||||
|
localStorage?.setItem('travis.auto_signin', 'true')
|
||||||
|
url = "#{@endpoint}/auth/handshake?redirect_uri=#{location}"
|
||||||
|
window.location = url
|
||||||
|
|
||||||
|
signOut: ->
|
||||||
|
localStorage?.clear()
|
||||||
|
sessionStorage?.clear()
|
||||||
|
@setUser()
|
||||||
|
|
||||||
|
|
||||||
|
setUser: (data) ->
|
||||||
|
data = JSON.parse(data) if typeof data == 'string'
|
||||||
|
user = @storeUser(data) if data
|
||||||
|
@set('state', if user then 'signed-in' else 'signed-out')
|
||||||
|
@set('user', if user then user else undefined)
|
||||||
|
|
||||||
|
storeUser: (data) ->
|
||||||
|
localStorage?.setItem('travis.auto_signin', 'true')
|
||||||
|
sessionStorage?.setItem('travis.user', JSON.stringify(data))
|
||||||
|
data.user.access_token = data.token # TODO why's the access_token not set on the user?
|
||||||
|
@store.load(Travis.User, data.user)
|
||||||
|
@store.loadMany(Travis.Account, data.accounts)
|
||||||
|
Travis.User.find(data.user.id)
|
||||||
|
|
||||||
|
receiveMessage: (event) ->
|
||||||
|
if event.origin == @expectedOrigin()
|
||||||
|
@setUser(event.data)
|
||||||
|
console.log("signed in as #{event.data.user.login}")
|
||||||
|
else
|
||||||
|
console.log("unexpected message #{event.origin}: #{event.data}")
|
||||||
|
|
||||||
|
expectedOrigin: ->
|
||||||
|
if @endpoint[0] == '/' then "#{location.protocol}//#{location.host}" else @endpoint
|
|
@ -30,7 +30,7 @@ jQuery.support.cors = true
|
||||||
|
|
||||||
ajax: (url, method, options) ->
|
ajax: (url, method, options) ->
|
||||||
endpoint = Travis.config.api_endpoint || ''
|
endpoint = Travis.config.api_endpoint || ''
|
||||||
if Travis.config.access_token
|
if access_token = Travis.app.get('accessToken')
|
||||||
options.headers ||= {}
|
options.headers ||= {}
|
||||||
options.headers['Authorization'] ||= "token #{Travis.config.access_token}"
|
options.headers['Authorization'] ||= "token #{access_token}"
|
||||||
@_super("#{endpoint}#{url}", method, $.extend(options, @DEFAULT_OPTIONS))
|
@_super("#{endpoint}#{url}", method, $.extend(options, @DEFAULT_OPTIONS))
|
||||||
|
|
|
@ -17,8 +17,8 @@
|
||||||
</li>
|
</li>
|
||||||
<li {{bindAttr class="view.classProfile"}}>
|
<li {{bindAttr class="view.classProfile"}}>
|
||||||
<p class="handle">
|
<p class="handle">
|
||||||
|
<a class="signed-out" href="#" {{action signIn target="Travis.app"}}>{{t layouts.top.github_login}}</a>
|
||||||
<a class="signed-in" href="#" class="name"><img {{bindAttr src="view.gravatarUrl"}}>{{user.name}}</a>
|
<a class="signed-in" href="#" class="name"><img {{bindAttr src="view.gravatarUrl"}}>{{user.name}}</a>
|
||||||
<a class="sign-in" href="#" {{action signIn target="Travis.app"}}>{{t layouts.top.github_login}}</a>
|
|
||||||
<span class="signing-in">Signing in</span>
|
<span class="signing-in">Signing in</span>
|
||||||
</p>
|
</p>
|
||||||
<ul>
|
<ul>
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
require 'travis/auth'
|
|
||||||
|
|
||||||
@Travis.reopen
|
@Travis.reopen
|
||||||
TopView: Travis.View.extend
|
TopView: Travis.View.extend
|
||||||
templateName: 'layouts/top'
|
templateName: 'layouts/top'
|
||||||
|
@ -23,14 +21,9 @@ require 'travis/auth'
|
||||||
classProfile: (->
|
classProfile: (->
|
||||||
classes = ['profile']
|
classes = ['profile']
|
||||||
classes.push('active') if @get('tab') == 'profile'
|
classes.push('active') if @get('tab') == 'profile'
|
||||||
if Travis.app.get('currentUser')
|
classes.push(Travis.app.get('authState'))
|
||||||
classes.push('signed-in')
|
|
||||||
else if Travis.app.get('signingIn')
|
|
||||||
classes.push('signing-in')
|
|
||||||
else
|
|
||||||
classes.push('sign-in')
|
|
||||||
classes.join(' ')
|
classes.join(' ')
|
||||||
).property('tab', 'Travis.app.currentUser', 'Travis.app.signingIn')
|
).property('tab', 'Travis.app.authState')
|
||||||
|
|
||||||
showProfile: ->
|
showProfile: ->
|
||||||
$('#top .profile ul').show()
|
$('#top .profile ul').show()
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
@Travis.Auth = ->
|
|
||||||
$ =>
|
|
||||||
@iframe.appendTo('body')
|
|
||||||
window.addEventListener "message", (e) => @receiveMessage(e)
|
|
||||||
this
|
|
||||||
|
|
||||||
$.extend Travis.Auth,
|
|
||||||
instance: new Travis.Auth()
|
|
||||||
|
|
||||||
signIn: ->
|
|
||||||
@instance.signIn()
|
|
||||||
|
|
||||||
trySignIn: ->
|
|
||||||
@instance.trySignIn()
|
|
||||||
|
|
||||||
$.extend Travis.Auth.prototype,
|
|
||||||
iframe: $('<iframe id="auth-frame" />').hide()
|
|
||||||
timeout: 10000
|
|
||||||
|
|
||||||
expectedOrigin: ->
|
|
||||||
if Travis.config.api_endpoint[0] == '/'
|
|
||||||
window.location.protocol + "//" + window.location.host
|
|
||||||
else
|
|
||||||
Travis.config.api_endpoint
|
|
||||||
|
|
||||||
receiveMessage: (event) ->
|
|
||||||
if event.origin != @expectedOrigin()
|
|
||||||
console.log("unexpected message #{event.origin}: #{event.data}")
|
|
||||||
else
|
|
||||||
Travis.config.access_token = event.data.token
|
|
||||||
Travis.app.setCurrentUser(event.data)
|
|
||||||
console.log("signed in as #{event.data.user.login}")
|
|
||||||
|
|
||||||
trySignIn: ->
|
|
||||||
@iframe.attr('src', "#{Travis.config.api_endpoint}/auth/post_message")
|
|
||||||
|
|
||||||
# # TODO: use views
|
|
||||||
# link = $("#navigation .profile")
|
|
||||||
# html = link.html()
|
|
||||||
# link.html("Signing in with GitHub...")
|
|
||||||
# window.setTimeout((-> link.html(html)), @timeout)
|
|
||||||
|
|
||||||
newUser: ->
|
|
||||||
localStorage?.setItem("travisTrySignIn", "true")
|
|
||||||
url = "#{Travis.config.api_endpoint}/auth/handshake?redirect_uri=#{window.location}"
|
|
||||||
window.location = url
|
|
||||||
|
|
||||||
checkUser: ->
|
|
||||||
@newUser() unless Travis.config.access_token?
|
|
||||||
|
|
||||||
signIn: ->
|
|
||||||
@trySignIn()
|
|
||||||
window.setTimeout((=> @checkUser()), @timeout)
|
|
|
@ -43,13 +43,13 @@
|
||||||
.handle
|
.handle
|
||||||
margin: 0
|
margin: 0
|
||||||
|
|
||||||
.signed-in, .signing-in, .sign-in
|
.signed-out, .signing-in, .signed-in
|
||||||
display: none
|
display: none
|
||||||
&.signed-in .signed-in
|
&.signed-out .signed-out
|
||||||
display: block
|
display: block
|
||||||
&.signing-in .signing-in
|
&.signing-in .signing-in
|
||||||
display: inline-block
|
display: inline-block
|
||||||
&.sign-in .sign-in
|
&.signed-in .signed-in
|
||||||
display: block
|
display: block
|
||||||
|
|
||||||
.signed-in
|
.signed-in
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -6628,11 +6628,11 @@ table.list .red .number a {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
/* line 46, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
/* line 46, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||||
#top .profile .signed-in, #top .profile .signing-in, #top .profile .sign-in {
|
#top .profile .signed-out, #top .profile .signing-in, #top .profile .signed-in {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
/* line 48, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
/* line 48, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||||
#top .profile.signed-in .signed-in {
|
#top .profile.signed-out .signed-out {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
/* line 50, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
/* line 50, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||||
|
@ -6640,7 +6640,7 @@ table.list .red .number a {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
/* line 52, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
/* line 52, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||||
#top .profile.sign-in .sign-in {
|
#top .profile.signed-in .signed-in {
|
||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
/* line 55, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
/* line 55, /Users/sven/Development/projects/travis/travis-ember/assets/stylesheets/top.sass */
|
||||||
|
|
Loading…
Reference in New Issue
Block a user