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.
Save the user's profile image in active storage. Even when I googled, I didn't see many articles about active storage because it was only carrier waves, so I posted it hoping that there would be people in the same situation.
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
It was very helpful! Thank you very much.
app/models/user.rb
require "open-uri" #here
class User < ApplicationRecord
#abridgement
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
user.email = User.dummy_email(auth) if user.provider == "twitter"
avatar = open("#{auth.info.image}") #here
user.image.attach(io: avatar, filename: "user_avatar.jpg ") #here
end
end
#abridgement
end
I was able to get the image by referring to the article. I didn't see many articles on active storage, so it was very helpful.
It was rumored that the image would be small and rough, so I tried to make it look like it would be bigger. There were some who specified it on the model side when looking at various things.
config/initializers/devise.rb
config.omniauth :facebook, ENV['FACEBOOK_ID'], ENV['FACEBOOK_SECRET_KEY'], :image_size => 'large'#this
config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
config.omniauth :twitter, ENV['TWITTER_API_KEY'], ENV['TWITTER_API_SECRET_KEY'], callback_url: "http://localhost:3000/users/auth/twitter/callback", :image_size => 'original'#this
The implementation related to login authentication may not work properly depending on the time of day, or if cookies remain in the browser, it may not work properly, so it seems good to do it patiently.
Recommended Posts