Wizcorp/phonegap-facebook-plugin

Not Able to Validate Server Side Login Using AccessToken and Fetch Profile Information

Opened this issue · 0 comments

I am getting accessToken on front End. however when i send the accessToken using POST to server i am not able to validate the accessToken and retrieve profile information. I get the accessToken
but cannot make it work in back end (NodeJS)

Code On Front End :
// Authenticate For Cordov ased FB Login Only.
//There is a Web Based Login Also For Other Device



log = console.log.bind(console);
app.provider('facebookAuth', [function() {

    var uri;
    return {

        config: function(options) {
            uri = options;
            log(uri);
        },
        $get: ['$http', function($http) {

            return {
                login: function(accessToken, cb) {

                    warn("Access Token :");
                    log(accessToken);
                    $http({
                        method: 'POST',
                        url: uri.facebookCordova,
                        params: { accessToken: accessToken }
                    }).then(function(resp) {
                        cb(resp);
                    });
                }
            }
        }]



    }
}])

On The Backend using NODEJS

log = console.log.bind(console);
//cordova Bsed Auth with AccessToken
    app.post('/auth/facebook/cordova', function(req, res) {

        log("/auth/cordova/facebook");
        var fields = ['id', 'email', 'first_name', 'last_name', 'link', 'name'];
        var graphApiUrl = 'https://graph.facebook.com/v2.5/me?fields=' + fields.join(',');

        var accessToken = req.body.accessToken || req.query.accessToken || req.param["accessToken"];
        log("Access Token :");
        log(accessToken);
        // Step 2. Retrieve profile information about the current user.
        request.get({ url: graphApiUrl, qs: accessToken, json: true }, function(err, response, profile) {
            if (err) {
                return res.status(401).send({ message: err });
            }

            // See if User Exist Or Not Else Create A New User
            UserProfileModel.findOne({ profile: profile.id }, function(err, existUser) {

                log("After Query User :");
                log(existUser);
                if (err) {
                    res.send({ message: 'Some Error Occured', token: undefined });
                }

                if (existUser) {
                    log("Existing User :");
                    // Update record and chnage flag values
                    log(existUser);
                    var token = createJWT(existUser);

                    res.send({ message: 'Existing User', profile: existUser, token: token, status: true });
                } else {

                    log("New User Found :");
                    log(profile);
                    var user = new UserProfileModel();
                    user.profile = profile.id || profile.userID;
                    user.picture = 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
                    user.fullName = profile.name;
                    user.email = profile.email;
                    user.isNewProfile = true;
                    user.isProfileCompleted = false;
                    log(user);
                    user.save(function(errSave, saved) {

                        if (errSave) {
                            res.status(401).send({ message: 'Duplicates Found', token: undefined, error: errSave, status: false });
                        }

                        log("User Saved Successfully :");
                        var token = createJWT(user);
                        res.send({ token: token, profile: user, status: true });

                    });
                }
            });
        });
    });

Please suggest what i am doing wrong here. I am trying to use AccessToken here to fetch Profile details and then save it to MongoDB is the user is new or return the profile details if exisiting user exist.

Thanks
Siddharth