Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook login to existing user database, and access tokens

Trying to add facebook login to an existing login system on a project I am working on. Built with angular, using the FB JS SDK. This is primarily to allow frictionless login, and not currently that fussed about using the access tokens to make further calls with the FB API.

So as a new user, they hit the FB login, accept permissions etc, and it fires me back an access token etc. The new user is created in my DB, along with the accesstoken, FB userid, etc.

How do I now authenticate the user with the userid and accesstoken now stored in my DB? As far as I can see, the access token changes on virtually every page load / request, so next time the user hits the FB login, or I check the FB login status the only constant thing I have is the userid.

Have done various reading on SO and FB docs eg:

https://developers.facebook.com/docs/facebook-login/web https://developers.facebook.com/docs/facebook-login/multiple-providers https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#checktoken

How should a Facebook user access token be consumed on the server-side?

... although that has only served to confuse things further.

I imagine I would take that stored accesstoken then check its validity, however due to the various instances of access tokens expiring and being invalidated, this also seems like an incomplete solution.

So my question: How do I securely authenticate my FB users with their counterpart user in my own DB?

like image 377
Horse Avatar asked Jan 06 '16 12:01

Horse


People also ask

How do I log into Facebook with authentication?

Tap in the top right of Facebook. Scroll to the bottom and tap Settings, then tap Security and Login. Tap Use two-factor authentication. Enter your password and tap Continue.


1 Answers

The Facebook login request returns user id + short lived access token (client side).

Use the server side Facebook SDK to check the validity of the access token (will return user_id and app_id fields if valid).

You can trust the user_id field returned from the Facebook API to check against your existing user database.

like image 181
James Avatar answered Oct 20 '22 00:10

James