-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dionyziz/auth
Auth
- Loading branch information
Showing
7 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
GITHUB_CLIENT_ID=yourid | ||
GITHUB_CLIENT_SECRET=yourtoke | ||
GITHUB_CALLBACK_URL=http://localhost:3000/auth/github/callback | ||
DB_URI=postgres://root@localhost/blockchain_course |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const GitHubStrategy = require('passport-github') | ||
|
||
const strategy = new GitHubStrategy({ | ||
clientID: process.env.GITHUB_CLIENT_ID, | ||
clientSecret: process.env.GITHUB_CLIENT_SECRET, | ||
callbackURL: process.env.GITHUB_CALLBACK_URL | ||
}, | ||
(authToken, refreshToken, profile, cb) => { | ||
/* | ||
console.log('GitHub profile:') | ||
console.log('===============') | ||
console.log(profile) | ||
console.log('===============') | ||
*/ | ||
/* | ||
User.findOrCreate({githubId: profile.id}, (err, user) => { | ||
return cb(err, user) | ||
}) | ||
*/ | ||
} | ||
) | ||
|
||
module.exports = strategy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const passport = require('passport') | ||
const GitHubStrategy = require('./github') | ||
|
||
passport.serializeUser((user, done) => { | ||
console.log('Serializing user:') | ||
console.log(user) | ||
done(null, {_id: user._id}) | ||
}) | ||
|
||
passport.deserializeUser((id, done) => { | ||
console.log('Deserializing user ', id) | ||
}) | ||
|
||
passport.use(GitHubStrategy) | ||
|
||
module.exports = passport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const express = require('express') | ||
const passport = require('passport') | ||
const router = express.Router() | ||
|
||
router.get( | ||
'/github', | ||
passport.authenticate('github', {scope: ['profile']}) | ||
) | ||
router.get( | ||
'/github/callback', | ||
passport.authenticate( | ||
'github', | ||
{ | ||
successRedirect: '/', | ||
failureRedirect: '/login' | ||
} | ||
) | ||
) | ||
router.post('/logout', (req, res) => { | ||
if (req.user) { | ||
console.log('Logging out user') | ||
req.session.destroy() | ||
res.clearCookie('connect.sid') | ||
return res.json({msg: 'Logged out'}) | ||
} | ||
return res.json({msg: 'You are not logged in'}) | ||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters