Skip to content

Commit aee189d

Browse files
committed
working on refactoring session controller
1 parent 7317ca4 commit aee189d

File tree

5 files changed

+253
-6
lines changed

5 files changed

+253
-6
lines changed

.DS_Store

0 Bytes
Binary file not shown.

electron/main.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function createWindow() {
1010
contextIsolation: true,
1111
},
1212
});
13-
win.loadURL('index.html');
13+
win.loadURL('http://localhost:8888');
1414
}
1515

1616
app.whenReady().then(createWindow);

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
"name": "argometrics",
33
"version": "1.0.0",
44
"description": "ORDER OF ACTION",
5-
"main": "./client/index.tsx",
5+
"main": "./electron/main.tsx",
66
"scripts": {
77
"build": "webpack --mode production",
88
"devdev": "webpack-dev-server --mode development --open --hot",
99
"dev": "concurrently \"webpack-dev-server --mode development --hot --open\" \"nodemon ./server/server.ts\"",
10-
"start": "nodemon server/server.ts"
10+
"start": "nodemon server/server.ts",
11+
"electron-start": "electron ."
1112
},
1213
"keywords": [],
1314
"author": "",

server/controllers/cookieController.ts

+100-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Request, Response, NextFunction, ErrorRequestHandler } from 'express';
1+
//original cookiecontroller
22

3+
import { Request, Response, NextFunction, ErrorRequestHandler } from 'express';
34
const cookieController = {
45
sessionCookie: (req: Request, res: Response, next: NextFunction) => {
56
if(res.locals.userInfo) {
@@ -17,4 +18,101 @@ const cookieController = {
1718
}
1819
}
1920

20-
module.exports = cookieController;
21+
module.exports = cookieController;
22+
23+
//----------------------------------------------------------------------------------------------------------
24+
25+
// import { ipcMain, session, IpcMainInvokeEvent } from 'electron';
26+
27+
// interface User {
28+
// googleId?: string;
29+
// }
30+
31+
// interface UserInfo {
32+
// _id?: string;
33+
// }
34+
35+
// ipcMain.handle('setCookies', async (event: IpcMainInvokeEvent, userInfo: UserInfo, user: User) => {
36+
// const defaultSession = session.defaultSession;
37+
38+
// if (userInfo && userInfo._id) {
39+
// const details = {
40+
// url: 'http://localhost',
41+
// name: 'session',
42+
// value: userInfo._id,
43+
// httpOnly: true
44+
// }
45+
46+
// await defaultSession.cookies.set(details);
47+
// }
48+
49+
// if (user && user.googleId) {
50+
// const details = {
51+
// url: 'http://localhost',
52+
// name: 'googleId',
53+
// value: user.googleId,
54+
// httpOnly: true
55+
// }
56+
57+
// await defaultSession.cookies.set(details);
58+
// }
59+
// });
60+
61+
// import { ipcRenderer } from 'electron';
62+
63+
// ipcRenderer.invoke('setCookies', userInfo, user);
64+
65+
//----------------------------------------------------------------------------------------------------------
66+
67+
// import { BrowserWindow } from 'electron';
68+
69+
// interface UserInfo {
70+
// _id?: string;
71+
// googleId?: string;
72+
// }
73+
74+
// const focusedWindow = BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0];
75+
76+
// // Get the webContents of the window
77+
// const webContents = focusedWindow.webContents;
78+
79+
// // Retrieve the URL of the currently loaded page
80+
// const currentUrl = webContents.getURL();
81+
82+
// // Extract the domain from the URL
83+
// const urlObject = new URL(currentUrl);
84+
// const domain = urlObject.hostname;
85+
86+
// const cookieController = {
87+
// sessionCookie: (userInfo: UserInfo | undefined, window: BrowserWindow) => {
88+
// if (userInfo) {
89+
// if (userInfo._id) {
90+
// window.webContents.session.cookies.set({
91+
// url: currentUrl,
92+
// name: 'session',
93+
// value: userInfo._id,
94+
// httpOnly: true
95+
// }).then(() => {
96+
// console.log('Session cookie set successfully');
97+
// }).catch((error) => {
98+
// console.error('Failed to set session cookie:', error);
99+
// });
100+
// }
101+
102+
// if (userInfo.googleId) {
103+
// window.webContents.session.cookies.set({
104+
// url: currentUrl,
105+
// name: 'googleId',
106+
// value: userInfo.googleId,
107+
// httpOnly: true
108+
// }).then(() => {
109+
// console.log('GoogleId cookie set successfully');
110+
// }).catch((error) => {
111+
// console.error('Failed to set GoogleId cookie:', error);
112+
// });
113+
// }
114+
// }
115+
// }
116+
// };
117+
118+
// export default cookieController;

server/controllers/sessionController.ts

+149-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,152 @@ const sessionController = {
5454
}
5555
}
5656

57-
module.exports = sessionController
57+
module.exports = sessionController
58+
59+
60+
61+
62+
//--------------------------------------------------------------
63+
64+
65+
66+
// import { Request, Response, NextFunction, ErrorRequestHandler } from 'express';
67+
// const Session = require('../models/sessionModel');
68+
69+
// const errorObject = (err: ErrorRequestHandler) => {
70+
// return {
71+
// log: 'Error in Session Controller',
72+
// message: { err: `Error caught: ${err}` }
73+
// }
74+
// }
75+
76+
// const sessionController: any = {
77+
// startSession: (req: Request, res: Response, next: NextFunction) => {
78+
// const { _id } = res.locals.userInfo;
79+
// const newSession = new Session({cookieId: _id});
80+
// newSession
81+
// .save()
82+
// .then((newSession: object) => {
83+
// res.locals.newSession = newSession;
84+
// return next();
85+
// })
86+
// .catch((err: ErrorRequestHandler) => {
87+
// const error = errorObject(err);
88+
// return next(error);
89+
// })
90+
// },
91+
// isLoggedIn: (req: Request, res: Response, next: NextFunction) => {
92+
// // console.log('request cookies', req.cookies)
93+
// Session.findOne({ cookieId: req.cookies.session })
94+
// .then((data: any) => {
95+
// // console.log('data from findOne', data)
96+
// if (!data && res.locals.userInfo) {
97+
// // console.log('userInfo', res.locals.userInfo)
98+
// const { _id } = res.locals.userInfo;
99+
// // find session based on _id in db -> if none, create
100+
// //query Session db by _id, if one exists, delete before creating a new one
101+
// // Session.findOne({cookieId: _id})
102+
// // .then((cookie: any)=>{
103+
// // if(!cookie){
104+
// const newSession = new Session({cookieId: _id});
105+
// console.log('newSession created', newSession)
106+
// newSession
107+
// .save()
108+
// .then((newSession: object) => {
109+
// res.locals.newSession = newSession;
110+
// return next();
111+
// })
112+
// // } else if(cookie) {
113+
// // // DELETE COOKIE SO NO DUPES & WE CAN REFRESH TIMER
114+
// // // CREATE NEW SESSION
115+
// // const newSession = new Session({cookieId: _id});
116+
// // console.log('newSession created', newSession)
117+
// // newSession
118+
// // .save()
119+
// // .then((newSession: object) => {
120+
// // // console.log('newSession inside save', newSession)
121+
// // res.locals.newSession = newSession;
122+
// // return next();
123+
// // })
124+
125+
// })
126+
// }
127+
// } else if (!data && !res.locals.userInfo) {
128+
// res.redirect('/register');
129+
// } else {
130+
// return next();
131+
// }
132+
// })
133+
// .catch((err: ErrorRequestHandler) => {
134+
// return next(errorObject(err));
135+
// })
136+
// }
137+
// }
138+
139+
// module.exports = sessionController
140+
141+
142+
143+
144+
//--------------------------------------------------------------
145+
146+
// import { Request, Response, NextFunction, ErrorRequestHandler } from 'express';
147+
// import Session from '../models/sessionModel';
148+
149+
// const errorObject = (err: ErrorRequestHandler) => {
150+
// return {
151+
// log: 'Error in Session Controller',
152+
// message: { err: `Error caught: ${err}` }
153+
// }
154+
// }
155+
156+
// const sessionController = {
157+
// startSession: (req: Request, res: Response, next: NextFunction) => {
158+
// const { _id } = res.locals.userInfo;
159+
// const newSession = new Session({ cookieId: _id });
160+
// newSession
161+
// .save()
162+
// .then((newSession: object) => {
163+
// res.locals.newSession = newSession;
164+
// return next();
165+
// })
166+
// .catch((err: ErrorRequestHandler) => {
167+
// const error = errorObject(err);
168+
// return next(error);
169+
// })
170+
// },
171+
// isLoggedIn: (req: Request, res: Response, next: NextFunction) => {
172+
// const sessionCookie = req.cookies.session;
173+
174+
// if (!sessionCookie) {
175+
// if (res.locals.userInfo) {
176+
// const { _id } = res.locals.userInfo;
177+
178+
// Session.findOne({ cookieId: _id })
179+
// .then((data: any) => {
180+
// if (!data) {
181+
// const newSession = new Session({ cookieId: _id });
182+
// return newSession.save();
183+
// }
184+
// })
185+
// .then((newSession: object) => {
186+
// if (newSession) {
187+
// res.cookie('session', newSession.cookieId, {
188+
// httpOnly: true
189+
// });
190+
// }
191+
// return next();
192+
// })
193+
// .catch((err: ErrorRequestHandler) => {
194+
// return next(errorObject(err));
195+
// });
196+
// } else {
197+
// res.redirect('/register');
198+
// }
199+
// } else {
200+
// return next();
201+
// }
202+
// }
203+
// };
204+
205+
// export default sessionController;

0 commit comments

Comments
 (0)