From 6238c41b744dc873bde24ab000410171115d3b1e Mon Sep 17 00:00:00 2001 From: Brian Gormanly Date: Fri, 27 Oct 2023 12:02:59 -0400 Subject: [PATCH] added fix for session db save effecting session on first load --- .bsp/sbt.json | 1 + server/agora.js | 70 +++++++++++++++-------------- server/controller/authController.js | 2 +- server/service/userService.js | 2 +- target/.history3 | 2 + 5 files changed, 42 insertions(+), 35 deletions(-) create mode 100644 .bsp/sbt.json create mode 100644 target/.history3 diff --git a/.bsp/sbt.json b/.bsp/sbt.json new file mode 100644 index 00000000..b46f7887 --- /dev/null +++ b/.bsp/sbt.json @@ -0,0 +1 @@ +{"name":"sbt","version":"1.8.2","bspVersion":"2.1.0-M1","languages":["scala"],"argv":["/Library/Java/JavaVirtualMachines/jdk-11.0.14.jdk/Contents/Home/bin/java","-Xms100m","-Xmx100m","-classpath","/opt/homebrew/Cellar/sbt/1.8.2/libexec/bin/sbt-launch.jar","-Dsbt.script=/opt/homebrew/Cellar/sbt/1.8.2/libexec/bin/sbt","xsbt.boot.Boot","-bsp"]} \ No newline at end of file diff --git a/server/agora.js b/server/agora.js index 564bca51..75699700 100644 --- a/server/agora.js +++ b/server/agora.js @@ -14,6 +14,8 @@ require( "dotenv" ).config(); // manage parsing json from body of the request const bodyParser = require( "body-parser" ); +// library that allows us to hook responses in the middleware +const responseHooks = require( "express-response-hooks" ); var path = require( "path" ); app.use( @@ -22,6 +24,8 @@ app.use( } ) ); app.use( bodyParser.json() ); +//app.use( responseHooks() ); + // cross origin const cors = require( "cors" ); @@ -50,26 +54,12 @@ app.use( express.static( publicPath ) ); // import uuid generator const { v4: uuidv4 } = require( "uuid" ); -// set up session -let session = require( "express-session" ); -const pgSession = require( "connect-pg-simple" )( session ); - // database connection const db = require( "./db/connection" ); -/** - * OpenAPI / Swagger - */ -const swaggerUi = require( "swagger-ui-express" ); -const swaggerJsDoc = require( "swagger-jsdoc" ); - -// global swagger info (TODO: maybe this should be moved into another file if it is staying?) -const YAML = require( "yamljs" ); -const swaggerApiDoc = YAML.load( "./server/agoraApi.yaml" ); - -// initialize swagger -//const swaggerDocInit = swaggerJsDoc( swaggerGlobal ); -app.use( "/api-docs", swaggerUi.serve, swaggerUi.setup( swaggerApiDoc ) ); +// set up session +let session = require( "express-session" ); +const pgSession = require( "connect-pg-simple" )( session ); let sess = { store: new pgSession( { @@ -97,24 +87,27 @@ if ( app.get( "env" ) === "production" ) { app.use( session( sess ) ); -// check that the user is logged in, if so include user data in req for view + + +//check that the user is logged in, if so include user session data in req for view (ejs needs locals) app.use( function ( req, res, next ) { // set the currentUrl so it is available in the esj res.locals.currentUrl = encodeURIComponent( req.url ); - // if authenticated set the session as a local of the esj - if ( !req.session.isAuth ) { - next(); - } - else { - res.locals.authUser = req.session.authUser; - next(); - } -} ); + // make the auth information available to the locals for ejs + res.locals.isAuth = req.session.isAuth; + res.locals.authUser = req.session.authUser; + next(); +} ); +/** + * Auth routes + */ +const authRoutes = require( "./routes/authRoutes" ); +app.use( "/", authRoutes ); /** * Page routes @@ -122,12 +115,6 @@ app.use( function ( req, res, next ) { const pageRoutes = require( "./routes/pagesRoutes" ); app.use( "/", pageRoutes ); -/** - * Auth routes - */ -const authRoutes = require( "./routes/authRoutes" ); -app.use( "/", authRoutes ); - /** * User routes */ @@ -174,6 +161,23 @@ const { errorController } = require( "./controller/apis/apiErrorController" ); const ApiMessage = require( "./model/util/ApiMessage" ); app.use( "/api/v1/open", apiUnauthRoutes ); + +/** + * OpenAPI / Swagger + */ +const swaggerUi = require( "swagger-ui-express" ); +const swaggerJsDoc = require( "swagger-jsdoc" ); + +// global swagger info (TODO: maybe this should be moved into another file if it is staying?) +const YAML = require( "yamljs" ); +const swaggerApiDoc = YAML.load( "./server/agoraApi.yaml" ); + +// initialize swagger +//const swaggerDocInit = swaggerJsDoc( swaggerGlobal ); +app.use( "/api-docs", swaggerUi.serve, swaggerUi.setup( swaggerApiDoc ) ); + + + // // workspace // let workspaceRoutes = require('./routes/community/workspaceRoutes'); // app.use('/community/workspace', workspaceRoutes); diff --git a/server/controller/authController.js b/server/controller/authController.js index 9d925257..4749214e 100644 --- a/server/controller/authController.js +++ b/server/controller/authController.js @@ -247,7 +247,7 @@ const signIn = async function( req, res ) { // log the data if( user && device ) { - userService.logUserSession( user.userId, ip, device ); + await userService.logUserSession( user.userId, ip, device ); } } diff --git a/server/service/userService.js b/server/service/userService.js index 18533e91..2edafe32 100644 --- a/server/service/userService.js +++ b/server/service/userService.js @@ -716,7 +716,7 @@ exports.logUserSession = async function( userId, ipAddress, device ) { try { - let response = await db.query( text, values ); + await db.query( text, values ); return true; } catch( e ) { diff --git a/target/.history3 b/target/.history3 new file mode 100644 index 00000000..c7d1dd4a --- /dev/null +++ b/target/.history3 @@ -0,0 +1,2 @@ +1677507046026:q +1677507049620:exit