Implicit user creation with Authlogic and Authlogic OAuth plugin

前端 未结 1 731
故里飘歌
故里飘歌 2021-01-30 00:43

I\'m trying to write a simple OAuth consumer app in Rails. I\'m using Authlogic for handling authentication and the Authlogic OAuth plugin to do the oauth thing.

The oau

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 01:14

    Seems like I'm going to answer the question myself.

    I use the following code to generate the Sign In button (in HAML):

    - form_tag({:controller => "users", :action => "create"}, {:method => "post"}) do
      = oauth_register_button :value => "Sign In with Twitter"
    

    and then I simply create the user's session object in the create method of the UsersController class, if the user already exists:

    def create
      @user = User.new(params[:user])
      @user.save do |result| # LINE A
        if result
          flash[:notice] = "Account registered!"
          redirect_to some_inner_path
        else
          unless @user.oauth_token.nil?
            @user = User.find_by_oauth_token(@user.oauth_token)
            unless @user.nil?
              UserSession.create(@user)
              flash.now[:message] = "Welcome back!"
              redirect_to some_inner_path        
            else
              redirect_back_or_default root_path
            end
          else
            redirect_back_or_default root_path
          end
        end
      end
    end
    

    If the user is a first time visitor, then the user object is successfully saved in the LINE A. And if it's not and there's an oauth token available, then we try to fetch the user from the DB and log him/her in.

    0 讨论(0)
提交回复
热议问题