mikker / passwordless

🗝 Authentication for your Rails app without the icky-ness of passwords
MIT License
1.26k stars 87 forks source link

Passwordless::ControllerHelpers#sign_in with authenticatable (`User') is deprecated warning #122

Closed arrowcircle closed 1 year ago

arrowcircle commented 2 years ago

Hey! I just found this warning and cant figure why I have this warning:

Passwordless::ControllerHelpers#sign_in with authenticatable (`User') is deprecated. Falling back to creating a new Passwordless::Session

It's not clear from this warning what to do. Any ideas how to fix this?

nickhammond commented 2 years ago

@arrowcircle Can you add some more context around how you're calling this? Normally just including the PasswordlessControllerHelpers module and then calling sign_in should do the trick. https://github.com/mikker/passwordless#registering-new-users

mhelmetag commented 2 years ago

I'm getting the same thing...

This is what I have:

class ApplicationController < ActionController::Base
  include Passwordless::ControllerHelpers

  # ...
end

class UsersController < ApplicationController
  # ...

  def create
    @user = User.new(user_params)

    if challenge_accepted? && @user.save
      sign_in @user
      redirect_to alerts_path
    else
      render :new, status: :unprocessable_entity
    end
  end

  # ...
end

And in my logs I see the same message.

This is from following the docs here: https://github.com/mikker/passwordless#registering-new-users

Everything still works... but the deprecation warning is just strange.

bcasci commented 1 year ago

Just a followup: I am get this warning as well, so I took a look at the source code. The warning makes sense when reading the code, which gives preference to Password::Session over another type of class instance, like our collective User models.

https://github.com/mikker/passwordless/blob/346ddb9da0570572423632e220e715b928e9f868/lib/passwordless/controller_helpers.rb#L76

If you want the warning to go away, you would build a Password::Session, and pass that to the sign_in method, even though the README advises you pass in an instance of your authenticatable class. I image it's this way because there might be plans to expose a Password::Session builder.

mikker commented 1 year ago

The current, non-deprecated way is this:

  def create
    @user = User.new(user_params)

    if @user.save
      sign_in(build_passwordless_session(@user)) # <-- This!
      redirect_to(@user, flash: { notice: 'Welcome!' })
    else
      render(:new)
    end
  end