Skip to content
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

Passport setup #60

Merged
merged 2 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const exphbs = require('express-handlebars');
const path = require('path');
const bodyParser = require('body-parser');
const passport = require('passport');
const cookieSession = require('cookie-session');
require('dotenv').config();

// import route controllers
Expand Down Expand Up @@ -32,15 +33,10 @@ app.engine(
// config middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// app.use(session({
// secret: 'keyboard cat',
// resave: true,
// saveUninitialized: true,
// }));
// // app.use(cookieSession({
// // maxAge: 30 * 24 * 60 * 60 * 1000,
// // keys: [process.env.COOKIE_KEY],
// // }));
app.use(cookieSession({
maxAge: 24 * 60 * 60 * 1000,
keys: [process.env.COOKIE_KEY],
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, '..', 'public')));
Expand Down
3 changes: 1 addition & 2 deletions src/model/database/db_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ CREATE TABLE tech_stack (
CREATE TABLE members (
id SERIAL PRIMARY KEY,
github_id INTEGER NOT NULL,
first_name VARCHAR(255),
last_name VARCHAR(255),
full_name VARCHAR(255),
github_handle VARCHAR(255) NOT NULL,
github_avatar_url VARCHAR(4000),
fac_campus VARCHAR(255),
Expand Down
9 changes: 4 additions & 5 deletions src/model/database/db_build_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ CREATE TABLE tech_stack (
CREATE TABLE members (
id SERIAL PRIMARY KEY,
github_id INTEGER NOT NULL,
first_name VARCHAR(255),
last_name VARCHAR(255),
full_name VARCHAR(255),
github_handle VARCHAR(255) NOT NULL,
github_avatar_url VARCHAR(4000),
fac_campus VARCHAR(255),
Expand Down Expand Up @@ -55,10 +54,10 @@ VALUES
('Node.js');

INSERT INTO members
(github_id, first_name, last_name, github_handle, github_avatar_url, fac_campus, fac_code_id, linkedin_url, twitter_handle, member_type, job_search_status, min_years_exp, max_years_exp, github_cv_url, cv_url, job_view_pref)
(github_id, full_name, github_handle, github_avatar_url, fac_campus, fac_code_id, linkedin_url, twitter_handle, member_type, job_search_status, min_years_exp, max_years_exp, github_cv_url, cv_url, job_view_pref)
VALUES
(1, 'Helen', 'Zhou', 'helenzhou6', 'https://uk.linkedin.com/dbsmith', 'london', 1, 'https://uk.linkedin.com/', 'hel_zhou', 'admin', 'red', 0, 1, 'https://github.com/helenzhou6/CV', 'https://github.com/helenzhou6/CV', 'private'),
(2, 'Deborah', 'Smith', 'dsmith', 'https://uk.linkedin.com/dbsmith', 'gaza', 2, 'https://uk.linkedin.com/dbsmith', 'dbsmith', 'member', 'yellow', 2, 5, NULL, NULL, 'public');
(1, 'Helen', 'helenzhou6', 'https://uk.linkedin.com/dbsmith', 'london', 1, 'https://uk.linkedin.com/', 'hel_zhou', 'admin', 'red', 0, 1, 'https://github.com/helenzhou6/CV', 'https://github.com/helenzhou6/CV', 'private'),
(2, 'Deborah', 'dsmith', 'https://uk.linkedin.com/dbsmith', 'gaza', 2, 'https://uk.linkedin.com/dbsmith', 'dbsmith', 'member', 'yellow', 2, 5, NULL, NULL, 'public');

INSERT INTO member_tech_stack
(member_id, stack_id, order_num)
Expand Down
2 changes: 1 addition & 1 deletion src/model/queries/postMemberInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const db = require('../database/db_connection');
const postMemberInfo = (githubJson) => {
const values = Object.values(githubJson);
return db.query(
'INSERT INTO members (github_id, github_handle, first_name, last_name, github_avatar_url) VALUES ($1, $2, $3, $4, $5)',
'INSERT INTO members (github_id, github_handle, full_name, github_avatar_url) VALUES ($1, $2, $3, $4)',
values,
).then(() => console.log(`${githubJson.github_handle}member info posted to DB`))
.catch((error) => {
Expand Down
46 changes: 31 additions & 15 deletions src/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const passport = require('passport');
const Strategy = require('passport-github2').Strategy;

// this is the database!!
// const User = require('../models/user');
const postMemberInfo = require('./model/queries/postMemberInfo.js');
const getMemberData = require('./model/queries/getMemberData.js');


(function config() {
Expand All @@ -13,29 +13,45 @@ const Strategy = require('passport-github2').Strategy;
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: `${process.env.BASE_URL}/auth/github/callback`,
},
((accessToken, refreshToken, profile, done) => {
console.log('strategy success!! ');
console.log(profile._json);
((accessToken, refreshToken, profile, next) => {

let memberProfile = {
github_id: profile._json.id,
github_handle: profile._json.login,
first_name: profile._json.name,
full_name: profile._json.name,
github_avatar_url: profile._json.avatar_url,
}

return done(null, profile);
return getMemberData(memberProfile.github_id)
.then((userDataObj) => {
if(!userDataObj){
postMemberInfo(memberProfile)
.then(() => {
getMemberData(memberProfile.github_id)
.then((newUserDataObj) => {
return next(null, newUserDataObj, { message: 'Signed up successfully' })
})
})
} else {
return next(null, userDataObj, { message: 'Logged in successfully' })
}
})
.catch(error => { throw new Error(error.message) });
}),
));
// profile ID info needs to go into the database or check against what is in the DB
passport.serializeUser((user, done) => {
done(null, user.id);

passport.serializeUser((userDataObj, next) => {
next(null, userDataObj.github_id);
});

passport.deserializeUser((id, done) => {
// need to get the user id from the dtb
// User.findById(id, (err, user) => {
// done(err, user);
// });
passport.deserializeUser((id, next) => {
getMemberData(id)
.then((user) => {
next(null, user);
})
.catch((error) => {
next(error);
})

});
})();
8 changes: 3 additions & 5 deletions src/test/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ test('Test member data to ensure correct data received', (t) => {
const correctResult = {
id: 1,
github_id: 1,
first_name: 'Helen',
last_name: 'Zhou',
full_name: 'Helen',
github_handle: 'helenzhou6',
github_avatar_url: 'https://uk.linkedin.com/dbsmith',
fac_campus: 'london',
Expand All @@ -72,7 +71,7 @@ test('Test member data to ensure correct data received', (t) => {
cv_url: 'https://github.com/helenzhou6/CV',
job_view_pref: 'private',
};
t.equal(Object.keys(res).length, 17, 'correct array length');
t.equal(Object.keys(res).length, 16, 'correct array length');
t.deepEqual(res, correctResult, 'deepEquals of first test member');
t.end();
})
Expand All @@ -88,8 +87,7 @@ test('Test postMemberInfo adds a row', (t) => {
const memberProfile = {
github_id: 43948924,
github_handle: 'john_profile',
first_name: 'John',
last_name: 'Doe',
full_name: 'John',
github_avatar_url: 'https://avatars3.githubusercontent.com/u/32312712?v=4',
};
runDbBuild().then(() => {
Expand Down