[Ruby] What I was addicted to when implementing google authentication with rails

1 minute read

This time, I summarized the things I was addicted to when implementing google authentication with rails, so even for reference. Article that I referred to when I implemented Google authentication ↓ [Rails] Implement user registration on Facebook/Twitter/Google at detonation speed using Devise & Omniauth

Error 400: invalid_request Missing required parameter: client_id

First one. It is an error that I got when I tried to authenticate by selecting an account on the Google authentication page. I was told that there is no client_id. There are two causes ① There was a space ② The key was entered as it was without using .env

① Space

devise.rb


config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'] ,ENV['GOOGLE_CLIENT_SECRET'], skip_jwt: true

From

devise.rb


config.omniauth :google_oauth2,ENV['GOOGLE_CLIENT_ID'],ENV['GOOGLE_CLIENT_SECRET'],skip_jwt: true

Changed to. It seems that I didn’t need space. There was space on the reference site.

② The key was entered as it was without using .env As far as other sites are concerned, it seems that some people were able to go this way, but it was not working in my environment. If you can’t solve it, try adding gem’dotenv-rails’.

Email verification is not done

This error is the next one If you do it normally, it will be displayed that the e-mail is not authenticated. this is

qiita.rb


class Users::OmniauthCallbacksController <Devise::OmniauthCallbacksController

  # callback for google
  def google_oauth2
    callback_for(:google)
  end

  # common callback method
  def callback_for(provider)
    @user = User.from_omniauth(request.env["omniauth.auth"])
    @user.skip_confirmation!  this and
    @user.save! This!
    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
    else
      session["devise.#{provider}_data"] = request.env["omniauth.auth"].except("extra")
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path
  end
  
end

@user.skip_confirmation! @user.save! It was solved by putting in the controller.

Petit advertising

I made an app called Public Diary.Itisanapplicationthatcanpublishadiary(itmaynotbepublished).OnlyIamusingit(crying). Please update it almost every day. Please.

URL: https://public-diary.herokuapp.com/

Summary

Actually, I have tried to implement sns authentication about 4 times and have failed. This was the first time I was able to do it, but when I was able to do it, I was pretty happy. Thank you for watching.

Tags: ,

Updated: