Skip to content

Commit

Permalink
feat(socialAuth): Setup social authentication
Browse files Browse the repository at this point in the history
  - setup passport for facebook authentication
  - setup passport for google authentication

[#164139772]
  • Loading branch information
Black Developa committed Apr 3, 2019
1 parent 3320b68 commit 2104031
Show file tree
Hide file tree
Showing 9 changed files with 983 additions and 1,391 deletions.
2,244 changes: 885 additions & 1,359 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
"joi": "14.3.1",
"jsonwebtoken": "8.5.0",
"morgan": "1.9.1",
"nock": "10.0.6",
"passport": "0.4.0",
"passport-facebook": "3.0.0",
"passport-google-oauth20": "2.0.0",
"pg": "7.8.1",
"pg-hstore": "2.3.2",
"sequelize": "4.43.0",
Expand Down
40 changes: 40 additions & 0 deletions server/config/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import passport from 'passport';
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import env from './env-config';
import { socialAuthCallback } from '../utils/helpers';

const setUpPassport = () => {
passport.use(
new GoogleStrategy(
{
clientID: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
callbackURL: 'http://localhost:3000/api/auth/google/callback'
},
socialAuthCallback
)
);

passport.use(
new FacebookStrategy(
{
clientID: env.FACEBOOK_CLIENT_ID,
clientSecret: env.FACEBOOK_CLIENT_SECRET,
callbackURL: 'http://localhost:3000/api/auth/facebook/callback',
profileFields: ['id', 'name', 'displayName', 'email', 'photos']
},
socialAuthCallback
)
);

passport.serializeUser((user, done) => {
done(null, user);
});

passport.deserializeUser((obj, done) => {
done(null, obj);
});
};

export default setUpPassport;
32 changes: 2 additions & 30 deletions server/config/env-config.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
import dotenv from 'dotenv';
import 'dotenv/config';

dotenv.config();
const { env } = process;
const {
DATABASE_URL,
NODE_ENV,
PORT,
SECRET,
SENDGRID_KEY,
ADMIN_EMAIL,
SITE_NAME,
HEROKU_APP_NAME,
UI_CLIENT_HOST,
API_SERVER_HOST,
AUTH_TOKEN_EXPIRY,
VERIFICATION_LINK_EXPIRY
} = env;

export default {
DATABASE_URL,
NODE_ENV,
PORT,
SECRET,
SENDGRID_KEY,
ADMIN_EMAIL,
SITE_NAME,
HEROKU_APP_NAME,
UI_CLIENT_HOST,
API_SERVER_HOST,
AUTH_TOKEN_EXPIRY,
VERIFICATION_LINK_EXPIRY
};
export default env;
Empty file.
13 changes: 11 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import morgan from 'morgan';
import debug from 'debug';
import passport from 'passport';
import session from 'express-session';
import passportSetUp from './config/auth';
import env from './config/env-config';
import routes from './routes/index';

const app = express();
const logger = debug('vale-ah::server: ');

passportSetUp();
app.use(passport.initialize());
app.use(passport.session());

app.use(cors());
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(passport.initialize());

app.use(
session({
Expand All @@ -27,8 +31,13 @@ app.use(
})
);

app.get('/', (req, res) => {
return res.status(200).json({
status: 200,
message: 'home'
});
});
routes(app);

app.listen(env.PORT, () => {
logger(`Listening on port ${env.PORT}`);
});
Expand Down
2 changes: 2 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import authRoutes from './auth-routes';
import userRoutes from './user-routes';
import recipeRoutes from './recipe-routes';
import profileRoutes from './profile-routes';
import socialAuth from './social-routes';
import { errorResponse } from '../utils/helpers';

const router = app => {
Expand All @@ -12,6 +13,7 @@ const router = app => {
app.use('/api/user', userRoutes);
app.use('/api/recipes', recipeRoutes);
app.use('/api/profiles', profileRoutes);
app.use('/api/auth', socialAuth);
app.use('*', (req, res) =>
errorResponse(res, 'The requested resource was not found', 404)
);
Expand Down
31 changes: 31 additions & 0 deletions server/routes/social-routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Router } from 'express';
import passport from 'passport';
import debug from 'debug';

const router = new Router();

const logger = debug('vale-ah::socialAuth: ');

router.get(
'/login/facebook',
passport.authenticate('facebook', { scope: ['email'] })
);
router.get(
'/facebook/callback',
passport.authenticate('facebook'),
(req, res) => {
logger(req.user);
res.redirect('/');
}
);

router.get(
'/login/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
router.get('/google/callback', passport.authenticate('google'), (req, res) => {
logger(req.user);
res.redirect('/');
});

export default router;
9 changes: 9 additions & 0 deletions server/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ export const rowArrayToObjectList = sequelizeRowArray =>
*/
export const randomHex = () =>
Math.floor(Math.random() * Date.now()).toString(16);

export const socialAuthCallback = (
accessToken,
refreshToken,
profile,
done
) => {
done(null, profile);
};

0 comments on commit 2104031

Please sign in to comment.