繁体   English   中英

如何在Devise中仅使用令牌身份验证,以确保后台无会话/ cookie混乱

[英]How do I use only token authentication with Devise making sure no session/cookie mess is going on in the background

我正在使用Devise和Rails来增强Android应用程序上的用户体验。 由于这是一个Android应用程序,因此我不会使用任何Cookie或会话。 如果我选择传递并存储客户端内容,则将明确执行此操作。 现在,我正在尝试测试我的注册和会话控制器,但是发现我遇到了会话问题。 也就是说,我不断收到requires_no_authentication莫名其妙地盘旋我欢迎使用Rails的页面。

我发现这是由于warden.authenticate!(scope: resource_name, recall: "#{controller_path}#failure"}工作方式造成的。

如果是这种情况,那么如何使我的“注册和会话”控制器正常工作?

我到了要把所有的Devise一起转储并从头开始制作自己的身份验证解决方案的地步。

似乎我的注册反复进行,因为我不使用warden.authenticate! 一旦使用,由于cookie /会话,它会与所有内容混淆。

这些是我目前的注册和会话控制器:

届会

class Api::V1::SessionsController < Devise::SessionsController
  skip_before_filter :verify_authenticity_token,
                     if: Proc.new { |c| c.request.format == 'application/json' }

  respond_to :json

  def create
    warden.authenticate!(scope: resource_name, recall: "#{controller_path}#failure")              

    render status: 200,
           json: { success: true,
                   info: "Logged in",
                   data: { auth_token: devise_current_user.authentication_token } }
  end

  def destroy
    warden.authenticate!(scope: resource_name, recall: "#{controller_path}#failure")

    current_user.update_column(:authentication_token, nil)

    render status: 200,
           json: { success: true,
                   info: "Logged out",
                   data: {} }
  end

  def failure
    render status: 401,
           json: { success: false,
                   info: "Login or Logout Failed",
                   data: {} }
  end
end

注册

class Api::V1::RegistrationsController < Devise::RegistrationsController
  skip_before_filter :verify_authenticity_token,
                     if: Proc.new { |c| c.request.format == 'application/json' }

  before_action :configure_permitted_parameters

  respond_to :json

  def create
    build_resource(sign_up_params)

    if resource.save
      sign_in resource

      resource_hash = {
        email: resource.email,
        name: resource.name,
        auth_token: resource.authentication_token,
        external_id: resource.external_id,
        created_at: resource.created_at
      }

      render status: 200,
             json: { success: true,
                     info: "Registered",
                     data: { user: resource_hash } }
    else
      render status: :unprocessable_entity,
             json: { success: false,
                     info: "Registration failed",
                     data: resource.errors }
    end
  end

  private

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:name, :email, :password, :password_confirmation)
    end
  end
end

设计删除令牌身份验证策略,因此您应该实施自己的令牌策略才能使用该指南,我现在正在处理相同的问题,因此我从头开始看守,请查看railscast 305情节以获取更多信息,是一个职业情节,但是我你没有访问权限,Warden的Wiki很容易解释,致谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM