@@ -4,26 +4,68 @@ const session = require('express-session');
44const cors = require ( 'cors' ) ;
55const config = require ( './config.js' ) ;
66const massive = require ( 'massive' ) ;
7+ const passport = require ( 'passport' ) ;
8+ const GithubStrategy = require ( 'passport-github2' ) . Strategy ;
79const connectionString = config . connectionString ;
810
11+ passport . serializeUser ( ( user , done ) => {
12+ done ( null , user ) ;
13+ } ) ;
14+
15+ passport . deserializeUser ( ( obj , done ) => {
16+ done ( null , obj ) ;
17+ } ) ;
18+
19+ passport . use ( new GithubStrategy ( {
20+ clientID : config . githubId ,
21+ clientSecret : config . githubSecret ,
22+ callbackURL : '/auth/github/callback'
23+ } , ( accessToken , refreshToken , profile , done ) => {
24+ // code goes here
25+ // go to database and look for profile.id
26+ // create user using profile.id
27+ return done ( null /*error*/ , profile /*info that goes on session*/ ) ;
28+ } ) ) ;
29+
30+
931const app = module . exports = express ( ) ;
1032
33+
1134const massiveInstance = massive . connectSync ( { connectionString : connectionString } ) ;
1235
1336app . use ( express . static ( __dirname + '/dist' ) ) ;
1437app . use ( bodyParser . json ( ) ) ;
1538app . use ( cors ( ) ) ;
1639
17- app . set ( 'db' , massiveInstance ) ;
18-
19- let db = app . get ( 'db' ) ;
2040
2141app . use ( session ( {
2242 secret : config . sessionSecret ,
2343 saveUninitialized : false ,
2444 resave : false
2545} ) ) ;
2646
27- app . listen ( config . port , ( ) => {
28- console . log ( 'listening to port' , config . port ) ;
47+
48+ app . use ( passport . initialize ( ) ) ;
49+ app . use ( passport . session ( ) ) ;
50+
51+ app . set ( 'db' , massiveInstance ) ;
52+ const db = app . get ( 'db' ) ;
53+ const endPointCtrl = require ( './controllers/endPointCtrl' ) ;
54+
55+ app . get ( '/auth/github' , passport . authenticate ( 'github' ) ) ;
56+
57+ app . get ( '/auth/github/callback' ,
58+ passport . authenticate ( 'github' , { failureRedirect : '/login' } ) ,
59+ function ( req , res ) {
60+ // Successful authentication, redirect home.
61+ res . redirect ( '/' ) ;
62+ } ) ;
63+
64+ app . post ( '/solution' , endPointCtrl . testScript ) ;
65+
66+
67+ app . listen ( config . port , function ( ) {
68+ console . log ( `listening on port ${ this . address ( ) . port } ` ) ;
69+
70+
2971} ) ;
0 commit comments