0

I have enable user to get logged in via social media Facebook and google. User is getting logged in via google but cannot logged in via Facebook. When I debug my code I came to know that user's emails value is not coming. However user name is coming. So the question is how to fetch user's emails. I have the same code for google and Facebook. Here is my code :

// get social contacts with this function.(like facebook,google,yahoo)
    function getLogin(network, path){
        var basePath = $('#baseurl').val(); // base url
        var redirectUrl = basePath+'login';
        var localScope = 'email';
        // initilize 
        hello.init({
            google : 'xxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxmbesm1.apps.googleusercontent.com',
            facebook : 'xxxxxxxxxxxxxxxxxx', 
            }, {
            redirect_uri: redirectUrl,
            scope:localScope
            }
        );
        // login
        hello.login( network, {scope:localScope}, function(auth){
            if(!auth||auth.error){  
                return;
            }
            else
            {
                // Get Profile
                hello( network ).api( '/me' , function responseHandler(p){
                    user    =   p;
                    var userstr="name="+user.name+"&";

                    userstr +="link="+''+"&";   
                    userstr +="username="+user.displayName+"&";
                    userstr +="socailid="+user.id+"&";   //str +="username="+user.given_name+"&";
                    userstr +="email="+user.email;
                    // save data to google
                    $.post(baseURL+'social_check', {email:user.email,socialid:user.id}, function(resp){
                        if(resp === 'true')
                        {
                            window.location.href = baseURL+'join';
                        }
                        else
                        {
                            $.ajax({
                                type: "POST",
                                url: baseURL+'social-signup',
                                dataType: 'json',
                                data: userstr,
                                success: function(response){

                                    if(response.success == 'inactive')
                                    {
                                        $('#gmail-error').show().html("Your account is inactive. To activate it, please click on the activation link we sent you by email. Want us to <a href='"+baseURL+"resend-confirmation-link'>resend it</a>?");
                                        $('#fb-error').hide();
                                        return false;
                                    }
                                    else if(response.success == 'deleted'){
                                        $('#gmail-error').show().html("Your account is inactive. To activate it, please click on the activation link we sent you by email. Want us to <a href='"+baseURL+"resend-confirmation-link'>resend it</a>?");
                                        $('#fb-error').show();
                                        return false;
                                    }
                                    else if(response.success=='error')
                                    {
                                        $('#gmail-error').show().text("We don't recognize this account - please sign up with it on the sign-up screen first.");
                                        $('#fb-error').hide();
                                        return true;
                                    }
                                    else if(response.success == 'not-inserted'){
                                        $('#gmail-error').show().text("Something went wrong.");
                                        $('#fb-error').hide();
                                        return false;
                                    }
                                    // if redirect url is not set
                                    if(response.success===false)
                                    {
                                        $('#fb-root').hide(); $('#status').hide();
                                        window.location.href = baseURL;
                                    }
                                    else{
                                        $('#fb-root').hide(); $('#status').hide();
                                        window.location.href = baseURL+response.success;
                                    }
                                },

                                error: function(response)
                                {
                                    $('#gmail-error').show().text("Oops something went wrong. Please try one more time.");
                                    $('#fb-error').hide();
                                    return false;
                                }
                            });
                        }
                    });
                }); 
            }
        });
    }
</script>

userstr +="email="+user.email; this is coming blank. What could I do get email of users. Any help would be highly appreciable. If anybody have an idea kindly help me out.

  • You need to _ask_ for the fields you want with the Facebook API, otherwise you will only get a small default subset, usually just the object id, and either name (for users/pages) or message content (for posts/comments.) Be aware though that you simply won’t _get_ an email address for every single Facebook user (for example if they signed up using only their mobile), so your app needs to be prepared for that case as well. – CBroe Jul 11 '18 at 09:32
  • I see the docs of facebook which says you don't need to ask for permission in order to get email of users. It is the default – John Anderson Jul 11 '18 at 09:40
  • Nobody said anything about a permission. Have you actually _read_ the duplicate …? – CBroe Jul 11 '18 at 09:43
  • Provides access to the person's primary email address. This permission is approved by default. – John Anderson Jul 11 '18 at 10:08
  • if you go on fb app and approved item you will get above message – John Anderson Jul 11 '18 at 10:09
  • Again: Nobody said anything about _permissions_ here. This is about the _fields_ you want to get from the user object in your request. How can that possibly still not be clear, after you have read the duplicate? – CBroe Jul 11 '18 at 10:11
  • _“I see the docs of facebook which says you don't need to ask for permission in order to get email of users. It is the default”_ - not even that is correct … you need to go and get at least a basic understanding of what you are dealing with here first. Of course you need to ask the individual user for their _permission_, before you _can_ get their email. What you are referring to is that your app’s ability to ask users for that permission is already approved by default, meaning you don’t need to submit it to Facebook for _review_ in that particular regard. – CBroe Jul 11 '18 at 10:13
  • You mean to say what facebook written (you don't need to ask for permission in order to get email of users. It is the default) is wrong. So in that case what should I do for getting permission from users – John Anderson Jul 11 '18 at 10:24
  • It does not say that anywhere. Again: You are confusing the need to get certain permissions _reviewed_ before your app _can_ ask normal users for them, with actually _asking_ the user for the permission. – CBroe Jul 11 '18 at 10:28
  • Alright. How can I achieve my desired goal? Any guideline ? any link ? which can solve my problem ?? – John Anderson Jul 11 '18 at 10:50
  • AGAIN: The duplicate explains how you ask for the fields you want. – CBroe Jul 11 '18 at 11:05

0 Answers0