Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit 30b2fe1

Browse files
committed
Added passport
Added passport.js and strategies for auth. Note: The code for auth is not tested it is a dump from passport js README and other doc sites. TODO: Will be adding correct auth methods and test if they are working
1 parent 40a77ec commit 30b2fe1

File tree

6 files changed

+212
-1
lines changed

6 files changed

+212
-1
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@
3030
"lodash": "^4.17.10",
3131
"morgan": "^1.9.1",
3232
"passport": "^0.4.0",
33+
"passport-facebook": "^3.0.0",
34+
"passport-github": "^1.1.0",
35+
"passport-google-oauth": "^2.0.0",
3336
"passport-jwt": "^4.0.0",
37+
"passport-linkedin": "^1.0.0",
38+
"passport-local": "^1.0.0",
39+
"passport-twitter": "^1.0.4",
3440
"pg": "^7.12.1",
3541
"pg-hstore": "^2.3.3",
3642
"sequelize": "^5.18.4",

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import schema from './schema'
1111
import resolvers from './resolvers'
1212
import models, { sequelize } from './models/index'
1313
import loaders from './loaders'
14+
import addAuthRoutes from './resolvers/authentication'
1415

1516
const app = express()
1617

1718
app.use(cors())
1819

1920
app.use(morgan('dev'))
2021

22+
addAuthRoutes(app)
23+
2124
const getLoggedInUser = async req => {
2225
const token = req.headers['x-token']
2326

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import passport from 'passport'
2+
import { Strategy as GitHubStrategy } from 'passport-github'
3+
import { User } from "../../models";
4+
5+
const {
6+
GITHUB_CLIENT_ID,
7+
GITHUB_CLIENT_SECRET
8+
} = process.env
9+
10+
const GIT_CONFIG = {
11+
clientID: GITHUB_CLIENT_ID,
12+
clientSecret: GITHUB_CLIENT_SECRET,
13+
callbackURL: "http://127.0.0.1:8000/auth/github/callback"
14+
}
15+
16+
passport.use(
17+
new GitHubStrategy(GIT_CONFIG, (accessToken, refreshToken, profile, cb) => {
18+
User.findOrCreate({ githubId: profile.id }, function(err, user) {
19+
return cb(err, user);
20+
});
21+
})
22+
);

src/resolvers/authentication/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import passport from 'passport'
2+
3+
import signUpUserLocal from './local'
4+
5+
const signUp = {
6+
local: signUpUserLocal
7+
}
8+
9+
export default (app) => {
10+
app.get(
11+
'/auth/:strategy',
12+
(req, res) => signUp[req.params.strategy](req, res)
13+
)
14+
15+
app.post(
16+
'/auth/:strategy/callback',
17+
(req, res) => passport.authenticate(
18+
req.params.strategy
19+
)(req, res)
20+
)
21+
}

src/resolvers/authentication/local.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import passport from 'passport'
2+
import { Strategy as LocalStrategy } from 'passport-local'
3+
import { User } from '../../models'
4+
5+
passport.use(
6+
new LocalStrategy(
7+
{
8+
usernameField: "email",
9+
passwordField: "password"
10+
},
11+
(email, password, done) => {
12+
console.log({email, password})
13+
User.findOne({ where: { email } }, (err, user) => {
14+
if (err) {
15+
return done(err)
16+
}
17+
if (!user) {
18+
return done(null, false, { message: "We couldn't find any profile associated with that email." })
19+
}
20+
if (!user.validPassword(password)) {
21+
return done(null, false, { message: "Incorrect password." })
22+
}
23+
return done(null, user)
24+
})
25+
}
26+
)
27+
)
28+
29+
const signUpUserLocal = (req, res) => {
30+
const {
31+
body: {
32+
email,
33+
firstName,
34+
lastName,
35+
password
36+
},
37+
params: {
38+
strategy
39+
}
40+
} = req
41+
User.findOrCreate({
42+
where: {
43+
email
44+
},
45+
defaults: {
46+
firstName,
47+
lastName,
48+
password
49+
}
50+
})
51+
res.redirect(`/auth/${strategy}`)
52+
}
53+
54+
export default signUpUserLocal

yarn.lock

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,11 @@ balanced-match@^1.0.0:
12531253
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
12541254
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=
12551255

1256+
1257+
version "3.0.1"
1258+
resolved "https://registry.yarnpkg.com/base64url/-/base64url-3.0.1.tgz#6399d572e2bc3f90a9a8b22d5dbb0a32d33f788d"
1259+
integrity sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A==
1260+
12561261
base@^0.11.1:
12571262
version "0.11.2"
12581263
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
@@ -3751,6 +3756,11 @@ number-is-nan@^1.0.0:
37513756
resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d"
37523757
integrity sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=
37533758

3759+
3760+
version "0.9.15"
3761+
resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1"
3762+
integrity sha1-vR/vr2hslrdUda7VGWQS/2DPucE=
3763+
37543764
object-assign@^4, object-assign@^4.1.0:
37553765
version "4.1.1"
37563766
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
@@ -3982,6 +3992,42 @@ pascalcase@^0.1.1:
39823992
resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14"
39833993
integrity sha1-s2PlXoAGym/iF4TS2yK9FdeRfxQ=
39843994

3995+
passport-facebook@^3.0.0:
3996+
version "3.0.0"
3997+
resolved "https://registry.yarnpkg.com/passport-facebook/-/passport-facebook-3.0.0.tgz#b16f7314128be55d020a2b75f574c194bd6d9805"
3998+
integrity sha512-K/qNzuFsFISYAyC1Nma4qgY/12V3RSLFdFVsPKXiKZt434wOvthFW1p7zKa1iQihQMRhaWorVE1o3Vi1o+ZgeQ==
3999+
dependencies:
4000+
passport-oauth2 "1.x.x"
4001+
4002+
passport-github@^1.1.0:
4003+
version "1.1.0"
4004+
resolved "https://registry.yarnpkg.com/passport-github/-/passport-github-1.1.0.tgz#8ce1e3fcd61ad7578eb1df595839e4aea12355d4"
4005+
integrity sha1-jOHj/NYa11eOsd9ZWDnkrqEjVdQ=
4006+
dependencies:
4007+
passport-oauth2 "1.x.x"
4008+
4009+
4010+
version "1.0.0"
4011+
resolved "https://registry.yarnpkg.com/passport-google-oauth1/-/passport-google-oauth1-1.0.0.tgz#af74a803df51ec646f66a44d82282be6f108e0cc"
4012+
integrity sha1-r3SoA99R7GRvZqRNgigr5vEI4Mw=
4013+
dependencies:
4014+
passport-oauth1 "1.x.x"
4015+
4016+
4017+
version "2.0.0"
4018+
resolved "https://registry.yarnpkg.com/passport-google-oauth20/-/passport-google-oauth20-2.0.0.tgz#0d241b2d21ebd3dc7f2b60669ec4d587e3a674ef"
4019+
integrity sha512-KSk6IJ15RoxuGq7D1UKK/8qKhNfzbLeLrG3gkLZ7p4A6DBCcv7xpyQwuXtWdpyR0+E0mwkpjY1VfPOhxQrKzdQ==
4020+
dependencies:
4021+
passport-oauth2 "1.x.x"
4022+
4023+
passport-google-oauth@^2.0.0:
4024+
version "2.0.0"
4025+
resolved "https://registry.yarnpkg.com/passport-google-oauth/-/passport-google-oauth-2.0.0.tgz#f6eb4bc96dd6c16ec0ecfdf4e05ec48ca54d4dae"
4026+
integrity sha512-JKxZpBx6wBQXX1/a1s7VmdBgwOugohH+IxCy84aPTZNq/iIPX6u7Mqov1zY7MKRz3niFPol0KJz8zPLBoHKtYA==
4027+
dependencies:
4028+
passport-google-oauth1 "1.x.x"
4029+
passport-google-oauth20 "2.x.x"
4030+
39854031
passport-jwt@^4.0.0:
39864032
version "4.0.0"
39874033
resolved "https://registry.yarnpkg.com/passport-jwt/-/passport-jwt-4.0.0.tgz#7f0be7ba942e28b9f5d22c2ebbb8ce96ef7cf065"
@@ -3990,11 +4036,53 @@ passport-jwt@^4.0.0:
39904036
jsonwebtoken "^8.2.0"
39914037
passport-strategy "^1.0.0"
39924038

4039+
passport-linkedin@^1.0.0:
4040+
version "1.0.0"
4041+
resolved "https://registry.yarnpkg.com/passport-linkedin/-/passport-linkedin-1.0.0.tgz#e3300cf232f7e7b4b95710e238f0bb7936b5d85a"
4042+
integrity sha1-4zAM8jL357S5VxDiOPC7eTa12Fo=
4043+
dependencies:
4044+
passport-oauth1 "1.x.x"
4045+
4046+
passport-local@^1.0.0:
4047+
version "1.0.0"
4048+
resolved "https://registry.yarnpkg.com/passport-local/-/passport-local-1.0.0.tgz#1fe63268c92e75606626437e3b906662c15ba6ee"
4049+
integrity sha1-H+YyaMkudWBmJkN+O5BmYsFbpu4=
4050+
dependencies:
4051+
passport-strategy "1.x.x"
4052+
4053+
4054+
version "1.1.0"
4055+
resolved "https://registry.yarnpkg.com/passport-oauth1/-/passport-oauth1-1.1.0.tgz#a7de988a211f9cf4687377130ea74df32730c918"
4056+
integrity sha1-p96YiiEfnPRoc3cTDqdN8ycwyRg=
4057+
dependencies:
4058+
oauth "0.9.x"
4059+
passport-strategy "1.x.x"
4060+
utils-merge "1.x.x"
4061+
4062+
4063+
version "1.5.0"
4064+
resolved "https://registry.yarnpkg.com/passport-oauth2/-/passport-oauth2-1.5.0.tgz#64babbb54ac46a4dcab35e7f266ed5294e3c4108"
4065+
integrity sha512-kqBt6vR/5VlCK8iCx1/KpY42kQ+NEHZwsSyt4Y6STiNjU+wWICG1i8ucc1FapXDGO15C5O5VZz7+7vRzrDPXXQ==
4066+
dependencies:
4067+
base64url "3.x.x"
4068+
oauth "0.9.x"
4069+
passport-strategy "1.x.x"
4070+
uid2 "0.0.x"
4071+
utils-merge "1.x.x"
4072+
39934073
[email protected], passport-strategy@^1.0.0:
39944074
version "1.0.0"
39954075
resolved "https://registry.yarnpkg.com/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4"
39964076
integrity sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ=
39974077

4078+
passport-twitter@^1.0.4:
4079+
version "1.0.4"
4080+
resolved "https://registry.yarnpkg.com/passport-twitter/-/passport-twitter-1.0.4.tgz#01a799e1f760bf2de49f2ba5fba32282f18932d7"
4081+
integrity sha1-AaeZ4fdgvy3knyul+6MigvGJMtc=
4082+
dependencies:
4083+
passport-oauth1 "1.x.x"
4084+
xtraverse "0.1.x"
4085+
39984086
passport@^0.4.0:
39994087
version "0.4.0"
40004088
resolved "https://registry.yarnpkg.com/passport/-/passport-0.4.0.tgz#c5095691347bd5ad3b5e180238c3914d16f05811"
@@ -5061,6 +5149,11 @@ type@^1.0.1:
50615149
resolved "https://registry.yarnpkg.com/type/-/type-1.2.0.tgz#848dd7698dafa3e54a6c479e759c4bc3f18847a0"
50625150
integrity sha512-+5nt5AAniqsCnu2cEQQdpzCAh33kVx8n0VoFidKpB1dVVLAN/F+bgVOqOJqOnEnrhp222clB5p3vUlD+1QAnfg==
50635151

5152+
5153+
version "0.0.3"
5154+
resolved "https://registry.yarnpkg.com/uid2/-/uid2-0.0.3.tgz#483126e11774df2f71b8b639dcd799c376162b82"
5155+
integrity sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I=
5156+
50645157
umzug@^2.1.0:
50655158
version "2.2.0"
50665159
resolved "https://registry.yarnpkg.com/umzug/-/umzug-2.2.0.tgz#6160bdc1817e4a63a625946775063c638623e62e"
@@ -5184,7 +5277,7 @@ util.promisify@^1.0.0:
51845277
define-properties "^1.1.2"
51855278
object.getownpropertydescriptors "^2.0.3"
51865279

5187-
5280+
51885281
version "1.0.1"
51895282
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
51905283
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
@@ -5306,11 +5399,23 @@ xdg-basedir@^3.0.0:
53065399
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-3.0.0.tgz#496b2cc109eca8dbacfe2dc72b603c17c5870ad4"
53075400
integrity sha1-SWsswQnsqNus/i3HK2A8F8WHCtQ=
53085401

5402+
5403+
version "0.1.27"
5404+
resolved "https://registry.yarnpkg.com/xmldom/-/xmldom-0.1.27.tgz#d501f97b3bdb403af8ef9ecc20573187aadac0e9"
5405+
integrity sha1-1QH5ezvbQDr4757MIFcxh6rawOk=
5406+
53095407
xtend@^4.0.0:
53105408
version "4.0.2"
53115409
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
53125410
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==
53135411

5412+
5413+
version "0.1.0"
5414+
resolved "https://registry.yarnpkg.com/xtraverse/-/xtraverse-0.1.0.tgz#b741bad018ef78d8a9d2e83ade007b3f7959c732"
5415+
integrity sha1-t0G60BjveNip0ug63gB7P3lZxzI=
5416+
dependencies:
5417+
xmldom "0.1.x"
5418+
53145419
y18n@^4.0.0:
53155420
version "4.0.0"
53165421
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"

0 commit comments

Comments
 (0)