Flo*_*nce 5 ruby ruby-on-rails authlogic grape-api
我正在尝试使用GrapeAPI和构建用于登录的API Authlogic。
我的代码如下所示:
class Api
class V1
class UserSessionsController < Grape::API
post :login do
@user_session = UserSession.new(params[:user_session])
if @user_session.save
fetch_api_key(@user_session.user)
else
error!('Unauthorized.', 401)
end
end
helpers do
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是当我跑步时@user_session = UserSession.new(params[:user_session])我会得到You must activate the Authlogic::Session::Base.controller with a controller object before creating objects。
我尝试添加
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
在尝试创建之前UserSession,现在我可以undefined method 'session' for #<#<Class:0x007fa7a63ade28>:0x007fa7a7b144b8>运行了@user_session.save。
PS:我也尝试添加authenticate_with User到user_session.rb文件中,并且
acts_as_authentic do |c|
c.session_class = UserSession
end
在user模型内部,但不起作用。
PS2:我正在使用rails 4,2,7,grape 0.18.0和authlogic 3.4.6