Skip to content
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
Loading