GithubHelp home page GithubHelp logo

Comments (11)

jaredhanson avatar jaredhanson commented on May 8, 2024 3

See passport-facebook-token, which implements this as a Passport strategy.

from passport-facebook.

chaolam avatar chaolam commented on May 8, 2024

+1 encountering the same issue too

from passport-facebook.

pavelnikolov avatar pavelnikolov commented on May 8, 2024

+1

from passport-facebook.

markwillis82 avatar markwillis82 commented on May 8, 2024

+1 on this too

from passport-facebook.

pavelnikolov avatar pavelnikolov commented on May 8, 2024

I have a single page application. It does not use cookies. Instead I use access_token with the requests that need authentication. This access_token is alpha-numeric string long 40 chars and valid only for 1 hour. In order to use Facebook auth I need to click a "login with facebook" button which leads to http://api.mydomain.com/auht/facebook. Then when the facebook callback is called my API generates a token and needs to send this token to the client somehow.
Right now I'm redirecting to the client to a parametrized URL like this one /#/auth/:token so that the client can get the token and from that point on make authentic requests to the API server. I don't like this solution. My app is still in development mode and I would like to release it in production with a better way of giving the token to the client.

-Pavel

from passport-facebook.

ptz0n avatar ptz0n commented on May 8, 2024

@pavelnikolov Your solution works but, as you mention, is kind of a hack.

After digging into this strategy I took a step back and realised that it's a pure OAuth 2.0 wrapper. Aside from the Facebook JavaScript SDK client-side authentication.

Using the Facebook client-side accessToken (short lived) it's not possible to login the user (via XHR) without the redirect that OAuth 2.0 enforces. I've implemented this in a project simply by using the req.login method after verifying the token and fetching user data from Facebook.

Conclution: Client-side authentication via the Facebook JavaScript SDK is not a natural feature of this strategy and should be defined as a separate Passport strategy.

from passport-facebook.

pavelnikolov avatar pavelnikolov commented on May 8, 2024

Thank you for your reply - I knew there should be a better way. I haven't used that so far. Can you point me to a link to some documentation or examples?

from passport-facebook.

ptz0n avatar ptz0n commented on May 8, 2024

@pavelnikolov Using Express, Mongoose and FBgraph, this is a simplified example of a POST controller:

graph.setAccessToken(req.body.accessToken);
graph.get('me?fields=id,email,name', function(err, data) {
    if(err) return res.json(400, err);

    User.findOne({'facebook.id': data.id}, function(err, user) {
        if(err) return res.json(400, err);

        if(!user) {
            // First login, create user object..

            // Then login the user
            req.login(user, function(err) {
                if(err) return res.json(400, err);
                res.json(user);
            });
        }
        else {
            // User found, update user object..

            // Then login the user
            req.login(user, function(err) {
                if(err) return res.json(400, err);
                res.json(user);
            });
        }
    });
});

from passport-facebook.

pavelnikolov avatar pavelnikolov commented on May 8, 2024

Thank you. I will try this for sure.

from passport-facebook.

etler avatar etler commented on May 8, 2024

The solutions here look good for facebook token authentication, but is there one for google? Google also provides a client side API method for getting an access token:

https://developers.google.com/+/web/signin/javascript-flow

from passport-facebook.

ptz0n avatar ptz0n commented on May 8, 2024

@etler Sure, using the same approach would work fine. See https://developers.google.com/+/web/signin/client-to-server-flow

from passport-facebook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.