get first_name and last_name fields from facebook omniauth

后端 未结 3 758
慢半拍i
慢半拍i 2021-02-15 15:58

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.



        
3条回答
  •  猫巷女王i
    2021-02-15 16:39

    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
    

提交回复
热议问题