Not able to create session with an object

167 Views Asked by At

I'm trying to implement my first authlogic-based app in ruby on rails. I need to sign user using a custom oauth2 flow, at the end of the process, I want to tell authlogic which user I want him to authenticate. Without providing a login nor password, just directly by passing a user object.

I've found in the README that I can do that using UserSession.create(user, true) but putting this at the end of my OAuth callback step is not persisting my user. There is no error nor exception, everything seems to be fine but UserSession.find returns nil (even in the next line after the UserSession.create). I'm on rails 5.1.6 and authlogic 4.0.1.

User model

class User < ApplicationRecord
  acts_as_authentic
end

UserSession model

class UserSession < Authlogic::Session::Base
end

Thanks in advance for any clues about what I did wrong.

1

There are 1 best solutions below

0
Fabrizio Bertoglio On

This is what I found out, from Authlogic::Session::Base

module Authlogic
  module Session # :nodoc:
    # This is the most important class in Authlogic. You will inherit this class
    # for your own eg. `UserSession`.
    #
    # Code is organized topically. Each topic is represented by a module. So, to
    # learn about password-based authentication, read the `Password` module.
    #
    # It is common for methods (.initialize and #credentials=, for example) to
    # be implemented in multiple mixins. Those methods will call `super`, so the
    # order of `include`s here is important.
    #
    # Also, to fully understand such a method (like #credentials=) you will need
    # to mentally combine all of its definitions. This is perhaps the primary
    # disadvantage of topical organization using modules.

as I read from their instructions, they do never say that you can authenticate user without email and password, but that you can create a session with the User object

# skip authentication and log the user in directly, the true means "remember me"
UserSession.create(my_user_object, true)

I would read more about the Session.rb

https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/session/session.rb

sorry if I can not give you more help

PS you can also debug this gem by setting a binding.pry in the gem files