diff options
author | Sam Wilkins <samuel_wilkins@brown.edu> | 2019-07-16 11:56:25 -0400 |
---|---|---|
committer | Sam Wilkins <samuel_wilkins@brown.edu> | 2019-07-16 11:56:25 -0400 |
commit | f36afcf2b00da1df5aa39deff5d9e931a823605c (patch) | |
tree | b444209b353b951def3ee457845fa54f2bcab709 | |
parent | 4c127ddd32b20bc6f1e6fb6b921e06cf3de4f1ca (diff) |
mongoose connection bug and usercontroller errors fixed
-rw-r--r-- | package.json | 4 | ||||
-rw-r--r-- | src/server/authentication/controllers/user_controller.ts | 25 | ||||
-rw-r--r-- | src/server/authentication/models/user_model.ts | 2 | ||||
-rw-r--r-- | src/server/index.ts | 2 |
4 files changed, 19 insertions, 14 deletions
diff --git a/package.json b/package.json index 22b3a6b21..7407a719f 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "@types/lodash": "^4.14.121", "@types/mobile-detect": "^1.3.4", "@types/mongodb": "^3.1.22", - "@types/mongoose": "^5.3.21", + "@types/mongoose": "^5.5.8", "@types/node": "^10.12.30", "@types/nodemailer": "^4.6.6", "@types/passport": "^1.0.0", @@ -144,7 +144,7 @@ "mobx-react-devtools": "^6.1.1", "mobx-utils": "^5.4.0", "mongodb": "^3.1.13", - "mongoose": "^5.4.18", + "mongoose": "^5.6.4", "node-sass": "^4.12.0", "nodemailer": "^5.1.1", "nodemon": "^1.18.10", diff --git a/src/server/authentication/controllers/user_controller.ts b/src/server/authentication/controllers/user_controller.ts index ca4fc171c..fa1cd647d 100644 --- a/src/server/authentication/controllers/user_controller.ts +++ b/src/server/authentication/controllers/user_controller.ts @@ -12,6 +12,9 @@ import * as nodemailer from 'nodemailer'; import c = require("crypto"); import { RouteStore } from "../../RouteStore"; import { Utils } from "../../../Utils"; +import { Schema } from "mongoose"; +import { Opt } from "../../../new_fields/Doc"; +import { MailOptions } from "nodemailer/lib/stream-transport"; /** * GET /signup @@ -45,21 +48,23 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => { return res.redirect(RouteStore.signup); } - const email = req.body.email; + const email = req.body.email as String; const password = req.body.password; - const user = new User({ - email, + const model = { + email: { type: email, unique: true }, password, userDocumentId: Utils.GenerateGuid() - }); + } as Partial<DashUserModel>; + + const user = new User(model); User.findOne({ email }, (err, existingUser) => { if (err) { return next(err); } if (existingUser) { return res.redirect(RouteStore.login); } - user.save((err) => { + user.save((err: any) => { if (err) { return next(err); } req.logIn(user, (err) => { if (err) { return next(err); } @@ -181,15 +186,15 @@ export let postForgot = function (req: Request, res: Response, next: NextFunctio } }); const mailOptions = { - to: user.email, + to: user.email.type, from: 'brownptcdash@gmail.com', subject: 'Dash Password Reset', text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' + 'Please click on the following link, or paste this into your browser to complete the process:\n\n' + 'http://' + req.headers.host + '/reset/' + token + '\n\n' + 'If you did not request this, please ignore this email and your password will remain unchanged.\n' - }; - smtpTransport.sendMail(mailOptions, function (err) { + } as MailOptions; + smtpTransport.sendMail(mailOptions, function (err: Error | null) { // req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.'); done(null, err, 'done'); }); @@ -254,12 +259,12 @@ export let postReset = function (req: Request, res: Response) { } }); const mailOptions = { - to: user.email, + to: user.email.type, from: 'brownptcdash@gmail.com', subject: 'Your password has been changed', text: 'Hello,\n\n' + 'This is a confirmation that the password for your account ' + user.email + ' has just been changed.\n' - }; + } as MailOptions; smtpTransport.sendMail(mailOptions, function (err) { done(null, err); }); diff --git a/src/server/authentication/models/user_model.ts b/src/server/authentication/models/user_model.ts index ee85e1c05..fb62de1c8 100644 --- a/src/server/authentication/models/user_model.ts +++ b/src/server/authentication/models/user_model.ts @@ -16,7 +16,7 @@ mongoose.connection.on('disconnected', function () { console.log('connection closed'); }); export type DashUserModel = mongoose.Document & { - email: string, + email: { type: String, unique: true }, password: string, passwordResetToken?: string, passwordResetExpires?: Date, diff --git a/src/server/index.ts b/src/server/index.ts index 1c0dec05b..06f8358e1 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -58,7 +58,7 @@ clientUtils = `//AUTO-GENERATED FILE: DO NOT EDIT\n${clientUtils.replace('"mode" fs.writeFileSync("./src/client/util/ClientUtils.ts", clientUtils, "utf8"); const mongoUrl = 'mongodb://localhost:27017/Dash'; -mongoose.connect(mongoUrl); +mongoose.connection.readyState === 0 && mongoose.connect(mongoUrl); mongoose.connection.on('connected', () => console.log("connected")); // SESSION MANAGEMENT AND AUTHENTICATION MIDDLEWARE |