[英]custom authentication with devise for an API
所以我在这里有一些棘手的组合
Company has many Users
User belongs to Company
通过设计管理用户进行身份验证
class User < ActiveRecord::Base
belongs_to :company
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
您可以作为用户登录并创建全部属于该公司而不属于该用户的对象,例如: Text
。 ( company.texts
)
现在,我使用acts_as_api
gem创建了一个简单的API。 为此,我只需要修改我的文本控制器,例如show动作。
class TextsController < ApplicationController
load_and_authorize_resource
def show
#@text = Text.find(params[:id])
respond_to do |format|
format.html
format.json { render_for_api :texts_all, :json => @text }
end
这在网站上效果很好。 问题是API。 通过用户模型访问api时,我不想进行身份验证。 该公司确实有一个名为:hash
的属性,我想在API中使用它来进行Auth。
我不知道如何使用设计(或任何其他方法)来实现这一目标。 因此,默认情况下, load_and_authorize_resource
希望由于我的控制器中的load_and_authorize_resource
用户登录,这对于html响应而言不错,但对于json响应而言则不错。
有任何想法吗?
感谢您阅读本文。 如果有不清楚的地方请发表评论!
根据接受的格式标题有条件地应用身份验证过滤器:
# override in controllers related ot API
def authenticate_user!
respond_to do |format|
format.html { super } # just like before
format.json { enforce_api_auth }
end
end
现在,API调用将执行自己的身份验证。
只需使用令牌Authenticateable并随API上的每个请求发送令牌即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.