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";
6
10
7
11
/**
8
12
* 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";
12
16
const { User } = require ( '../models/userModel' ) ;
13
17
const bcrypt = require ( 'bcryptjs' ) ;
14
18
15
- const userController : userControllerType = {
19
+ const userController /* : userControllerType*/ = {
16
20
17
21
// Middleware to encrypt passwords using bcrypt
18
- bcrypt : ( req : Request , res : Response , next : NextFunction ) : void => {
22
+ bcrypt : ( req /* : Request*/ , res /* : Response*/ , next /* : NextFunction*/ ) /* : void*/ => {
19
23
// The cost factor determines how much time is needed to calculate a single bcrypt hash
20
- const saltRounds : number = 10 ;
24
+ const saltRounds = 10 ;
21
25
// Destructure password from request body
22
- const { password } : { password : String } = req . body ;
26
+ const { password } /* : { password: String }*/ = req . body ;
23
27
// 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*/ => {
25
29
// 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*/ => {
27
31
// Save encrypted password into res.locals to be accessed later
28
32
res . locals . encryptedPassword = hash ;
29
33
return next ( ) ;
@@ -32,13 +36,13 @@ const userController: userControllerType = {
32
36
} ,
33
37
34
38
// 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)*/ => {
36
40
// collection.create method to insert new user
37
41
User . create (
38
42
// 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*/ } ,
40
44
// Callback to handle results of query
41
- ( err : ErrorRequestHandler , newUser : ( null | undefined | { _id : number } ) ) => {
45
+ ( err /* : ErrorRequestHandler*/ , newUser /* : (null | undefined | { _id: number })*/ ) => {
42
46
if ( ! newUser ) return res . status ( 400 ) . json ( "Username already exists, please choose another one." ) ;
43
47
// If there is an error, invoke global error handler
44
48
if ( err ) return next ( err ) ;
@@ -51,16 +55,16 @@ const userController: userControllerType = {
51
55
} ,
52
56
53
57
// 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*/ => {
55
59
// 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*/ => {
58
62
// If there is an error, invoke global error handler
59
63
if ( err ) return next ( err ) ;
60
64
// If there are no matching usernames, invoke global error handler
61
65
if ( result . length === 0 ) return next ( 'Incorrect username/password combo' ) ;
62
66
// 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*/ ) => {
64
68
// If an error occurs in the compare method, invoke global error handler
65
69
if ( err ) return next ( err ) ;
66
70
// If there is a match, invoke next middleware
@@ -74,25 +78,25 @@ const userController: userControllerType = {
74
78
} ) ;
75
79
} ,
76
80
77
- getUsers : ( req : Request , res : Response , next : NextFunction ) : void => {
81
+ getUsers : ( req /* : Request*/ , res /* : Response*/ , next /* : NextFunction*/ ) /* : void*/ => {
78
82
// 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 }>*/ ) => {
80
84
// If there is an error, invoke global error handler
81
85
if ( err ) return next ( err ) ;
82
86
res . locals . users = result ;
83
87
return next ( ) ;
84
88
} ) ;
85
89
} ,
86
90
87
- githubLogin : ( req : Request , res : Response , next : NextFunction ) : void => {
91
+ githubLogin : ( req /* : Request*/ , res /* : Response*/ , next /* : NextFunction*/ ) /* : void*/ => {
88
92
// store user._id in res.locals
89
93
if ( ! req . user || ! req . user . _id ) throw new Error ( "User or user ID not defined." )
90
94
res . locals . userId = req . user . _id ;
91
95
92
96
return next ( ) ;
93
97
} ,
94
98
95
- googleLogin : ( req : Request , res : Response , next : NextFunction ) : void => {
99
+ googleLogin : ( req /* : Request*/ , res /* : Response*/ , next /* : NextFunction*/ ) /* : void*/ => {
96
100
// store user._id in res.locals
97
101
if ( ! req . user || ! req . user . _id ) throw new Error ( "User or user ID not defined." )
98
102
res . locals . userId = req . user . _id ;
0 commit comments