aboutsummaryrefslogtreecommitdiff
path: root/src/server/authentication
diff options
context:
space:
mode:
authorbob <bcz@cs.brown.edu>2019-12-02 13:22:02 -0500
committerbob <bcz@cs.brown.edu>2019-12-02 13:22:02 -0500
commit1ef06e189a352e5472ee267d44d4b3c96042f03c (patch)
treeeefed629be388e83dc71a7b3c574326fc0343f06 /src/server/authentication
parent1280c005829cf49fd106fd872afcf4ed6593a2f6 (diff)
parent0595f93dde717b7b6990e9a81c5b43a73a3808d5 (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web
Diffstat (limited to 'src/server/authentication')
-rw-r--r--src/server/authentication/config/passport.ts5
-rw-r--r--src/server/authentication/controllers/user_controller.ts35
-rw-r--r--src/server/authentication/models/current_user_utils.ts13
-rw-r--r--src/server/authentication/models/user_model.ts14
4 files changed, 23 insertions, 44 deletions
diff --git a/src/server/authentication/config/passport.ts b/src/server/authentication/config/passport.ts
index 8915a4abf..726df7fd7 100644
--- a/src/server/authentication/config/passport.ts
+++ b/src/server/authentication/config/passport.ts
@@ -3,7 +3,6 @@ import * as passportLocal from 'passport-local';
import _ from "lodash";
import { default as User } from '../models/user_model';
import { Request, Response, NextFunction } from "express";
-import { RouteStore } from '../../RouteStore';
const LocalStrategy = passportLocal.Strategy;
@@ -35,13 +34,13 @@ export let isAuthenticated = (req: Request, res: Response, next: NextFunction) =
if (req.isAuthenticated()) {
return next();
}
- return res.redirect(RouteStore.login);
+ return res.redirect("/login");
};
export let isAuthorized = (req: Request, res: Response, next: NextFunction) => {
const provider = req.path.split("/").slice(-1)[0];
- if (_.find((req.user as any).tokens, { kind: provider })) {
+ if (_.find((req.user as any).tokens!, { kind: provider })) {
next();
} else {
res.redirect(`/auth/${provider}`);
diff --git a/src/server/authentication/controllers/user_controller.ts b/src/server/authentication/controllers/user_controller.ts
index f5c6e1610..517353479 100644
--- a/src/server/authentication/controllers/user_controller.ts
+++ b/src/server/authentication/controllers/user_controller.ts
@@ -3,17 +3,11 @@ import { Request, Response, NextFunction } from "express";
import * as passport from "passport";
import { IVerifyOptions } from "passport-local";
import "../config/passport";
-import * as request from "express-validator";
import flash = require("express-flash");
-import * as session from "express-session";
-import * as pug from 'pug';
import * as async from 'async';
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";
/**
@@ -23,8 +17,7 @@ import { MailOptions } from "nodemailer/lib/stream-transport";
*/
export let getSignup = (req: Request, res: Response) => {
if (req.user) {
- let user = req.user;
- return res.redirect(RouteStore.home);
+ return res.redirect("/home");
}
res.render("signup.pug", {
title: "Sign Up",
@@ -45,7 +38,7 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => {
const errors = req.validationErrors();
if (errors) {
- return res.redirect(RouteStore.signup);
+ return res.redirect("/signup");
}
const email = req.body.email as String;
@@ -62,7 +55,7 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => {
User.findOne({ email }, (err, existingUser) => {
if (err) { return next(err); }
if (existingUser) {
- return res.redirect(RouteStore.login);
+ return res.redirect("/login");
}
user.save((err: any) => {
if (err) { return next(err); }
@@ -81,7 +74,7 @@ let tryRedirectToTarget = (req: Request, res: Response) => {
req.session.target = undefined;
res.redirect(target);
} else {
- res.redirect(RouteStore.home);
+ res.redirect("/home");
}
};
@@ -93,7 +86,7 @@ let tryRedirectToTarget = (req: Request, res: Response) => {
export let getLogin = (req: Request, res: Response) => {
if (req.user) {
req.session!.target = undefined;
- return res.redirect(RouteStore.home);
+ return res.redirect("/home");
}
res.render("login.pug", {
title: "Log In",
@@ -115,13 +108,13 @@ export let postLogin = (req: Request, res: Response, next: NextFunction) => {
if (errors) {
req.flash("errors", "Unable to login at this time. Please try again.");
- return res.redirect(RouteStore.signup);
+ return res.redirect("/signup");
}
passport.authenticate("local", (err: Error, user: DashUserModel, info: IVerifyOptions) => {
if (err) { next(err); return; }
if (!user) {
- return res.redirect(RouteStore.signup);
+ return res.redirect("/signup");
}
req.logIn(user, (err) => {
if (err) { next(err); return; }
@@ -141,7 +134,7 @@ export let getLogout = (req: Request, res: Response) => {
if (sess) {
sess.destroy((err) => { if (err) { console.log(err); } });
}
- res.redirect(RouteStore.login);
+ res.redirect("/login");
};
export let getForgot = function (req: Request, res: Response) {
@@ -168,7 +161,7 @@ export let postForgot = function (req: Request, res: Response, next: NextFunctio
User.findOne({ email }, function (err, user: DashUserModel) {
if (!user) {
// NO ACCOUNT WITH SUBMITTED EMAIL
- res.redirect(RouteStore.forgot);
+ res.redirect("/forgotPassword");
return;
}
user.passwordResetToken = token;
@@ -192,7 +185,7 @@ export let postForgot = function (req: Request, res: Response, next: NextFunctio
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' +
+ 'http://' + req.headers.host + '/resetPassword/' + token + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n'
} as MailOptions;
smtpTransport.sendMail(mailOptions, function (err: Error | null) {
@@ -202,14 +195,14 @@ export let postForgot = function (req: Request, res: Response, next: NextFunctio
}
], function (err) {
if (err) return next(err);
- res.redirect(RouteStore.forgot);
+ res.redirect("/forgotPassword");
});
};
export let getReset = function (req: Request, res: Response) {
User.findOne({ passwordResetToken: req.params.token, passwordResetExpires: { $gt: Date.now() } }, function (err, user: DashUserModel) {
if (!user || err) {
- return res.redirect(RouteStore.forgot);
+ return res.redirect("/forgotPassword");
}
res.render("reset.pug", {
title: "Reset Password",
@@ -239,7 +232,7 @@ export let postReset = function (req: Request, res: Response) {
user.save(function (err) {
if (err) {
- res.redirect(RouteStore.login);
+ res.redirect("/login");
return;
}
req.logIn(user, function (err) {
@@ -271,6 +264,6 @@ export let postReset = function (req: Request, res: Response) {
});
}
], function (err) {
- res.redirect(RouteStore.login);
+ res.redirect("/login");
});
}; \ No newline at end of file
diff --git a/src/server/authentication/models/current_user_utils.ts b/src/server/authentication/models/current_user_utils.ts
index 5b9bba47d..ac4462f78 100644
--- a/src/server/authentication/models/current_user_utils.ts
+++ b/src/server/authentication/models/current_user_utils.ts
@@ -1,4 +1,4 @@
-import { action, computed, observable, reaction, runInAction } from "mobx";
+import { action, computed, observable, reaction } from "mobx";
import * as rp from 'request-promise';
import { DocServer } from "../../../client/DocServer";
import { Docs } from "../../../client/documents/Documents";
@@ -11,10 +11,9 @@ import { listSpec } from "../../../new_fields/Schema";
import { ScriptField, ComputedField } from "../../../new_fields/ScriptField";
import { Cast, PromiseValue } from "../../../new_fields/Types";
import { Utils } from "../../../Utils";
-import { RouteStore } from "../../RouteStore";
-import { InkingControl } from "../../../client/views/InkingControl";
-import { DragManager } from "../../../client/util/DragManager";
import { nullAudio } from "../../../new_fields/URLField";
+import { DragManager } from "../../../client/util/DragManager";
+import { InkingControl } from "../../../client/views/InkingControl";
export class CurrentUserUtils {
private static curr_id: string;
@@ -206,8 +205,8 @@ export class CurrentUserUtils {
return doc;
}
- public static loadCurrentUser() {
- return rp.get(Utils.prepend(RouteStore.getCurrUser)).then(response => {
+ public static async loadCurrentUser() {
+ return rp.get(Utils.prepend("/getCurrentUser")).then(response => {
if (response) {
const result: { id: string, email: string } = JSON.parse(response);
return result;
@@ -220,7 +219,7 @@ export class CurrentUserUtils {
public static async loadUserDocument({ id, email }: { id: string, email: string }) {
this.curr_id = id;
Doc.CurrentUserEmail = email;
- await rp.get(Utils.prepend(RouteStore.getUserDocumentId)).then(id => {
+ await rp.get(Utils.prepend("/getUserDocumentId")).then(id => {
if (id && id !== "guest") {
return DocServer.GetRefField(id).then(async field =>
Doc.SetUserDoc(await this.updateUserDocument(field instanceof Doc ? field : new Doc(id, true))));
diff --git a/src/server/authentication/models/user_model.ts b/src/server/authentication/models/user_model.ts
index 45fbf23b1..cc670a03a 100644
--- a/src/server/authentication/models/user_model.ts
+++ b/src/server/authentication/models/user_model.ts
@@ -1,20 +1,8 @@
//@ts-ignore
import * as bcrypt from "bcrypt-nodejs";
//@ts-ignore
-import * as mongoose from "mongoose";
-var url = 'mongodb://localhost:27017/Dash';
+import * as mongoose from 'mongoose';
-mongoose.connect(url, { useNewUrlParser: true });
-
-mongoose.connection.on('connected', function () {
- console.log('Stablished connection on ' + url);
-});
-mongoose.connection.on('error', function (error) {
- console.log('Something wrong happened: ' + error);
-});
-mongoose.connection.on('disconnected', function () {
- console.log('connection closed');
-});
export type DashUserModel = mongoose.Document & {
email: String,
password: string,