Rails6 OmniAuth activestorage Get user image

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

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.

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

Reference ・ How to save the image URL in active storage https://qiita.com/gomasio1010/items/09c6ee58ed4c95f109ff

It was very helpful! Thank you very much.

Save the image url in active storage Assuming that the function can be implemented

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

Rails6 OmniAuth activestorage Get user image
[Rails] Implement User search function
Rails Posts and User Linkage
Rails Active_storage -Simplify saving images-
Rails image slides automatically [Swiper]
[Rails] Implement image posting function
Memorandum [Rails] User authentication Devise
[Rails] [ActiveStorage] Find out if a particular user has an avatar