Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication using Facebook with Passportjs: what is accessToken for, what should I store after registration?

I am using Passport to register/authenticate using Facebook. When oAuth is successful, I am returned:

  • accessToken
  • refreshToken
  • profile

Now... when a user successfully registers using Facebook, I store accessToken and the profile info. When somebody wants to login, and goes through the oauth motions again, my app once more gets accessToken and profile. Trouble is, accessToken is different. I actually expected the accessToken to be the same after the first authentication...

At this point, I am connecting my own local user with the facebook's id field from the profile. But... how would I actually use accessToken? Does it even make sense to keep it? If so, why would I actually keep it?

I actually expect accessToken to be the same, and use that to match a successful login. I obviously can't do that... so I am confused!

like image 580
Merc Avatar asked Jun 16 '13 16:06

Merc


People also ask

How do I use Facebook token for passport?

So to authenticate an API route using passport-facebook-token, you'll need to set up a passport strategy like so: passport. use('facebook-token', new FacebookTokenStrategy({ clientID : "123-your-app-id", clientSecret : "ssshhhhhhhhh" }, function(accessToken, refreshToken, profile, done) { // console.

What does passport authenticate () do?

In this route, passport. authenticate() is middleware which will authenticate the request. By default, when authentication succeeds, the req. user property is set to the authenticated user, a login session is established, and the next function in the stack is called.

What is a passport token?

This module lets you authenticate using a token in your Node. js applications. It is based on passport-local module by Jared Hanson. By plugging into Passport, token authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.


2 Answers

You need to keep accessToken if you want to query facebook's API on behalf of your logged-in-via-facebook user. If you want to use facebook just for login only, you can discard it. If you want to ask facebook for the user's most recent status update, for example, you need to include that accessToken as a parameter when making that API call. The point of the accessToken is that it allows a set of operations on behalf of a user, but it expires so if it falls into the wrong hands it cannot be used to cause as much damage as a permanent token or the user's actual password. It will be different every time by design.

like image 155
Peter Lyons Avatar answered Oct 18 '22 20:10

Peter Lyons


You should store facebook Id. It should be in the profile object. Access token will change according to facebooks policy of authorization. What you should be doing is

  1. Get the user to login through facebook
  2. Check their facebook id against the facebook id in your database.

Access tokens expire frequently as described here

like image 4
Akshat Jiwan Sharma Avatar answered Oct 18 '22 19:10

Akshat Jiwan Sharma