-
-
Notifications
You must be signed in to change notification settings - Fork 58
Thirdy Party Authentication with passport #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| PORT=3000 | ||
| PORT=4000 | ||
| NODE_ENV=development | ||
| JWT_SECRET=thisismysupersecrettokenjustkidding | ||
| DATABASE_URL=mongodb://localhost:27017/donut-development |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,38 @@ | ||
| var express = require('express') | ||
| var router = express.Router() | ||
| const passport = require('passport') | ||
| const authPassport = require('../../config/authPassport'); | ||
| const documentationUrl = 'https://documenter.getpostman.com/view/1159934/SWDze1Rp' | ||
|
|
||
| authPassport(passport); | ||
|
|
||
| /* GET home page. */ | ||
| router.get('/', function (req, res, next) { | ||
| res.redirect(documentationUrl) | ||
| }) | ||
|
|
||
| router.get('/auth/google', passport.authenticate('google', { | ||
| scope: ['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'] | ||
| })); | ||
|
|
||
| router.get('/auth/google/callback', | ||
| passport.authenticate('google', { | ||
| failureRedirect: 'http://localhost:3000' | ||
| }), | ||
| (req, res) => { | ||
| res.redirect('http://localhost:3000/dashboard'); | ||
| } | ||
| ); | ||
|
|
||
| router.get('/auth/github', | ||
| passport.authenticate('github', { scope: [ 'user:email' ] })); | ||
|
|
||
| router.get('/auth/github/callback', | ||
| passport.authenticate('github', { | ||
| failureRedirect: 'http://localhost:3000' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. . |
||
| }), | ||
| (req, res) => { | ||
| res.redirect('http://localhost:3000/dashboard'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. . |
||
| }); | ||
|
|
||
| module.exports = router | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy; | ||
| const GitHubStrategy = require('passport-github2').Strategy; | ||
| const configAuth = require('./authTokens'); | ||
| const User = require('../app/models/User') | ||
|
|
||
|
|
||
| module.exports = function(passport) { | ||
| passport.serializeUser(function(user, done) { | ||
| done(null, user.id); | ||
| }); | ||
|
|
||
| passport.deserializeUser(function(id, done) { | ||
| User.findById(id, function(err, user) { | ||
| done(err, user); | ||
| }); | ||
| }); | ||
|
|
||
| passport.use(new GoogleStrategy({ | ||
| clientID : configAuth.googleAuth.clientID, | ||
| clientSecret : configAuth.googleAuth.clientSecret, | ||
| callbackURL : configAuth.googleAuth.callbackURL, | ||
| }, | ||
| function(accessToken, refreshToken, profile, done) { | ||
| User.findOne({email: profile.emails[0].value}, async function(err,user){ | ||
| if(!user){ | ||
| const user = new User(); | ||
| user.name = profile.displayName; | ||
| user.provider = profile.provider; | ||
| user.providerID = profile.id; | ||
| user.email = profile.emails[0].value; | ||
| user.save() | ||
| } | ||
| return done(err, user); | ||
| }) | ||
| } | ||
| )); | ||
| passport.use(new GitHubStrategy({ | ||
| clientID : configAuth.githubAuth.clientID, | ||
| clientSecret : configAuth.githubAuth.clientSecret, | ||
| callbackURL : configAuth.githubAuth.callbackURL, | ||
| }, | ||
| async function(accessToken, refreshToken, profile, done) { | ||
| User.findOne({email: profile.emails[0].value}, async function(err,user){ | ||
| if(!user){ | ||
| const user = new User(); | ||
| user.name = profile.displayName; | ||
| user.provider = profile.provider; | ||
| user.providerID = profile.id; | ||
| user.email = profile.emails[0].value; | ||
| user.save() | ||
| } | ||
| return done(err, user); | ||
| }) | ||
| } | ||
| )); | ||
|
|
||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module.exports = { | ||
| 'githubAuth' : { | ||
| 'clientID' : process.env.GITHUBCLIENTID, | ||
| 'clientSecret' : process.env.GITHUBSECRET, | ||
| 'callbackURL' : 'http://localhost:4000/auth/github/callback' | ||
| }, | ||
| 'googleAuth' : { | ||
| 'clientID' : process.env.GOOGLECLIENTID, | ||
| 'clientSecret' : process.env.GOOGLECLIENTSECRET, | ||
| 'callbackURL' : 'http://localhost:4000/auth/google/callback' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. . |
||
| } | ||
| }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. . |
||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.