Send remember_me checkbox data to omniauth_authorize_path in form using Devise and Rails 7

Viewed 9

I'd like users to be able to select if they want remembered or not when they authenticate with google.

However, the only way to access the input user put into the checkbox is to add data to omniauth_authorize_path(resource_name, 'google_oauth2', remember_me: "") and then access it with request.env["omniauth.params"]. The params from the form never make it to the controller.

How do I put dynamic user input into this line to replace the remember_me hash data:

<%= form_with url: omniauth_authorize_path(resource_name, 'google_oauth2', remember_me: ""), method: :post, data: { turbo: false } do |f| %>

<%- if devise_mapping.omniauthable? %>
  <div class="mb-2">
    Sign in with<br/>
  </div>

  <%= form_with url: omniauth_authorize_path(resource_name, 'google_oauth2', remember_me: ""), method: :post, data: { turbo: false } do |f| %>

    <div class="mb-2">
      <%= button_tag( :class => "btn btn-outline-dark btn-sm") do %>
        <i class="bi bi-google"></i> Google
      <% end %>
    </div>

    <% if devise_mapping.rememberable? %>
      <div class="mb-3">
        <%= f.check_box :remember_me, class: "form-check-input" %>
        <%= f.label :remember_me, class: "form-check-label" %>
      </div>
    <% end %>

  <% end %>
<% end %>
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    # You need to implement the method below in your model
    @user = User.from_omniauth(request.env['omniauth.auth'])
    remember_me = request.env["omniauth.params"].try(:fetch, "remember_me", false)
    puts '-----------'
    puts params
    puts '-----------'
    puts request.env["omniauth.params"]
    puts '-----------'
    if @user and @user.persisted? and !params.empty?
      #@user.remember_me = remember_me
      flash[:notice] = I18n.t 'devise.omniauth_callbacks.success',
                       kind: 'Google'
      sign_in_and_redirect @user, event: :authentication
    elsif !helpers.allow_users_to_join?
      session['devise.google_data'] = request.env['omniauth.auth']
                                      .except('extra')
      redirect_to new_user_registration_url,
                  alert: 'Not authorized' unless current_user
    else
      session['devise.google_data'] = request.env['omniauth.auth']
                                      .except('extra')
      if !current_user
        redirect_to new_user_registration_url,
                  alert: @user.errors.full_messages.join("\n")
      end
    end
  end
  # You should configure your model like this:
  # devise :omniauthable, omniauth_providers: [:twitter]

  # You should also create an action method in this controller like this:
  # def twitter
  # end

  # More info at:
  # https://github.com/heartcombo/devise#omniauth

  # GET|POST /resource/auth/twitter
  # def passthru
  #   super
  # end

  # GET|POST /users/auth/twitter/callback
  # def failure
  #   super
  # end

  # protected

  # The path used when OmniAuth fails
  # def after_omniauth_failure_path_for(scope)
  #   super(scope)
  # end
end
0 Answers
Related