@@ -2,6 +2,7 @@ const { ApolloServer } = require('apollo-server-express');
2
2
const express = require ( 'express' ) ;
3
3
const cookieParser = require ( 'cookie-parser' ) ;
4
4
const passport = require ( 'passport' ) ;
5
+ const GitHubStrategy = require ( 'passport-github2' ) . Strategy ;
5
6
6
7
const path = require ( 'path' ) ;
7
8
const cors = require ( 'cors' ) ;
@@ -17,11 +18,10 @@ const isDev = process.env.NODE_ENV === 'development';
17
18
const isProd = process . env . NODE_ENV === 'production' ;
18
19
const isTest = process . env . NODE_ENV === 'test' ;
19
20
20
- app . use ( express . json ( { limit : '100mb' } ) ) ;
21
- app . use ( express . urlencoded ( { limit : '100mb' , extended : true } ) )
21
+ app . use ( express . json ( { limit : '100mb' } ) ) ;
22
+ app . use ( express . urlencoded ( { limit : '100mb' , extended : true } ) ) ;
22
23
app . use ( cookieParser ( ) ) ;
23
24
24
-
25
25
// Routes
26
26
const stylesRouter = require ( './routers/stylesRouter' ) ;
27
27
@@ -30,18 +30,39 @@ const stylesRouter = require('./routers/stylesRouter');
30
30
app . use (
31
31
cors ( {
32
32
origin : [ 'http://localhost:8080' , 'app://rse' ] ,
33
- credentials : true ,
34
- } ) ,
33
+ credentials : true
34
+ } )
35
35
) ;
36
36
37
37
// TODO: github Oauth still needs debugging
38
38
// on initial login, redirect back to app is not working correctly when in production environment
39
39
// subsequent logins seem to be working fine, however
40
40
41
+ passport . use (
42
+ new GitHubStrategy (
43
+ {
44
+ clientID : process . env . GITHUB_ID ,
45
+ clientSecret : process . env . GITHUB_SECRET ,
46
+ callbackURL : 'http://localhost:5000/github/callback'
47
+ } ,
48
+ function ( accessToken , refreshToken , profile , done ) {
49
+ console . log ( profile ) ;
50
+ }
51
+ )
52
+ ) ;
53
+
41
54
// initializes passport and passport sessions
42
55
app . use ( passport . initialize ( ) ) ;
43
56
app . use ( passport . session ( ) ) ;
44
57
58
+ app . get (
59
+ '/auth/github' ,
60
+ passport . authenticate ( 'github' , { session : false } ) ,
61
+ ( req , res ) => {
62
+ res . send ( 'github' ) ;
63
+ }
64
+ ) ;
65
+
45
66
// for Oauth which is currently not working
46
67
app . get (
47
68
'/github/callback' ,
@@ -53,13 +74,22 @@ app.get(
53
74
sessionController . startSession ,
54
75
( req , res ) => {
55
76
if ( isDev ) {
56
- return res . status ( 200 ) . redirect ( `http://localhost:8080?=${ res . locals . ssid } ` ) ;
77
+ return res
78
+ . status ( 200 )
79
+ . redirect ( `http://localhost:8080?=${ res . locals . ssid } ` ) ;
57
80
} else {
58
81
return res . status ( 200 ) . redirect ( `app://rse?=${ res . locals . ssid } ` ) ;
59
82
}
60
83
}
61
84
) ;
62
85
86
+ // app.get('/github/callback', passport.authenticate('github'), function(
87
+ // req,
88
+ // res
89
+ // ) {
90
+ // console.log(req.user);
91
+ // res.redirect('http://localhost:8080');
92
+ // });
63
93
64
94
/*
65
95
GraphQl Router
@@ -74,11 +104,13 @@ const Mutation = require('./graphQL/resolvers/mutation');
74
104
// package resolvers into one variable to pass to Apollo Server
75
105
const resolvers = {
76
106
Query,
77
- Mutation,
107
+ Mutation
78
108
} ;
79
109
80
- app . use ( '/demoRender' , express . static ( path . join ( __dirname , './assets/renderDemo.css' ) ) ) ;
81
-
110
+ app . use (
111
+ '/demoRender' ,
112
+ express . static ( path . join ( __dirname , './assets/renderDemo.css' ) )
113
+ ) ;
82
114
83
115
// Re-direct to route handlers:
84
116
app . use ( '/user-styles' , stylesRouter ) ;
@@ -97,41 +129,41 @@ app.post(
97
129
userController . createUser ,
98
130
cookieController . setSSIDCookie ,
99
131
sessionController . startSession ,
100
- ( req , res ) => res . status ( 200 ) . json ( { sessionId : res . locals . ssid } ) ,
132
+ ( req , res ) => res . status ( 200 ) . json ( { sessionId : res . locals . ssid } )
101
133
) ;
102
134
103
135
app . post (
104
136
'/login' ,
105
137
userController . verifyUser ,
106
138
cookieController . setSSIDCookie ,
107
139
sessionController . startSession ,
108
- ( req , res ) => res . status ( 200 ) . json ( { sessionId : res . locals . ssid } ) ,
140
+ ( req , res ) => res . status ( 200 ) . json ( { sessionId : res . locals . ssid } )
109
141
) ;
110
142
111
143
// user must be logged in to get or save projects, otherwise they will be redirected to login page
112
144
app . post (
113
145
'/saveProject' ,
114
146
sessionController . isLoggedIn ,
115
147
projectController . saveProject ,
116
- ( req , res ) => res . status ( 200 ) . json ( res . locals . savedProject ) ,
148
+ ( req , res ) => res . status ( 200 ) . json ( res . locals . savedProject )
117
149
) ;
118
150
119
151
app . post (
120
152
'/getProjects' ,
121
153
sessionController . isLoggedIn ,
122
154
projectController . getProjects ,
123
- ( req , res ) => res . status ( 200 ) . json ( res . locals . projects ) ,
155
+ ( req , res ) => res . status ( 200 ) . json ( res . locals . projects )
124
156
) ;
125
157
126
158
app . delete (
127
159
'/deleteProject' ,
128
160
sessionController . isLoggedIn ,
129
161
projectController . deleteProject ,
130
- ( req , res ) => res . status ( 200 ) . json ( res . locals . deleted ) ,
162
+ ( req , res ) => res . status ( 200 ) . json ( res . locals . deleted )
131
163
) ;
132
164
133
- app . get ( "/" , function ( req , res ) {
134
- res . send ( " Houston, Caret is in orbit!" ) ;
165
+ app . get ( '/' , function ( req , res ) {
166
+ res . send ( ' Houston, Caret is in orbit!' ) ;
135
167
} ) ;
136
168
137
169
// catch-all route handler
@@ -142,7 +174,7 @@ app.use((err, req, res, next) => {
142
174
const defaultErr = {
143
175
log : 'Express error handler caught unknown middleware' ,
144
176
status : 500 ,
145
- message : { err : 'An error occurred' } ,
177
+ message : { err : 'An error occurred' }
146
178
} ;
147
179
148
180
const errorObj = Object . assign ( { } , defaultErr , err ) ;
0 commit comments