I am using the rails_api gem in my project. I want to add session management for authentication, but it seems the session does not work. Here is my configuration in config/initializer/session_store.rb
:
Pmcapi::Application.config.session_store :cookie_store, {
key: '_pmcapi_session',
expire_after: 1.hour
}
I added config.api_only = false
in application.rb
(Adding cookie session store back to Rails API app)
and in my session_controller
, I added session to store the token
# session_controller.rb
def create
#just to generate new token
user.reset_sso_token!
session[:token] ||= user.sso_token
self.current_user = user
redirect_to root_path
end
When in application_controller
, I want to access session[:token]
but the result is nil
:
# application_controller.rb
def authenticate_user!
#puts("User Authentication")
#puts(request.authorization)
#puts(request)
@user = User.authenticate_with_token(session[:token])
#head :unauthorized unless @user.present?
redirect_to sign_in_path if @user.nil?
end