Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@jimmycode/simple-oauth2-facebook
Advanced tools
Readme
This library is a wrapper around Simple OAuth2 Library
Specially made for Authorization Code Flow with Facebook.
Latest Node 8 LTS or newer versions.
npm install --save simple-oauth2 @jimmycode/simple-oauth2-facebook
or
yarn add simple-oauth2 @jimmycode/simple-oauth2-facebook
const simpleOAuth2Facebook = require('@jimmycode/simple-oauth2-facebook');
const facebook = simpleOAuth2Facebook.create(options);
facebook
object exposes 3 keys:
access_token
SimpleOAuth2Facebook comes with default values for most of the options.
Required options
Option | Description |
---|---|
clientId | Your App Id. |
clientSecret | Your App Secret Id. |
callbackURL | Callback configured when you created the app. |
Other options
Option | Default | Description |
---|---|---|
scope | ['email'] | https://developers.facebook.com/docs/facebook-login/permissions |
state | '' | Your CSRF anti-forgery token. More at: https://auth0.com/docs/protocols/oauth2/oauth-state |
returnError | false | When is false (default), will call the next middleware with the error object. When is true, will set req.tokenError to the error, and call the next middleware as if there were no error. |
authorizeHost | 'https://facebook.com' | |
authorizePath | '/dialog/oauth' | |
tokenHost | 'https://graph.facebook.com' | |
tokenPath | '/oauth/access_token' | |
authorizeOptions | {} | Pass extra parameters when requesting authorization. |
tokenOptions | {} | Pass extra parameters when requesting access_token. |
const oauth2 = require('simple-oauth2').create({
client: {
id: process.env.FACEBOOK_APP_ID,
secret: process.env.FACEBOOK_APP_SECRET
},
auth: {
authorizeHost: 'https://facebook.com/',
authorizePath: '/dialog/oauth',
tokenHost: 'https://graph.facebook.com',
tokenPath: '/oauth/access_token'
}
});
router.get('/auth/facebook', (req, res) => {
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: 'http://localhost:3000/auth/facebook/callback',
scope: ['email']
});
res.redirect(authorizationUri);
});
router.get('/auth/facebook/callback', async(req, res) => {
const code = req.query.code;
const options = {
code,
redirect_uri: 'http://localhost:3000/auth/facebook/callback'
};
try {
// The resulting token.
const result = await oauth2.authorizationCode.getToken(options);
// Exchange for the access token.
const token = oauth2.accessToken.create(result);
return res.status(200).json(token);
} catch (error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});
const simpleOAuth2Facebook = require('@jimmycode/simple-oauth2-facebook');
const facebook = simpleOAuth2Facebook.create({
clientId: process.env.FACEBOOK_APP_ID,
clientSecret: process.env.FACEBOOK_APP_SECRET,
callbackURL: 'http://localhost:3000/auth/facebook/callback'
});
// Ask the user to authorize.
router.get('/auth/facebook', facebook.authorize);
// Exchange the token for the access token.
router.get('/auth/facebook/callback', facebook.accessToken, (req, res) => {
return res.status(200).json(req.token);
});
FAQs
A simple Node.js client library for Facebook OAuth2
The npm package @jimmycode/simple-oauth2-facebook receives a total of 4 weekly downloads. As such, @jimmycode/simple-oauth2-facebook popularity was classified as not popular.
We found that @jimmycode/simple-oauth2-facebook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.