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

Skip to content

Commit 1268d9a

Browse files
author
Sashko Stubailo
committed
Refactor GitHub stuff to clean up index
1 parent 4d48eb3 commit 1268d9a

File tree

3 files changed

+68
-52
lines changed

3 files changed

+68
-52
lines changed

api/githubKeys.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import dotenv from 'dotenv';
2+
3+
dotenv.config({ silent: true });
4+
5+
export const {
6+
GITHUB_CLIENT_ID,
7+
GITHUB_CLIENT_SECRET,
8+
} = process.env;

api/githubLogin.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import express from 'express';
2+
import session from 'express-session';
3+
import passport from 'passport';
4+
import { Strategy as GitHubStrategy } from 'passport-github';
5+
import knex from './sql/connector';
6+
7+
import {
8+
GITHUB_CLIENT_ID,
9+
GITHUB_CLIENT_SECRET,
10+
} from './githubKeys';
11+
12+
const KnexSessionStore = require('connect-session-knex')(session);
13+
const store = new KnexSessionStore({
14+
knex,
15+
});
16+
17+
export function setUpGitHubLogin(app) {
18+
app.use(session({
19+
secret: 'your secret',
20+
resave: true,
21+
saveUninitialized: true,
22+
store,
23+
}));
24+
25+
app.use(passport.initialize());
26+
app.use(passport.session());
27+
28+
app.use(express.static('dist'));
29+
30+
app.get('/login/github',
31+
passport.authenticate('github'));
32+
33+
app.get('/login/github/callback',
34+
passport.authenticate('github', { failureRedirect: '/' }),
35+
(req, res) => res.redirect('/'));
36+
37+
app.get('/logout', (req, res) => {
38+
req.logout();
39+
res.redirect('/');
40+
});
41+
}
42+
43+
const gitHubStrategyOptions = {
44+
clientID: GITHUB_CLIENT_ID,
45+
clientSecret: GITHUB_CLIENT_SECRET,
46+
callbackURL: 'http://localhost:3000/login/github/callback',
47+
};
48+
49+
passport.use(new GitHubStrategy(gitHubStrategyOptions, (accessToken, refreshToken, profile, cb) => {
50+
cb(null, profile);
51+
}));
52+
53+
passport.serializeUser((user, cb) => cb(null, user));
54+
passport.deserializeUser((obj, cb) => cb(null, obj));

api/index.js

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,31 @@
11
import path from 'path';
22
import express from 'express';
3-
import session from 'express-session';
4-
import passport from 'passport';
53
import { apolloExpress, graphiqlExpress } from 'apollo-server';
64
import { makeExecutableSchema } from 'graphql-tools';
7-
import { Strategy as GitHubStrategy } from 'passport-github';
85
import bodyParser from 'body-parser';
9-
import dotenv from 'dotenv';
10-
import knex from './sql/connector';
11-
12-
const KnexSessionStore = require('connect-session-knex')(session);
13-
const store = new KnexSessionStore({
14-
knex,
15-
});
166

7+
import {
8+
GITHUB_CLIENT_ID,
9+
GITHUB_CLIENT_SECRET,
10+
} from './githubKeys';
11+
import { setUpGitHubLogin } from './githubLogin';
1712
import { schema, resolvers } from './schema';
1813
import { GitHubConnector } from './github/connector';
1914
import { Repositories, Users } from './github/models';
2015
import { Entries, Comments } from './sql/models';
2116

22-
dotenv.config({ silent: true });
2317
let PORT = 3010;
2418

2519
if (process.env.PORT) {
2620
PORT = parseInt(process.env.PORT, 10) + 100;
2721
}
2822

29-
const {
30-
GITHUB_CLIENT_ID,
31-
GITHUB_CLIENT_SECRET,
32-
} = process.env;
33-
3423
const app = express();
3524

36-
app.use(session({
37-
secret: 'your secret',
38-
resave: true,
39-
saveUninitialized: true,
40-
store,
41-
}));
42-
43-
app.use(passport.initialize());
44-
app.use(passport.session());
45-
4625
app.use(bodyParser.urlencoded({ extended: true }));
4726
app.use(bodyParser.json());
4827

49-
app.use(express.static('dist'));
50-
51-
app.get('/login/github',
52-
passport.authenticate('github'));
53-
54-
app.get('/login/github/callback',
55-
passport.authenticate('github', { failureRedirect: '/' }),
56-
(req, res) => res.redirect('/'));
57-
58-
app.get('/logout', (req, res) => {
59-
req.logout();
60-
res.redirect('/');
61-
});
28+
setUpGitHubLogin(app);
6229

6330
const executableSchema = makeExecutableSchema({
6431
typeDefs: schema,
@@ -115,16 +82,3 @@ app.get('/', (req, res) => {
11582
app.listen(PORT, () => console.log( // eslint-disable-line no-console
11683
`API Server is now running on http://localhost:${PORT}`
11784
));
118-
119-
const gitHubStrategyOptions = {
120-
clientID: GITHUB_CLIENT_ID,
121-
clientSecret: GITHUB_CLIENT_SECRET,
122-
callbackURL: 'http://localhost:3000/login/github/callback',
123-
};
124-
125-
passport.use(new GitHubStrategy(gitHubStrategyOptions, (accessToken, refreshToken, profile, cb) => {
126-
cb(null, profile);
127-
}));
128-
129-
passport.serializeUser((user, cb) => cb(null, user));
130-
passport.deserializeUser((obj, cb) => cb(null, obj));

0 commit comments

Comments
 (0)