• Make Over Arena

Selling Things On Facebook - Sell Items On Facebook Group

Selling Things On Facebook – Facebook is fast becoming one of the most powerful online marketing platforms for promoting businesses, cond...

Facebook api login example

 No comments   

Summary

I want that my WebApp authenticate the user with facebook. Once it is done, the WebApp will send the facebookToken to the api, and thanks to that the api will be able to have access to facebook user infomation. And we will be able to create a JsonWebToken to authenticate our REST api.
You can find the source code in github : https://github.com/Tkanos/FacebookRestAuthentication

Code

You can find the source code in github : https://github.com/Tkanos/FacebookRestAuthentication
So we need to have an web app that connect on facebook and receive the facebooktoken.
In our API, we will need to have a Login endpoint to receive this facebooktoken , sent by the web app.
var getToken = function(req, res) {
var facebookToken = req.headers['facebooktoken'];
//TODO : check the expirationdate of facebooktoken
if(facebookToken) {
var path = 'https://graph.facebook.com/me?access_token=' + facebookToken;
request(path, function (error, response, body) {
var facebookUserData = JSON.parse(body);
if (!error && response && response.statusCode && response.statusCode == 200) {
if(facebookUserData && facebookUserData.id) {
var accessToken = jsonWebToken.sign(facebookUserData, jwtSecret, {
//Set the expiration
expiresIn: 86400
});
res.status(200).send(accessToken);
} else {
res.status(403);
res.send('Access Forbidden');
}
}
else {
console.log(facebookUserData.error);
//console.log(response);
res.status(500);
res.send('Access Forbidden');
}
});
res.status(403);
res.send('Access Forbidden');
}
};
As you can see receiving the facebookToken we get all the information the user share with us calling :
'https://graph.facebook.com/me?access_token=' + facebookToken;
Once we have the answer, that is all information of our user on Json. We can “Sign” it on jsonwebtoken (eventually save it in our database). and send back our new token that will be use to communicate with our api.

Be careful, for security purpose sign a jsonwebtoken is not enough, you will also need to encrypt it. On this example we only sign, but in your own code don’t forget to encrypt.

As you can see on API/apiRouter.js (see on github), we will get the jsonwebtoken on every communication, we have “verified” it in order to have our user information back in json.
apiRouter.use('/', function(req, res, next){
var jwt = req.headers.authorization;
if(jwt) {
var jwtManager = require('./Login/loginController')();
jwtManager.verify(accessToken, function(err, decoded){
if(err) {
res.status(401).send('Invalid Token');
}
else {
req.connectedUser = decoded;
next();
}
});
}
else {
next();
}
});
So in all methods of my application I will potentially have the userconnected attached to the request.
req.connectedUser

How to test this Api

To test we will need to have a facebook Token. You can get one for test on https://developers.facebook.com/tools/explorer/?method=GET&path=me&version=v2.7
Then (after have done a npm start), you can use a REST CALL to our Login service. put your facebooktoken on the header, you will receive the jsonwebtoken on the response:

Link

  • https://github.com/Tkanos/FacebookRestAuthentication
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Blog Archive

  • ►  2019 (502)
    • ►  July (5)
      • ►  Jul 08 (2)
      • ►  Jul 07 (2)
      • ►  Jul 06 (1)
    • ►  February (368)
      • ►  Feb 12 (33)
      • ►  Feb 11 (21)
      • ►  Feb 09 (37)
      • ►  Feb 08 (40)
      • ►  Feb 07 (45)
      • ►  Feb 06 (40)
      • ►  Feb 05 (23)
      • ►  Feb 04 (49)
      • ►  Feb 02 (25)
      • ►  Feb 01 (55)
    • ►  January (129)
      • ►  Jan 31 (78)
      • ►  Jan 30 (20)
      • ►  Jan 29 (11)
      • ►  Jan 20 (4)
      • ►  Jan 19 (3)
      • ►  Jan 18 (4)
      • ►  Jan 17 (5)
      • ►  Jan 16 (1)
      • ►  Jan 12 (1)
      • ►  Jan 11 (1)
      • ►  Jan 10 (1)
  • ▼  2018 (2465)
    • ►  November (64)
      • ►  Nov 25 (4)
      • ►  Nov 24 (4)
      • ►  Nov 23 (7)
      • ►  Nov 22 (9)
      • ►  Nov 21 (11)
      • ►  Nov 19 (14)
      • ►  Nov 15 (7)
      • ►  Nov 03 (6)
      • ►  Nov 01 (2)
    • ►  October (178)
      • ►  Oct 31 (9)
      • ►  Oct 30 (1)
      • ►  Oct 26 (1)
      • ►  Oct 24 (16)
      • ►  Oct 23 (21)
      • ►  Oct 22 (26)
      • ►  Oct 19 (5)
      • ►  Oct 16 (50)
      • ►  Oct 15 (36)
      • ►  Oct 14 (12)
      • ►  Oct 13 (1)
    • ▼  September (771)
      • ►  Sep 27 (58)
      • ►  Sep 26 (50)
      • ►  Sep 25 (70)
      • ►  Sep 24 (70)
      • ►  Sep 20 (50)
      • ►  Sep 19 (80)
      • ►  Sep 18 (50)
      • ►  Sep 17 (43)
      • ▼  Sep 14 (81)
        • How can you see who has looked at your facebook
        • How do you delete likes on instagram
        • How to find facebook friends on twitter
        • How can you add gifs to facebook
        • How to see sent request on facebook
        • How do i view archived messages on messenger app
        • How to deactivate fb account without login
        • How to do memories on snapchat
        • How to delete an option on a facebook poll
        • How to post multiple pics on facebook
        • How to post on instagram pc
        • How to check who checked my facebook
        • How to go to non mobile facebook site
        • How to unblock facebook group
        • How to deactivate my fb
        • How to remove my fb account
        • How to get your instagram password
        • How to Change Video to gif
        • How to access full facebook site on phone (Android)
        • My snapchat memories
        • How to download your facebook videos
        • Delete facebook account in mobile
        • Facebook profile pic app: The 9 Best Apps and Site...
        • How to change name of group on facebook
        • How to temporarily deactivate your facebook account
        • I forgot my email address password for Gmail
        • How to block people from seeing your posts on face...
        • How to read anyone's facebook messages
        • How to allow google to use my location
        • How to get free followers on instagram for free
        • Sign in to instagram via facebook
        • I can t get into my facebook: What to Do
        • How to verify instagram without email
        • How to edit a poll on facebook
        • Someone else is using my phone number on facebook
        • How to hide friends list in fb
        • Facebook messenger for iphone 4
        • Facebook app update problems
        • How does facebook come up with friend suggestions
        • Find someone on facebook with mobile number
        • Will someone know if you block them on facebook
        • Facebook api login example
        • Gifs to share on facebook
        • How can I get back my deleted pictures
        • How to save all facebook photos at once
        • Best hotel sites: The 10 Best Hotel Booking Sites
        • Fb sent requests: See who left your friend request...
        • How to logout of all facebook accounts
        • How to get other languages on iphone
        • How to contact facebook management
        • How do you unblock on facebook
        • How to add multiple photos to facebook post
        • How to view blocked messages iphone
        • Will someone know if i block them on facebook
        • How to Install fb messenger for android
        • How do i add a link to my instagram profile
        • How to upload gif to twitter and Facebook
        • What to do when you forget your facebook password
        • How do i connect my facebook to twitter
        • Unfriend tracker: Find out Who Unfriended You on F...
        • How to download Samsung messenger app
        • How to find person on facebook with phone number
        • How to use messenger without facebook
        • How to delete all your followers on instagram
        • How to See hidden facebook photos
        • How to download video from facebook using android
        • How can you permanently delete facebook
        • How can I delete instagram photos
        • How To link instagram to facebook
        • How to recover Fb disabled account
        • Facebook: If I unfollow will they know
        • How to add new admin to facebook page
        • Facebook remove admin notification
        • Download Multiple Facebook 2 Ios
        • How to hack fb account without knowing the password
        • How can I make my facebook friends list private
        • How to turn off facebook notifications on iphone
        • How to change your facebook name on mobile
        • How to undelete a facebook message
        • How to check email at yahoo
        • How to delete my facebook link
      • ►  Sep 13 (39)
      • ►  Sep 12 (101)
      • ►  Sep 11 (69)
      • ►  Sep 10 (9)
      • ►  Sep 09 (1)
    • ►  June (215)
      • ►  Jun 07 (13)
      • ►  Jun 06 (5)
      • ►  Jun 05 (48)
      • ►  Jun 04 (49)
      • ►  Jun 03 (50)
      • ►  Jun 02 (50)
    • ►  May (1205)
      • ►  May 30 (50)
      • ►  May 29 (50)
      • ►  May 28 (43)
      • ►  May 26 (50)
      • ►  May 25 (50)
      • ►  May 24 (50)
      • ►  May 23 (50)
      • ►  May 21 (50)
      • ►  May 20 (100)
      • ►  May 18 (92)
      • ►  May 17 (98)
      • ►  May 16 (93)
      • ►  May 15 (25)
      • ►  May 14 (25)
      • ►  May 13 (90)
      • ►  May 12 (25)
      • ►  May 11 (50)
      • ►  May 09 (25)
      • ►  May 08 (25)
      • ►  May 07 (32)
      • ►  May 06 (21)
      • ►  May 05 (18)
      • ►  May 04 (25)
      • ►  May 03 (18)
      • ►  May 02 (25)
      • ►  May 01 (25)
    • ►  April (26)
      • ►  Apr 12 (25)
      • ►  Apr 10 (1)
    • ►  March (6)
      • ►  Mar 26 (6)

Search

Copyright © Make Over Arena