Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.