     @extractOauthFromSession clientId, (err, foreignAuthInfo, session)=>
       return callback err  if err
       return callback()    unless foreignAuthInfo
+      return callback()    unless session
+
       @saveOauthToUser foreignAuthInfo, username, (err)=>
         return callback err  if err
         @clearOauthFromSession session, (err)=>

  @extractOauthFromSession: (clientId, callback)->
    JSession.one {clientId: clientId}, (err, session)->
      return callback err  if err

      {foreignAuth, foreignAuthType} = session
      if foreignAuth and foreignAuthType

    {sessionToken} = client
    JSession.one {clientId: sessionToken}, (err, session) =>
      return callback err  if err
      return callback createKodingError "Couldn't restore your session."  unless session

      kallback = (err, resp={}) ->
        {account, replacementToken} = resp
