Rails6 OmniAuth twitter Authentication Email conditional branch

A note for yourself by an eternal beginner. Rails learning started 3 months. For the time being, I would appreciate it if you could think about how it worked. If you have an opinion that such a person is ok, I would be grateful if you could give me a kind comment.

Purpose of this article

Since twitter authentication does not get an email address, it generates a random email address. For google and facebook authentication, get a legitimate email address. twitter is a random address. Others are conditional branching with a regular address.

Environment Ruby 2.7.1p83 Rails 6.0.3.3

Prerequisites

Login function by devise has been implemented.

Login authentication function for Twitter, google, facebook, etc. by OmniAuth has been implemented.

I referred to the following article ・ Procedure related https://qiita.com/kazuooooo/items/47e7d426cbb33355590e ・ Introduction of OmniAuth https://qiita.com/LuckHackMahiro/items/9dfca6e67777a2161240

Conditional branch: Twitter or other than Twitter Assuming that the function can be implemented

app/models/user.rb

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.provider = auth.provider
    user.uid = auth.uid
    user.name = auth.info.name
    user.password = Devise.friendly_token[0, 20]
    user.email = auth.info.email    #google,at facebook
    user.email = User.dummy_email(auth) if user.provider == "twitter"   #At the time of twitter
    avatar = open("#{auth.info.image}")
    user.image.attach(io: avatar, filename: "user_avatar.jpg ")
  end
end

If so, a random address on twitter. I was able to get a legitimate address on google and facebook.

I was happy that the conditional branching went well.

Recommended Posts

Rails6 OmniAuth twitter Authentication Email conditional branch
[Rails] Google, Twitter, Facebook authentication using Devise and Omniauth
[rails] Set validation
Steps to set a favicon in Rails
[Rails 6] Implementation of SNS (Twitter) sharing function
Rails6 OmniAuth twitter Authentication Email conditional branch
Java conditional branch
[Java] Conditional branch
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Ruby conditional branch processing