I am now implementing omniauth feature into my app. Everything works fine except that i cant get the first and last name from the facebook. Here is my model code.
After some fiddling around i found the solution. Now i think we have to explicitly require the fields we require. For me the fix is just to add first_name
and last_name
to facebook
.
In my initializers
i added first_name
and last_name
to info fields
.
info_fields: 'email, first_name, last_name'
Update
My full config file will look like this now
config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], scope: 'email', info_fields: 'email, first_name, last_name'
From checking the Facebook docs, the first_name
and last_name
fields are both part of the public profile so your permissions should be fine.
No idea if this would work (in fact, I would kind of hope it doesn't), but in the implementation we've got working in production at the moment we're using Hash accessors instead of methods. So:
new(
first_name: oauth_data["info"]["first_name"],
last_name: oauth_data["info"]["last_name"],
...
Given your email
etc fields are getting set correctly, I'd be surprised if trying that works, but it might be worth a shot.
Failing that, have you got any validations or before_create
callbacks which could be interfering somehow?
Ran into this issue while designing the OAuth flow for an app that was using Devise + omniauth-facebook.
Using the public_profile scope, you get back a name attribute on the auth hash.
request.env["omniauth.auth"]
My issue is that a user has one profile and it is autosaved ( user is required to enter a first name and last name at sign up which is validated on the profile model)
Solution: Create a name parsing method in your omniauth services model, which you can pass the request.env["omniauth.auth"]["info"]["name"]
as an argument.
# auth hash returns a name like "John Doe Smith"
def parse_name_from_string(ful_name_string)
name_array = name_string.split(" ") # break apart the name
# I chose to return a PORO so i can do something like parsed_user_name_from_facebook_auth.first_name
{
last_name: name_array.pop,
first_name: name_array.join(" ")
}
end