Skip to content

Commit 0db7e6d

Browse files
committed
reverted server TS files to JS and commented out type annotations
1 parent 760eda2 commit 0db7e6d

15 files changed

+241
-202
lines changed

server/config/passport.ts renamed to server/config/passport.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { TablePaginationUnstyledSpacerSlotProps } from "@mui/base";
2-
import { Error } from "mongoose";
3-
import { Authenticator, Profile } from "passport";
1+
/** using import statements in the electron / node files breaks npm start and nodepty
2+
* - types are left in place in these files for future iteration alternate import method is required for them to function
3+
*/
4+
// import { TablePaginationUnstyledSpacerSlotProps } from "@mui/base";
5+
// import { Error } from "mongoose";
6+
// import { Authenticator, Profile } from "passport";
47

58
const GitHubStrategy = require('passport-github2').Strategy;
69
const GoogleStrategy = require('passport-google-oauth20').Strategy;
710
const { GithubUser, GoogleUser } = require('../models/userModel.js');
811

9-
module.exports = function (passport: Authenticator) {
12+
module.exports = function (passport/*: Authenticator */) {
1013
passport.use(
1114
new GitHubStrategy(
1215
{
@@ -15,10 +18,10 @@ module.exports = function (passport: Authenticator) {
1518
callbackURL: 'http://localhost:3001/auth/github/callback',
1619
},
1720

18-
(accessToken: String, refreshToken: (String | undefined), profile: Profile, done: Function): void => {
21+
(accessToken/* : String */, refreshToken/* : (String | undefined) */, profile/* : Profile */, done/* : Function */)/* : void */ => {
1922
//console.log('this is our accessToken:', accessToken);
2023
// we are checking if the github profile is in our monogDB
21-
GithubUser.findOne({ githubId: profile.id }, (err: Error, result: { githubId: String, username: String }): void => {
24+
GithubUser.findOne({ githubId: profile.id }, (err/* : Error */, result/* : { githubId: String, username: String } */)/* : void */ => {
2225
if (result) {
2326
// already have this user
2427
//console.log('user is: ', result);
@@ -31,7 +34,7 @@ module.exports = function (passport: Authenticator) {
3134
username: profile.displayName,
3235
})
3336
.save()
34-
.then((newUser): void => {
37+
.then((newUser)/* : void */=> {
3538
//console.log('created new user: ', newUser);
3639
// res.locals.userId = newUser._id
3740
done(null, newUser);
@@ -52,10 +55,10 @@ module.exports = function (passport: Authenticator) {
5255
callbackURL: 'http://localhost:3001/auth/google/callback',
5356
},
5457

55-
(accessToken: String, refreshToken: (String | undefined), profile: Profile, done: Function): void => {
58+
(accessToken/* : String */, refreshToken/* : (String | undefined) */, profile/* : Profile */, done/* : Function */)/* : void */=> {
5659
//console.log('this is our accessToken:', accessToken);
5760
// we are checking if the google profile is in our monogDB
58-
GoogleUser.findOne({ googleId: profile.id }, (err: Error, result: {googleId: String, username: String}) => {
61+
GoogleUser.findOne({ googleId: profile.id }, (err/* : Error */, result/* : {googleId: String, username: String} */) => {
5962
if (result) {
6063
// already have this user
6164
//console.log('user is: ', result);
@@ -67,7 +70,7 @@ module.exports = function (passport: Authenticator) {
6770
googleId: profile.id,
6871
})
6972
.save()
70-
.then((newUser): void => {
73+
.then((newUser)/* : void */=> {
7174
//console.log('created new user: ', newUser);
7275
// res.locals.userId = newUser._id
7376
done(null, newUser);
@@ -80,11 +83,11 @@ module.exports = function (passport: Authenticator) {
8083
)
8184
);
8285

83-
passport.serializeUser((user, done): void => {
86+
passport.serializeUser((user, done)/* : void */=> {
8487
done(null, user);
8588
});
8689

87-
passport.deserializeUser((obj: (false | Express.User | null | undefined), done): void => {
90+
passport.deserializeUser((obj/* : (false | Express.User | null | undefined) */, done)/* : void */ => {
8891
done(null, obj);
8992
});
9093
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** using import statements in the electron / node files breaks npm start and nodepty
2+
* - types are left in place in these files for future iteration alternate import method is required for them to function
3+
*/
4+
// import { NextFunction, Request, Response } from "express";
5+
// import { cookieControllerType } from "../utils/backendTypes";
6+
7+
const cookieController /*: cookieControllerType*/ = {};
8+
9+
// Middleware to initialize a cookie when user logs in
10+
cookieController.setSSIDCookie = (req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: void*/ => {
11+
// eslint-disable-next-line no-useless-escape
12+
//removing double quotes with Regex?
13+
res.cookie('ssid', JSON.stringify(res.locals.userId).replace(/\"/g, ''));
14+
return next();
15+
};
16+
17+
// Middleware to delete a cookie upon user logging out
18+
cookieController.deleteCookie = (req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: void*/ => {
19+
res.clearCookie('ssid');
20+
return next();
21+
};
22+
23+
module.exports = cookieController;

server/controllers/cookieController.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/** using import statements in the electron / node files breaks npm start and nodepty
2+
* - types are left in place in these files for future iteration alternate import method is required for them to function
3+
*/
4+
// import { ErrorRequestHandler, NextFunction, Request, Response } from "express";
5+
// import { MongoError, FindCursor } from "mongodb";
6+
// import { sessionControllerType } from "../utils/backendTypes";
7+
8+
// Import the session model that defines schema of session
9+
const Session = require('../models/sessionModel');
10+
11+
const sessionController /*:sessionControllerType*/ = {};
12+
13+
// Middleware to initialize a session upon successful login
14+
sessionController.startSession = (req /* : Request */, res /* : Response */, next /* : NextFunction */) /* : void */ => {
15+
Session.create({ cookieId: res.locals.userId }, (err /* : MongoError */, result /* : void */) /* : void */ => {
16+
if (err && err.code !== 11000) return next(err);
17+
res.locals.ssid = res.locals.userId;
18+
return next();
19+
});
20+
};
21+
22+
// Middleware to end currently active sessions, if any
23+
sessionController.endSession = (req /* : Request */, res /*: Response */, next /* : NextFunction */)/*: void*/ => {
24+
Session.deleteMany({ cookieId: req.cookies.ssid }, (err /* : ErrorRequestHandler */) /* void */ => {
25+
if (err) return next(err);
26+
return next();
27+
});
28+
};
29+
30+
// Middleware to check if entered user is currently already logged in
31+
sessionController.isLoggedIn = (req /* :Request*/, res /* Response*/, next /* : NextFunction */) /* void */ => {
32+
Session.find({ cookieId: req.cookies.ssid }, (err /* : ErrorRequestHandler */, data /* : Array<{cookieId: String, createdAt: Date}> */) /* void */ => {
33+
if (err) return next(err);
34+
if (data.length === 0) return next('User Not Logged In');
35+
return next();
36+
});
37+
};
38+
39+
module.exports = sessionController;

server/controllers/sessionController.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/** using import statements in the electron / node files breaks npm start and nodepty
2+
* - types are left in place in these files for future iteration alternate import method is required for them to function
3+
*/
4+
// import { ErrorRequestHandler, NextFunction, Request, Response } from "express";
5+
// import { FindCursor } from "mongodb";
6+
// import { testStateControllerType } from "../utils/backendTypes";
7+
8+
// Import test state model that defines the structure of test stored in DB
9+
const TestState = require('../models/testStateModel');
10+
const testStateController /* :testStateControllerType */ = {};
11+
12+
// Middleware to upload a passed test into DB
13+
testStateController.upload = (req /* : Request */, res /* : Response */, next /* : NextFunction */) /* : void */ => {
14+
const { testName, testType, testState }/*:
15+
{ testName : string, testType : string, testState : Object }*/ = req.body;
16+
const userId /* : number */ = req.cookies.ssid;
17+
18+
TestState.create(
19+
{
20+
userId: String,
21+
testName: String,
22+
testType: String,
23+
testState: Object
24+
},
25+
(err /* : Error */) /* : void */ => {
26+
if (err) return next('Upload Failed');
27+
return next();
28+
}
29+
);
30+
};
31+
32+
// Middleware too get all saved tests of current user and of selected type
33+
testStateController.getTests = (req /* : Request */, res /* : Response */, next /* : NextFunction */) => {
34+
TestState.find({ userId: req.cookies.ssid, testType: req.params.testType }, (err /* : ErrorRequestHandler */, result /* : FindCursor */) /* : void */ => {
35+
// If an error occurs, invoke error handler with err object
36+
if (err) return next(err);
37+
// Save resulting tests array to locals object
38+
res.locals.tests = result;
39+
return next();
40+
});
41+
};
42+
43+
module.exports = testStateController;

server/controllers/testStateController.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

server/controllers/userController.ts renamed to server/controllers/userController.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { expression } from "@babel/template";
2-
import { ErrorRequestHandler, NextFunction, Request, Response } from "express";
3-
import { Cursor } from "mongoose";
4-
import { BasicGroupByOptions } from "rxjs";
5-
import { userControllerType } from "../utils/backendTypes";
1+
/** using import statements in the electron / node files breaks npm start and nodepty
2+
* - types are left in place in these files for future iteration alternate import method is required for them to function
3+
*/
4+
5+
// import { expression } from "@babel/template";
6+
// import { ErrorRequestHandler, NextFunction, Request, Response } from "express";
7+
// import { Cursor } from "mongoose";
8+
// import { BasicGroupByOptions } from "rxjs";
9+
// import { userControllerType } from "../utils/backendTypes";
610

711
/**
812
* Errors on lines 89-90 and 97-98 are due to Typescript not recognizing inherent _id property on MongoDB collections.
@@ -12,18 +16,18 @@ import { userControllerType } from "../utils/backendTypes";
1216
const { User } = require('../models/userModel');
1317
const bcrypt = require('bcryptjs');
1418

15-
const userController: userControllerType = {
19+
const userController/*: userControllerType*/ = {
1620

1721
// Middleware to encrypt passwords using bcrypt
18-
bcrypt: (req: Request, res: Response, next: NextFunction): void => {
22+
bcrypt: (req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: void*/ => {
1923
// The cost factor determines how much time is needed to calculate a single bcrypt hash
20-
const saltRounds: number = 10;
24+
const saltRounds = 10;
2125
// Destructure password from request body
22-
const { password }: { password: String } = req.body;
26+
const { password }/*: { password: String }*/ = req.body;
2327
// Generate the salt by passing in saltRounds (cost factor)
24-
bcrypt.genSalt(saltRounds, (err: ErrorRequestHandler, salt: String): void => {
28+
bcrypt.genSalt(saltRounds, (err/*: ErrorRequestHandler*/, salt/*: String*/)/*: void*/ => {
2529
// Hash a password by passing in the plaintext into the hash function
26-
bcrypt.hash(password, salt, (err: ErrorRequestHandler, hash: String): void => {
30+
bcrypt.hash(password, salt, (err/*: ErrorRequestHandler*/, hash/*: String*/)/*: void*/ => {
2731
// Save encrypted password into res.locals to be accessed later
2832
res.locals.encryptedPassword = hash;
2933
return next();
@@ -32,13 +36,13 @@ const userController: userControllerType = {
3236
},
3337

3438
// Middleware to save user information in database
35-
signup: (req: Request, res: Response, next: NextFunction): (void | Response) => {
39+
signup: (req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: (void | Response)*/ => {
3640
// collection.create method to insert new user
3741
User.create(
3842
// Pass in username from request body and encrypted password
39-
{ username: req.body.username, password: res.locals.encryptedPassword },
43+
{ username/*: req.body.username*/, password/*: res.locals.encryptedPassword*/ },
4044
// Callback to handle results of query
41-
(err: ErrorRequestHandler, newUser: (null | undefined | { _id: number })) => {
45+
(err/*: ErrorRequestHandler*/, newUser/*: (null | undefined | { _id: number })*/) => {
4246
if (!newUser) return res.status(400).json("Username already exists, please choose another one.");
4347
// If there is an error, invoke global error handler
4448
if (err) return next(err);
@@ -51,16 +55,16 @@ const userController: userControllerType = {
5155
},
5256

5357
// Middleware to check credentials and log user into application
54-
login: (req: Request, res: Response, next: NextFunction): void => {
58+
login: (req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: void*/ => {
5559
// Collection.find method to look for all user instances with passed username
56-
User.find({ username: req.body.username }, (err: ErrorRequestHandler,
57-
result: Array<{ _id: number, username: String, password: String }>): void => {
60+
User.find({ username/*: req.body.username*/ }, (err/*: ErrorRequestHandler*/,
61+
result/*: Array<{ _id: number, username: String, password: String }>*/)/*: void*/ => {
5862
// If there is an error, invoke global error handler
5963
if (err) return next(err);
6064
// If there are no matching usernames, invoke global error handler
6165
if (result.length === 0) return next('Incorrect username/password combo');
6266
// If there is a user with passed username, use the bcrypt.compare method to compare plaintext password with encrypted password
63-
bcrypt.compare(req.body.password, result[0].password, (err: ErrorRequestHandler, match: boolean) => {
67+
bcrypt.compare(req.body.password, result[0].password, (err/*: ErrorRequestHandler*/, match/*: boolean*/) => {
6468
// If an error occurs in the compare method, invoke global error handler
6569
if (err) return next(err);
6670
// If there is a match, invoke next middleware
@@ -74,25 +78,25 @@ const userController: userControllerType = {
7478
});
7579
},
7680

77-
getUsers: (req: Request, res: Response, next: NextFunction): void => {
81+
getUsers: (req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: void*/ => {
7882
// Collection.find method to look for all user instances with passed username
79-
User.find({}, (err: ErrorRequestHandler, result: Array<{ _id: number, username: String, password: String }>) => {
83+
User.find({}, (err/*: ErrorRequestHandler*/, result/*: Array<{ _id: number, username: String, password: String }>*/) => {
8084
// If there is an error, invoke global error handler
8185
if (err) return next(err);
8286
res.locals.users = result;
8387
return next();
8488
});
8589
},
8690

87-
githubLogin: (req: Request, res: Response, next: NextFunction): void => {
91+
githubLogin: (req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: void*/ => {
8892
// store user._id in res.locals
8993
if(!req.user || !req.user._id) throw new Error("User or user ID not defined.")
9094
res.locals.userId = req.user._id;
9195

9296
return next();
9397
},
9498

95-
googleLogin: (req: Request, res: Response, next: NextFunction): void => {
99+
googleLogin: (req/*: Request*/, res/*: Response*/, next/*: NextFunction*/)/*: void*/ => {
96100
// store user._id in res.locals
97101
if(!req.user || !req.user._id) throw new Error("User or user ID not defined.")
98102
res.locals.userId = req.user._id;

0 commit comments

Comments
 (0)