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

This time, I summarized what I was addicted to when implementing google authentication with rails, so for reference. Articles I referred to when implementing Google authentication ↓ [Rails] Implement user registration on Facebook / Twitter / Google at explosive speed using Devise & Omniauth

Error 400: invalid_request Missing required parameter: client_id

First one. I got an error 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 looks like 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 I can see from other sites, it seems that some people could go as it is, but it was not possible in my environment. If you can't solve it, try adding gem'dotenv-rails'.

Email authentication is not done

The next thing that came out was this error If you do it normally, it will be displayed if the email 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!← with this
    @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! I solved it by putting it in the controller.

Petit promotion

I made an app called Public Diary. It's an app that allows you to publish your diary (it's possible not to publish it), but I'm the only one using it (crying). Please check it because it is updated almost every day. Please.

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

Summary

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

Recommended Posts

What I was addicted to when implementing google authentication with rails
What I was addicted to when trying to properly openAPI/Swagger documentation with Rails + Grape + Grape Swagger
What I was addicted to when introducing the JNI library
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
What I was addicted to while using rspec on rails
I was addicted to setting default_url_options with Rails devise introduction
What I was addicted to with the Redmine REST API
Memorandum: What I was addicted to when I hit the accounting freee API
[Rails] I was addicted to the nginx settings when using Action Cable.
Problems I was addicted to when building the digdag environment with docker
A memo that I was addicted to when making batch processing with Spring Boot
What to do when you launch an application with rails
The story I was addicted to when setting up STS
I was addicted to starting sbt
I was addicted to WSl when trying to build an android application development environment with Vue.js
A note when I was addicted to converting Ubuntu on WSL1 to WSL2
[Rails] I want to add data to Params when transitioning with link_to
What I was addicted to when updating the PHP version of the development environment (Docker) from 7.2.11 to 7.4.x
I was addicted to rewriting to @SpringApplicationConfiguration-> @SpringBootTest
I was addicted to the roll method
I was addicted to the Spring-Batch test
[Rails] How to solve ActiveSupport :: MessageVerifier :: InvalidSignature that I was addicted to when introducing twitter login [ActiveStorage]
A story I was addicted to when testing the API using MockMVC
My.cnf configuration problem that I was addicted to when I was touching MySQL 8.0 like 5.7
I was a little addicted to running old Ruby environment and old Rails
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
I was addicted to unit testing with the buffer operator in RxJava
I was addicted to using RXTX on Sierra
I tried to make Basic authentication with Java
I was addicted to installing Ruby/Tk on MacOS
I want to play with Firestore from Rails
What I did when I converted java to Kotlin
[Rails] I want to load CSS with webpacker
SpringSecurity I was addicted to trying to log in with a hashed password (solved)
I tried what I wanted to try with Stream softly.
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
I was addicted to the record of the associated model
What to do when rails creates a 〇〇 2.rb file
When I tried to unit test with IntelliJ, I was told "java.lang.OutOfMemoryError: Java heap space"
When importing CSV with Rails, it was really easy to use the nkf command
Addicted to the webpacker that comes standard with Rails 6
A story I was addicted to when getting a key that was automatically tried on MyBatis
When I tried to scroll automatically with JScrollBar, the event handler was drawn only once.
A story I was addicted to before building a Ruby and Rails environment using Ubuntu (20.04.1 LTS)
When I personally developed with Rails, it was a painful story that Rails was hit very much
When I tried to run Azure Kinect DK with Docker, it was blocked by EULA
[Rails] What to do when the view collapses when a message is displayed with the errors method
A story I was addicted to with implicit type conversion of ActiveRecord during unit testing
How to automatically generate ER diagram when migrating with Rails6
How to set environment variables when using Payjp with Rails
[Rails] I tried to create a mini app with FullCalendar
I want to push an app made with Rails 6 to GitHub
I entered from Rails and didn't know what [attr_accessor] was
I was addicted to looping the Update statement on MyBatis
What to check when rails db: migration does not pass
How to specify db when creating an app with rails
I was addicted to the setting of laradock + VSCode + xdebug
[Rails] I tried to implement batch processing with Rake task
What happens to instance variables when copying an instance with ruby
When I used Slick on Rails, it competed with Turbolinks.
I was addicted to using Java's Stream API in Scala