Skip to content

Instantly share code, notes, and snippets.

@magicien
Last active September 13, 2017 20:23
Show Gist options
  • Save magicien/72452907d32029c5fe13a398e0002aec to your computer and use it in GitHub Desktop.
Save magicien/72452907d32029c5fe13a398e0002aec to your computer and use it in GitHub Desktop.
Identifying Users for GitHub Apps at Heroku
const express = require('express')
const session = require('express-session')
const passport = require('passport')
const GitHubStrategy = require('passport-github2').Strategy
const clientId = process.env.GITHUB_APP_CLIENT_ID
const clientSecret = process.env.GITHUB_APP_CLIENT_SECRET
const domainName = process.env.DOMAIN_NAME
const app = express()
passport.use(new GitHubStrategy({
clientID: clientId,
clientSecret: clientSecret,
state: true
}, (accessToken, refreshToken, profile, done) => {
process.accessToken = accessToken
process.nextTick(() => {
return done(null, profile)
})
}))
passport.serializeUser((user, done) => {
done(null, user)
})
passport.deserializeUser((obj, done) => {
done(null, obj)
})
app.use(session({
name: 'AppSessionID',
secret: 'IT\'S A SECRET TO EVERYBODY',
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: 'auto',
proxy: true,
domain: domainName,
maxAge: 1000 * 60 * 30 // 30min
}
}))
app.use(passport.initialize())
app.use(passport.session())
// needslogin => GitHub => applogin
app.get('/needslogin', passport.authenticate('github'))
app.get('/applogin', passport.authenticate('github', {
failureRedirect: '/',
successRedirect: '/'
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment