lib/login_system.rb
# controller method to handle twitter callback (expected after login_by_oauth invoked)
controller method to handle twitter callback (expected after login_by_oauth invoked)
controller method to handle twitter callback ( expected after login_by_oauth invoked )
def callback
    self.twitagent.exchange_request_for_access_token( session[:request_token], session[:request_token_secret], params[:oauth_verifier] )
    user_info = self.twitagent.verify_credentials
    raise LoginSystem::RequestError unless user_info['id'] && user_info['screen_name'] && user_info['profile_image_url']
    @user = User.find_by_screen_name(user_info['screen_name'])
    if @user == nil
      if is_logged_in? 
        logged_in_user.update_attributes(
          :twitter_id => user_info['id'],
          :screen_name => user_info['screen_name'],
          :token => self.twitagent.access_token.token,
          :secret => self.twitagent.access_token.secret,
          :profile_image_url => user_info['profile_image_url'])
        self.twitagent( user_token = logged_in_user.token, user_secret = logged_in_user.secret )
        flash[:notice] = "你的帳戶已與 Twitter 建立連接。下次您可以使用這個帳號來登入"
        return redirect_to user_path(logged_in_user)
      else
        @user = User.new({ 
          :twitter_id => user_info['id'],
          :screen_name => user_info['screen_name'],
          :token => self.twitagent.access_token.token,
          :secret => self.twitagent.access_token.secret,
          :profile_image_url => user_info['profile_image_url'] })
        flash[:notice] = "已完成 Twitter 帳戶驗證，接下來我們需要您填寫一些基本資料"
        @title = "註冊 #{app_name} 帳戶"
        render :action => 'new'
      end
    else
      if is_logged_in? && !is_me?(@user)
        flash[:error] = "抱歉，這個 Twitter 帳戶已經與其他帳戶連結了"
        redirect_back_or_default('/')
      else 
        @user.token = self.twitagent.access_token.token
        @user.secret = self.twitagent.access_token.secret
        @user.profile_image_url = user_info['profile_image_url']
        self.logged_in_user = @user
        self.logged_in_user.increment!(:signin_count)
        flash[:notice] = "嗨 #{@user.screen_name}，你已經透過 Twitter 帳戶登入"
        return redirect_back_or_default('/')
      end
    end
  end
def callback self . twitagent . exchange_request_for_access_token ( session [ :request_token ] , session [ :request_token_secret ] , params [ :oauth_verifier ] ) user_info = self . twitagent . verify_credentials raise LoginSystem :: RequestError unless user_info [ 'id' ] && user_info [ 'screen_name' ] && user_info [ 'profile_image_url' ] @user = User . find_by_screen_name ( user_info [ 'screen_name' ] ) if @user == nil if is_logged_in? logged_in_user . update_attributes ( :twitter_id => user_info [ 'id' ] , :screen_name => user_info [ 'screen_name' ] , :token => self . twitagent . access_token . token , :secret => self . twitagent . access_token . secret , :profile_image_url => user_info [ 'profile_image_url' ] ) self . twitagent ( user_token = logged_in_user . token , user_secret = logged_in_user . secret ) flash [ :notice ] = "你的帳戶已與 Twitter 建立連接。下次您可以使用這個帳號來登入" return redirect_to user_path ( logged_in_user ) else @user = User . new ( { :twitter_id => user_info [ 'id' ] , :screen_name => user_info [ 'screen_name' ] , :token => self . twitagent . access_token . token , :secret => self . twitagent . access_token . secret , :profile_image_url => user_info [ 'profile_image_url' ] } ) flash [ :notice ] = "已完成 Twitter 帳戶驗證，接下來我們需要您填寫一些基本資料" @title = "註冊 #{app_name} 帳戶" render :action => 'new' end else if is_logged_in? && ! is_me? ( @user ) flash [ :error ] = "抱歉，這個 Twitter 帳戶已經與其他帳戶連結了" redirect_back_or_default ( '/' ) else @user . token = self . twitagent . access_token . token @user . secret = self . twitagent . access_token . secret @user . profile_image_url = user_info [ 'profile_image_url' ] self . logged_in_user = @user self . logged_in_user . increment! ( :signin_count ) flash [ :notice ] = "嗨 #{@user.screen_name}，你已經透過 Twitter 帳戶登入" return redirect_back_or_default ( '/' ) end end end
controller method to handle twitter callback (expected after login_by_oauth invoked)
controller method to handle twitter callback ( expected after login_by_oauth invoked )
# We have an authorized user, save the information to the database. # Authenticate from user's setting page or status_update twitter button # If user is logged in and no Twitter ID in the database! # Authenticate from signup page # Logged user in via twitter oauth