Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom JWT token #86

Open
brenwell opened this issue Jan 17, 2018 · 2 comments
Open

Custom JWT token #86

brenwell opened this issue Jan 17, 2018 · 2 comments

Comments

@brenwell
Copy link

brenwell commented Jan 17, 2018

Hey there guys. First thanks for the awesome library!

I was wondering if you had the time to push me in right direction. I would like user's to be able to login via the FB SDK and then send the accessToken to my server for authentication. Which works. But if I understood it correctly the FB accessToken is then used as the token to authorize every other call. I would like to have more than just FB login so I think I need a standard JWT token to use across all strategies. The question is how can I do this?

this is what I have so far

passport.use(new FacebookTokenStrategy(opts,
    function(accessToken, refreshToken, profile, done) {
        const user = {
            name: profile.displayName,
            email: profile.emails[0].value,
            facebookId: profile.id,
            facebookEmail: profile.emails[0].value,
            facebookUsername: profile.displayName,
            facebookFirstName: profile.name.givenName,
            facebookLastName: profile.name.familyName,
        }

        getOrCreateUser(user, (err, resp) => {
            const userWithToken = {
                ...profile,
                token: Token(user)
            }
            done(err, userWithToken)
        })

    }))

passport.serializeUser(function(user, done) {
    console.log('serializeUser',user)
    done(null, user._id);
});

passport.deserializeUser(function(id, done) {
    console.log('deserializeUser',id)
    getUserById(id, done)
});

auth.get('/facebook/token',
    passport.authenticate('facebook-token', { session: false }),
    function (req, res) {
        if (req.user)
        {
            res.status(200).json( {
                success : true,
                message : "User logged in",
                token: req.user.token,
                user: req.user
            });
        }
        else
        {
            res.status(401).json( {
                success : false,
                message : "User not logged in",
            });
        }
    }
);

The client would then send a request with accessToken and it will swap that for my custom JWT in all future requests.

I hope that made sense and thanks for any help

@nabati
Copy link

nabati commented Nov 2, 2018

Not sure what the question is here really, but this sounds like the correct approach;

The client would then send a request with accessToken and it will swap that for my custom JWT in all future requests.

@kanodianeha
Copy link

I have a similar problem, is there a way I can use this library at server end along with other login mechanism like Twitter. For that, if I could send in a JWT token along with each request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants