Friday, 7 April 2017

Integrating Facebook Login into Mobile Application using Node.JS Web Service as back-end

I have been coding an RESTful Web Service with Express.JS for our mobile game. I am trying to integrate our mobile game's auth service with Facebook. But I have some questions I don't have an answer.

Following is my code in web service (snippet from app.js);

app.post('/login/facebook', function(req, res) {
    var username = req.body.username;

    fb.api('oauth/authorize', {
        client_id: 'My Facebook App ID',
        redirect_uri: 'http://localhost:8080/login/facebook/callback'
    }, function (res) {
        if(!res || res.error) {
            console.log(!res ? 'error occurred' : res.error);
            return;
        }

        var accessToken = res.access_token;
        var expires = res.expires ? res.expires : 0;
        console("/login/facebook ||| " + accessToken + " ||| " + expires);
    });
});

app.post('/login/facebook/callback', function(req, res) {
    console.log("Facebook Callback Executed!");

    fb.api('oauth/access_token', {
        client_id: 'APP ID',
        client_secret: 'SECRET',
        code: req.params.code,
        redirect_uri: 'http://localhost:8080/login/facebook/callback'
    }, function (res) {
        if(!res || res.error) {
            console.log(!res ? 'error occurred' : res.error);
            return;
        }

        var accessToken = res.access_token;
        var expires = res.expires ? res.expires : 0;
        console("/login/facebook ||| " + accessToken + " ||| " + expires);
    });
});

What I am trying to do is connecting username with Facebook account.
When I deliver username with POST method to localhost:8080/login/facebook and try to invoke https://graph.facebook.com/oauth/authorize, it should redirect to Facebook Login Page, then redirect it to redirect_uri.
But as this is web service, I don't want it to. I want to show Facebook Login/Auth in my mobile application.
Am I using wrong API functions?
I can't seem to achieve this or find some readings on topic.

I need step by step instructions, because I can't understand.



via Oğuzhan Durgun