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
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'.
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.
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/
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