简体   繁体   中英

Ruby on Rails omniauth and carrierwave

I'm using omniauth ( omniauth-facebook) in my ruby on rails application. I would like to know whether I can retrieve user's city, country and gender information. And how can I take user profile photo with carrierwave,

Thanks.

Using the (Carrierwave Documentation)[ https://github.com/carrierwaveuploader/carrierwave] as my reference, you would need to modify your user model to add a new fields to support the carrierwave gem functionality.

Typically this means:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end

But it will vary if you are using MongoDB instead of an SQL/ActiveRecord.

You can test this new field exists by initializing a new object in the rails console and examining the fields available.

bundle exec rails c 

u = User.new
u.(tab twice quickly to see the available options)
# You are looking for the avatar field
u.avatar

You can set it from console to test it using

File.open('somewhere') do |f|
  u.avatar = f
end

Then finally you would add it to the form field in your views once you tested it. You typically have a CRUD for your user so in the Create/Update forms in the views, you modify these forms to include a file form (this is HTML not specifically rails, so you can look up more information using HTML in your search), and once it is added, you will need to modify the corresponding controllers to whitelist the value so it can be saved using these controller actions.

The process is similar to adding other fields to your user object after the initial generation of the user model.

Usually omniauth will return a standard answer that is only concerned with authentication, basically a hash with a combination of email , name/first/last , profile_picture url , username , etc. Some providers give you email, others don't, others only provide some fields if you ask specifically for them through scopes.

For facebook I'm using the following on my omniauth.rb (inside config/initializers/*)

provider :facebook, ENV['FB_ID'], ENV['FB_SECRET'], scope: 'public_profile, email, manage_pages, user_friends', info_fields: 'id, first_name, last_name, link, email', image_size: 'large'

This means that facebook will provide me an email, some basic info such as ID, first name and last name on the response hash (that omniauth takes care of arranging) after a successful oauth authorization. The token it will provide will also be scoped for managing pages although I don't ask for any field related to it initially.

So in your case you would ask for city , country and whatever in info_fields (double checking through their graph explorer that you don't need any extra scope for those fields).

After you get the response through omniauth (which is basically a piece of code written as middleware, that does the oauth2 flow for you - you could do it by yourself as well) you'll have a profile pic url. You'll want to download that picture. Using carrierwave you do that either on your controller or module/class by instantiating the column where you have the uploader set and then executing the method .download! passing it the url from where to download:

user.avatar = AvatarUploader.new
user.avatar.download! omniauth_hash['blabla_fields']['blabla_picture_url']

This will download a remote picture url and save it as a regular carrierwave attachment, that you can then access through your model normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM