Skip to content

Instantly share code, notes, and snippets.

@bendytree
Created April 9, 2020 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendytree/67ec68628f808b88aa3136bb1e2e7f97 to your computer and use it in GitHub Desktop.
Save bendytree/67ec68628f808b88aa3136bb1e2e7f97 to your computer and use it in GitHub Desktop.
import app from 'src/api';
import * as qs from 'query-string';
import axios from 'axios';
const settings = {
appId: '**************',
appSecret: '**************************',
redirectUrl: 'https://myapp.com/facebook/callback',
};
app.get('/facebook/login', async (req, res) => {
const params = qs.stringify({
client_id: settings.appId,
redirect_uri: settings.redirectUrl,
scope: ['email'].join(','),
response_type: 'code',
auth_type: 'rerequest',
});
const url = `https://www.facebook.com/v4.0/dialog/oauth?${params}`;
res.redirect(url);
});
app.get('/facebook/callback', async (req, res) => {
// Get the login code
const { code } = req.query;
// Fetch the access token
const tokenResponse = await axios({
url: 'https://graph.facebook.com/v4.0/oauth/access_token',
method: 'get',
params: {
client_id: settings.appId,
client_secret: settings.appSecret,
redirect_uri: settings.redirectUrl,
code,
},
});
const { access_token } = tokenResponse.data;
// Fetch the email
const meResponse = await axios({
url: 'https://graph.facebook.com/me',
method: 'get',
params: {
fields: ['email'].join(','), // matches scope
access_token,
},
});
const { email } = meResponse.data;
// TODO: login with email
res.redirect('/');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment