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
26 changes: 18 additions & 8 deletions api/passport/facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,34 @@ const UserController = require('../controllers/user');
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',
passport.authenticate('facebook', {
let domain;
app.get('/login/facebook', (req, res, next) => {
domain = req.headers.referer;
tomivm marked this conversation as resolved.
Show resolved Hide resolved
//if referer is private insert default hostname
if (!domain) {
domain = 'https://app.cboard.io/';
}
return passport.authenticate('facebook', {
session: false,
scope: config.facebook.SCOPE
})
);
scope: config.facebook.SCOPE,
callbackURL: domain + config.facebookCallbackPath
tomivm marked this conversation as resolved.
Show resolved Hide resolved
})(req, res, next);
});

app.get(
'/login/facebook/callback',
passport.authenticate('facebook', { session: false }),
(req, res, next) => {
return passport.authenticate('facebook', {
session: false,
callbackURL: domain + config.facebookCallbackPath
})(req, res, next);
},
(req, res) => {
res.json(req.user);
}
Expand Down
29 changes: 20 additions & 9 deletions api/passport/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ const UserController = require('../controllers/user');

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',
passport.authenticate('google', {
let domain;
app.get('/login/google', (req, res, next) => {
domain = req.headers.referer;
//if referer is private insert default hostname
if (!domain) {
domain = 'https://app.cboard.io/';
}
return passport.authenticate('google', {
session: false,
scope: config.google.SCOPE
})
);
scope: config.google.SCOPE,
callbackURL: domain + config.googleCallbackPath
})(req, res, next);
});

// GET /login/google/callback
// Use passport.authenticate() as route middleware to authenticate the
Expand All @@ -29,7 +34,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) => {
return passport.authenticate('google', {
callbackURL: domain + config.googleCallbackPath,
failureRedirect: '/',
session: false
})(req, res, next);
},
(req, res) => {
res.json(req.user);
}
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