I'm trying to build an API for login using GrapeAPI
and Authlogic
.
My code looks like this:
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
The problem is that when I run @user_session = UserSession.new(params[:user_session])
I get You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
.
I tried adding
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
before trying to create the UserSession
and now I get undefined method 'session' for #<#<Class:0x007fa7a63ade28>:0x007fa7a7b144b8>
when I run @user_session.save
.
PS: I also tried adding authenticate_with User
to the user_session.rb
file, and
acts_as_authentic do |c|
c.session_class = UserSession
end
Inside the user
model, but it doesn't work.
PS2: I'm using rails 4,2,7 , grape 0.18.0 and authlogic 3.4.6