Skip to content

Commit

Permalink
add facebook login functionality and remove fb secret key
Browse files Browse the repository at this point in the history
  • Loading branch information
ellenlee authored and Carrot7712 committed Sep 10, 2020
1 parent cbada51 commit 0780cbe
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FACEBOOK_ID=SKIP
FACEBOOK_SECRET=SKIP
FACEBOOK_CALLBACK=http://localhost:3000/auth/facebook/callback
SESSION_SECRET=ThisIsMySecret
PORT=3000
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ Desktop.ini
# npm
node_modules
*.log
*.gz
*.gz

.env
8 changes: 6 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const flash = require('connect-flash')

if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}

const routes = require('./routes')

const usePassport = require('./config/passport')

const app = express()
const PORT = 3000
const PORT = process.env.PORT

app.engine('hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }))
app.set('view engine', 'hbs')

app.use(session({
secret: 'ThisIsMySecret',
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true
}))
Expand Down
28 changes: 28 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const FacebookStrategy = require('passport-facebook').Strategy
const bcrypt = require('bcryptjs')

const db = require('../models')
Expand All @@ -25,6 +26,33 @@ module.exports = app => {
.catch(err => done(err, false))
}))

passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_ID,
clientSecret: process.env.FACEBOOK_SECRET,
callbackURL: process.env.FACEBOOK_CALLBACK,
profileFields: ['email', 'displayName']
},
(accessToken, refreshToken, profile, done) => {
const { name, email } = profile._json
User.findOne({ where: { email } })
.then(user => {
if (user) return done(null, user)

const randomPassword = Math.random().toString(36).slice(-8)
bcrypt
.genSalt(10)
.then(salt => bcrypt.hash(randomPassword, salt))
.then(hash => User.create({
name,
email,
password: hash
}))
.then(user => done(null, user))
.catch(err => done(err, false))
})
}
))

passport.serializeUser((user, done) => {
done(null, user.id)
})
Expand Down
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
"dependencies": {
"bcryptjs": "^2.4.3",
"connect-flash": "^0.1.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-handlebars": "^4.0.4",
"express-session": "^1.17.1",
"method-override": "^3.0.0",
"mysql2": "^2.1.0",
"passport": "^0.4.1",
"passport-facebook": "^3.0.0",
"passport-local": "^1.0.0",
"sequelize": "^5.21.13",
"sequelize-cli": "^5.5.1"
Expand Down
2 changes: 2 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const router = express.Router()
const home = require('./modules/home')
const users = require('./modules/users')
const todos = require('./modules/todos')
const auth = require('./modules/auth')

const { authenticator } = require('../middleware/auth')

router.use('/todos', authenticator, todos)
router.use('/users', users)
router.use('/auth', auth)
router.use('/', authenticator, home)

module.exports = router
15 changes: 15 additions & 0 deletions routes/modules/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require('express')
const router = express.Router()

const passport = require('passport')

router.get('/facebook', passport.authenticate('facebook', {
scope: ['email', 'public_profile']
}))

router.get('/facebook/callback', passport.authenticate('facebook', {
successRedirect: '/',
failureRedirect: 'users/login'
}))

module.exports = router

0 comments on commit 0780cbe

Please sign in to comment.