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.
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.
Login function by devise has been implemented. p>
Login authentication function for Twitter, google, facebook, etc. by OmniAuth has been implemented.
I referred to the following article p> ・ Procedure related https://qiita.com/kazuooooo/items/47e7d426cbb33355590e ・ Introduction of OmniAuth https://qiita.com/LuckHackMahiro/items/9dfca6e67777a2161240
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