diff --git a/src/app.js b/src/app.js index 10846d0..f6e4283 100644 --- a/src/app.js +++ b/src/app.js @@ -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 @@ -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'))); diff --git a/src/model/database/db_build.sql b/src/model/database/db_build.sql index 07b1c80..3ff7fd9 100644 --- a/src/model/database/db_build.sql +++ b/src/model/database/db_build.sql @@ -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), diff --git a/src/model/database/db_build_test.sql b/src/model/database/db_build_test.sql index 9cc046e..4f70c6c 100644 --- a/src/model/database/db_build_test.sql +++ b/src/model/database/db_build_test.sql @@ -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), @@ -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) diff --git a/src/model/queries/postMemberInfo.js b/src/model/queries/postMemberInfo.js index 4f820f0..0da999e 100644 --- a/src/model/queries/postMemberInfo.js +++ b/src/model/queries/postMemberInfo.js @@ -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) => { diff --git a/src/oauth.js b/src/oauth.js index 9384ba8..07e928a 100644 --- a/src/oauth.js +++ b/src/oauth.js @@ -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() { @@ -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); + }) + }); })(); diff --git a/src/test/db.test.js b/src/test/db.test.js index bb38944..5eab4b0 100644 --- a/src/test/db.test.js +++ b/src/test/db.test.js @@ -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', @@ -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(); }) @@ -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(() => {