Skip to content

Commit

Permalink
on last.fm callback, do minimal work then refresh. fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Mar 7, 2012
1 parent 2c44bf9 commit 68e8b59
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/app.coffee
Expand Up @@ -1243,6 +1243,27 @@ handleResize = ->

$document.ready ->
socket = io.connect()

# special case when we get the callback from Last.fm.
# tell the server the token and save the session in localStorage.
# then refresh but remove the "?token=*" from the URL.
if (token = Util.parseQuery(location.search.substring(1))?.token)?
socket.emit 'LastfmGetSession', token

refreshPage = ->
location.href = "#{location.protocol}//#{location.host}/"
socket.on 'LastfmGetSessionSuccess', (data) ->
params = JSON.parse(data)
localStorage?.lastfm_username = params.session.name
localStorage?.lastfm_session_key = params.session.key
delete localStorage?.lastfm_scrobbling_on
refreshPage()
socket.on 'LastfmGetSessionError', (data) ->
params = JSON.parse(data)
alert "Error authenticating: #{params.message}"
refreshPage()
return

mpd = new window.SocketMpd socket
mpd.on 'error', (msg) -> alert msg
mpd.on 'libraryupdate', ->
Expand All @@ -1264,16 +1285,6 @@ $document.ready ->

if (user_name = localStorage?.user_name)?
socket.emit 'SetUserName', user_name
if (token = Util.parseQuery(location.search.substring(1))?.token)?
socket.emit 'LastfmGetSession', token

socket.on 'LastfmGetSessionSuccess', (data) ->
params = JSON.parse(data)
localStorage?.lastfm_username = params.session.name
localStorage?.lastfm_session_key = params.session.key
delete localStorage?.lastfm_scrobbling_on
renderSettings()

setUpUi()
initHandlebars()

Expand Down

0 comments on commit 68e8b59

Please sign in to comment.