Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic callback for Google and Facebook login #181

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
18 changes: 12 additions & 6 deletions api/passport/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,33 @@ const { Strategy: FacebookStrategy } = require('passport-facebook');
const config = require('../../config');
const UserController = require('../controllers/user');

const urljoin = require('url-join');

const FBStrategy = {
clientID: process.env.FACEBOOK_APP_ID,
clientSecret: process.env.FACEBOOK_APP_SECRET,
callbackURL: process.env.FACEBOOK_CALLBACK_URL,
profileFields: ['id', 'emails', 'name', 'displayName', 'gender', 'picture']
};

passport.use(new FacebookStrategy(FBStrategy, UserController.facebookLogin));

const configureFacebookStrategy = app => {
app.get(
'/login/facebook',
app.get('/login/facebook', (req, res, next) =>
passport.authenticate('facebook', {
session: false,
scope: config.facebook.SCOPE
})
scope: config.facebook.SCOPE,
callbackURL: urljoin(req.domain, config.facebookCallbackPath)
})(req, res, next)
);

app.get(
'/login/facebook/callback',
passport.authenticate('facebook', { session: false }),
(req, res, next) =>
passport.authenticate('facebook', {
session: false,
callbackURL: urljoin(req.domain, config.facebookCallbackPath)
})(req, res, next)
,
(req, res) => {
res.json(req.user);
}
Expand Down
21 changes: 14 additions & 7 deletions api/passport/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ const { OAuth2Strategy: GoogleStrategy } = require('passport-google-oauth');
const config = require('../../config');
const UserController = require('../controllers/user');

const urljoin = require('url-join');

const GoogleStrategyConfig = {
clientID: config.google.APP_ID,
clientSecret: config.google.APP_SECRET,
callbackURL: config.google.CALLBACK_URL
clientSecret: config.google.APP_SECRET
};

passport.use(
new GoogleStrategy(GoogleStrategyConfig, UserController.googleLogin)
);

const configureGoogleStrategy = app => {
app.get(
'/login/google',
app.get('/login/google', (req, res, next) =>
passport.authenticate('google', {
session: false,
scope: config.google.SCOPE
})
scope: config.google.SCOPE,
callbackURL: urljoin(req.domain, config.googleCallbackPath)
})(req, res, next)
);

// GET /login/google/callback
Expand All @@ -29,7 +30,13 @@ const configureGoogleStrategy = app => {
// which, in this example, will redirect the user to the home page.
app.get(
'/login/google/callback',
passport.authenticate('google', { failureRedirect: '/', session: false }),
(req, res, next) =>
passport.authenticate('google', {
callbackURL: urljoin(req.domain, config.googleCallbackPath),
failureRedirect: '/',
session: false
})(req, res, next)
,
(req, res) => {
res.json(req.user);
}
Expand Down
5 changes: 5 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ swaggerTools.initializeMiddleware(swaggerConfig, async function (middleware) {

app.use(middleware.swaggerUi());

app.use((req, res, next) => {
req.domain = req.headers.referer || 'https://app.cboard.io/';
next();
});

Facebook.configureFacebookStrategy(app);
Google.configureGoogleStrategy(app);
GoogleToken.configureGoogleTokenStrategy(app);
Expand Down
4 changes: 3 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const production = require('./env/production');

const defaults = {
host: process.env.HOST || 'mongodb',
port: process.env.PORT || 8100
port: process.env.PORT || 8100,
googleCallbackPath: 'login/google/callback',
facebookCallbackPath: 'login/facebook/callback'
};

function getConfig() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"swagger-express-mw": "^0.7.0",
"swagger-tools": "^0.10.4",
"translate-api": "^0.3.18",
"url-join": "^4.0.1",
tomivm marked this conversation as resolved.
Show resolved Hide resolved
"uuid": "^3.4.0",
"validator": "^13.6.0",
"yamljs": "^0.3.0",
Expand Down
Loading