Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 鉴定设计_Ruby On Rails_Http_Authentication_Devise_Restful Authentication - Fatal编程技术网

Ruby on rails 鉴定设计

Ruby on rails 鉴定设计,ruby-on-rails,http,authentication,devise,restful-authentication,Ruby On Rails,Http,Authentication,Devise,Restful Authentication,我希望能够简单地确定我正在创建的iOS应用程序中是否正确提供了用户凭据 我现在设置它的方式是使用sessions_controller.rb来处理和返回用户令牌。问题是,如果我仍然想通过web登录(而不仅仅是通过curl或类似的方式进行检查),那么它不会进行身份验证,并且会吐出 {“success”:false,“message”:“登录或密码错误”} 所以我的问题是…我如何进行身份验证并保持我的web登录操作?这是我的相关文件。我希望我可以卷曲到一个url,比如localhost:3000/a

我希望能够简单地确定我正在创建的iOS应用程序中是否正确提供了用户凭据

我现在设置它的方式是使用sessions_controller.rb来处理和返回用户令牌。问题是,如果我仍然想通过web登录(而不仅仅是通过curl或类似的方式进行检查),那么它不会进行身份验证,并且会吐出

{“success”:false,“message”:“登录或密码错误”}

所以我的问题是…我如何进行身份验证并保持我的web登录操作?这是我的相关文件。我希望我可以卷曲到一个url,比如localhost:3000/auth_检查并获得一种类型的身份验证响应,然后继续让我的用户通过localhost:3000/sign_登录

来自designe.rb

config.skip_session_storage = [:http_auth, :token_auth]
config.token_authentication_key = :auth_token
从routes.rb

  resources :clippings
  root to: 'clippings#index'
  #devise_for :users

  resources :auth_checks
  devise_for(:users, :controllers => { :sessions => "sessions" })


  resources :posts do
  end
  namespace :api do
      namespace :v1  do
        resources :tokens,:only => [:create, :destroy]
      end
    end
从auth_checks_controller.rb

class AuthChecksController < ApplicationController

before_filter :authenticate_user!

  # GET /auth_checks
  # GET /auth_checks.json
  def index
    @auth_checks = AuthCheck.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @auth_checks }
    end
  end

  # GET /auth_checks/1
  # GET /auth_checks/1.json
  def show
    @auth_check = AuthCheck.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @auth_check }
    end
  end

  # GET /auth_checks/new
  # GET /auth_checks/new.json
  def new
    @auth_check = AuthCheck.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @auth_check }
    end
  end

  # GET /auth_checks/1/edit
  def edit
    @auth_check = AuthCheck.find(params[:id])
  end

  # POST /auth_checks
  # POST /auth_checks.json
  def create
    @auth_check = AuthCheck.new(params[:auth_check])

    respond_to do |format|
      if @auth_check.save
        format.html { redirect_to @auth_check, notice: 'Auth check was successfully created.' }
        format.json { render json: @auth_check, status: :created, location: @auth_check }
      else
        format.html { render action: "new" }
        format.json { render json: @auth_check.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /auth_checks/1
  # PUT /auth_checks/1.json
  def update
    @auth_check = AuthCheck.find(params[:id])

    respond_to do |format|
      if @auth_check.update_attributes(params[:auth_check])
        format.html { redirect_to @auth_check, notice: 'Auth check was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @auth_check.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /auth_checks/1
  # DELETE /auth_checks/1.json
  def destroy
    @auth_check = AuthCheck.find(params[:id])
    @auth_check.destroy

    respond_to do |format|
      format.html { redirect_to auth_checks_url }
      format.json { head :no_content }
    end
  end
end
类AuthChecksController
我学到了很多……以下是我最后做的事情。如果你处在这个位置,我强烈建议你花时间(不是很多时间)来做这个方法

如果你和我一样,你已经有了一个使用标准Desive登录结构的用户库

我将此添加到我的routes.rb

  resources :clippings
  root to: 'clippings#index'
  #devise_for :users

  resources :auth_checks
  devise_for(:users, :controllers => { :sessions => "sessions" })


  resources :posts do
  end
  namespace :api do
      namespace :v1  do
        resources :tokens,:only => [:create, :destroy]
      end
    end
然后在controllers/api/v1/(我创建的)内部创建并添加令牌\u controller.rb

编码:utf-8 类Api::V1::TokensController406,:json=>{:message=>“请求必须是json”} 返回 结束 如果email.nil?还是密码。零? 渲染:状态=>400, :json=>{:message=>“请求必须包含用户电子邮件和密码。”} 返回 结束 @用户=用户。通过电子邮件查找(email.downcase) 如果@user.nil? info(“用户#{email}登录失败,找不到用户。”) render:status=>401,:json=>{:message=>“无效的电子邮件或密码。”} 返回 结束 # http://rdoc.info/github/plataformatec/devise/master/Devise/Models/TokenAuthenticatable @user.sure\u身份验证\u令牌! 如果不是@user.valid\u密码?(密码) logger.info(“用户{email}登录失败,密码\”{password}\“无效”) render:status=>401,:json=>{:message=>“无效的电子邮件或密码。”} 其他的 render:status=>200,:json=>{:token=>@user.authentication\u token} 结束 结束 def销毁 @user=user.find\u by\u authentication\u令牌(params[:id]) 如果@user.nil? #logger.info(“找不到令牌”) #render:status=>404,:json=>{:message=>“无效令牌”。} 其他的 @user.reset\u身份验证\u令牌! render:status=>200,:json=>{:token=>params[:id]} 结束 结束 结束 这就是我要做的一切。我现在可以从iOS应用程序测试此api以进行身份验证。希望这对外面的人有意义

试试这个: