使用 Restful_authentication 在单独的控制器中对用户进行身份验证

发布于 2024-08-13 05:17:49 字数 682 浏览 1 评论 0 原文

我试图让用户尽可能快地登录,因此我希望用户能够以相同的表单登录并创建记录。

是否可以通过某种方式调用会话控制器中的 create 方法,使用来自任何控制器的 Restful_authentication 插件对用户进行身份验证,并返回经过身份验证的用户?看起来这可以很容易地完成,但我就是不知道如何在 Rails 中做到这一点。

也许是这样的:

#Records Controller

def create
    if params[:login] && params[:password]
        #This method would call /session/ and pass the login/password params
        user = authenticate_user(params[:login'], params[:password])
    end

    @record = Record.new(params[:record])
    @record.user = user

    if @question.save && user
        flash[:notice] = 'Record was successfully created.'
        redirect_to(@record)
    end
end

任何关于如何做到这一点的想法将不胜感激!

I am trying to make it possible for users to login as quick as possible, so I want users to be able to login and create records in the same form.

Is it possible to authenticate a user with the restful_authentication plugin from any controller by somehow calling the create method in the session controller, and return the authenticated user? It seems like this could be done easily somehow, but I just can't figure out how to do it in Rails.

Maybe something like:


#Records Controller

def create
    if params[:login] && params[:password]
        #This method would call /session/ and pass the login/password params
        user = authenticate_user(params[:login'], params[:password])
    end

    @record = Record.new(params[:record])
    @record.user = user

    if @question.save && user
        flash[:notice] = 'Record was successfully created.'
        redirect_to(@record)
    end
end

Any ideas on how to do this would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2024-08-20 05:17:49

我已经在 Rails 2.3.4 上测试了这段代码,它可以工作;用户保持登录状态。请记住,您应该尝试重构,以便身份验证代码位于单个位置,而不是将其复制到多个控制器中。

另请注意,此代码片段中的身份验证代码是会话控制器中的身份验证代码的简化版本,&所以不处理任何“记住我”功能。

# POST /stacks
# POST /stacks.xml
def create
  @stack = Stack.new(params[:stack])

  if params[:login] && params[:password]
    logout_keeping_session!
    user = User.authenticate(params[:login], params[:password])
    self.current_user = user
  end

  respond_to do |format|
    if !user
      flash[:error] = 'Login details incorrect.'
      format.html { render :action => "new" }
      format.xml  { render :xml => @stack.errors, :status => :unprocessable_entity }
    elsif @stack.save
      flash[:notice] = 'Stack was successfully created.'
      format.html { redirect_to(@stack) }
      format.xml  { render :xml => @stack, :status => :created, :location => @stack }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @stack.errors, :status => :unprocessable_entity }
    end
  end
end

I've tested this code on Rails 2.3.4 and it works; the user remains logged in. Bear in mind that you should try to refactor so that the authentication code lives in a single place, rather than having it duplicated in several controllers.

Note also that the authentication code in this snippet is a simplified version of that in the Sessions controller, & so doesn't handle any of the 'remember me' functionality.

# POST /stacks
# POST /stacks.xml
def create
  @stack = Stack.new(params[:stack])

  if params[:login] && params[:password]
    logout_keeping_session!
    user = User.authenticate(params[:login], params[:password])
    self.current_user = user
  end

  respond_to do |format|
    if !user
      flash[:error] = 'Login details incorrect.'
      format.html { render :action => "new" }
      format.xml  { render :xml => @stack.errors, :status => :unprocessable_entity }
    elsif @stack.save
      flash[:notice] = 'Stack was successfully created.'
      format.html { redirect_to(@stack) }
      format.xml  { render :xml => @stack, :status => :created, :location => @stack }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @stack.errors, :status => :unprocessable_entity }
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文