Sha256: b6c983217b406d8695d3c1ed52f74f9ad84f62ba3e5a19c15fef92f706b72c23

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

class SessionsController < ApplicationController
  
  unloadable

  def new

    oauth_callback = request.protocol + request.host_with_port + '/oauth_callback'
    @request_token = SoundcloudAuth.consumer.get_request_token({:oauth_callback=>oauth_callback})
    session[:request_token] = @request_token.token
    session[:request_token_secret] = @request_token.secret
   
    url = @request_token.authorize_url
    url << "&oauth_callback=#{CGI.escape(SoundcloudAuth.oauth_callback)}" if SoundcloudAuth.oauth_callback?      
    redirect_to url
      
  end

  def oauth_callback
    
    unless session[:request_token] && session[:request_token_secret] 
      authentication_failed('No authentication information was found in the session. Please try again.') and return
    end

    unless params[:oauth_token].blank? || session[:request_token] ==  params[:oauth_token]
     authentication_failed('Authentication information does not match session information. Please try again.') and return
    end

    @request_token = OAuth::RequestToken.new(SoundcloudAuth.consumer, session[:request_token], session[:request_token_secret])

    oauth_verifier = params["oauth_verifier"]
    @access_token = @request_token.get_access_token(:oauth_verifier => oauth_verifier)
    
    session[:request_token] = nil
    session[:request_token_secret] = nil

    @user = User.identify_or_create_from_access_token(@access_token)

    session[:user_id] = @user.id

    cookies[:remember_token] = @user.remember_me

    authentication_succeeded 
    
    rescue Net::HTTPServerException => e
      case e.message
        when '401 "Unauthorized"'
          authentication_failed('This authentication request is no longer valid. Please try again.') and return
        else
          authentication_failed('There was a problem trying to authenticate you. Please try again.') and return
    end 
    
  end
  
  def destroy
    logout_keeping_session!
    redirect_back_or_default('/')
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
soundcloud-auth-0.2.0 app/controllers/sessions_controller.rb
soundcloud-auth-0.1.2 app/controllers/sessions_controller.rb
soundcloud-auth-0.1.1 app/controllers/sessions_controller.rb