我有2个数据库表,即coreteam
和user
。两种模型都经过精心设计。现在登录核心团队,我的API是:localhost:3000/api/v1/core_teams/sign_in
,带有以下请求数据。
{
"core_team": {
"email": "[email protected]",
"password": "Password21"
}
}
并且用户登录的API为:localhost:3000//api/v1/users/sign_in
带有以下请求数据。
{
"user": {
"email": "[email protected]",
"password": "Password20"
}
}
以下是我的routes.rb文件
Rails.application.routes.draw do
root to: 'users#index'
scope 'api/v1', defaults: { format: :json } do
devise_for :core_teams, controllers: {
sessions: 'api/v1/sessions',
passwords: 'api/v1/passwords'
}
devise_for :users, controllers: {
sessions: 'api/v1/sessions',
invitations: 'api/v1/invitations',
passwords: 'api/v1/passwords',
registrations: 'api/v1/registrations'
}
end
end
而且我正在使用devisesessioncontroller登录。唯一的区别是我渲染数据的方式。
我的sessions_controller.rb如下:
module Api
module V1
class SessionsController < Devise::SessionsController
private
def respond_with(resource, _opts = {})
render json: resource
end
def respond_to_on_destroy
head :no_content
end
end
end
end
现在,我的目标是仅使用一个API调用即可使用相同类型的请求数据进行登录。之所以要使用两个不同的用户表,是因为我的应用程序是多租户的,而核心团队不在租户之内,而用户在租户之内。
根据Devise的文档here,您可以通过在路线中指定sign_in URL中要访问的devise_scope
来实现。类似于以下内容(未经测试)
devise_scope :user do
get 'login', to: 'api/v1/sessions#new'
end
devise_scope :core_teams do
get 'login', to: 'api/v1/sessions#new'
end