Created
April 9, 2020 03:26
-
-
Save bendytree/67ec68628f808b88aa3136bb1e2e7f97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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