diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/views/Main.tsx | 13 | ||||
-rw-r--r-- | src/server/authentication/config/passport.ts | 2 | ||||
-rw-r--r-- | src/server/authentication/controllers/user_controller.ts (renamed from src/server/authentication/controllers/user.ts) | 40 | ||||
-rw-r--r-- | src/server/authentication/models/user_model.ts (renamed from src/server/authentication/models/User.ts) | 22 | ||||
-rw-r--r-- | src/server/index.ts | 43 |
5 files changed, 85 insertions, 35 deletions
diff --git a/src/client/views/Main.tsx b/src/client/views/Main.tsx index 567f17752..e66816b6b 100644 --- a/src/client/views/Main.tsx +++ b/src/client/views/Main.tsx @@ -37,18 +37,25 @@ document.addEventListener("pointerdown", action(function (e: PointerEvent) { }), true) let mainDocId: string; -request.get(window.location.origin + "/getUserDocId", (error, response, body) => { +request.get(window.location.origin + "/getActiveWorkspaceId", (error, response, body) => { + const here = window.location.origin; if (body) { mainDocId = body; } else { mainDocId = Utils.GenerateGuid(); - request.post(window.location.origin + "/setUserDocId", { + request.post(here + "/addWorkspaceId", { body: { - userDocumentId: mainDocId + target: mainDocId }, json: true }) } + request.post(here + "/setActiveWorkspaceId", { + body: { + target: mainDocId + }, + json: true + }) init(); }) diff --git a/src/server/authentication/config/passport.ts b/src/server/authentication/config/passport.ts index 9f1303135..d90bedb18 100644 --- a/src/server/authentication/config/passport.ts +++ b/src/server/authentication/config/passport.ts @@ -2,7 +2,7 @@ import * as passport from 'passport' import * as passportLocal from 'passport-local'; import * as mongodb from 'mongodb'; import * as _ from "lodash"; -import { default as User } from '../models/User'; +import { default as User } from '../models/user_model'; import { Request, Response, NextFunction } from "express"; const LocalStrategy = passportLocal.Strategy; diff --git a/src/server/authentication/controllers/user.ts b/src/server/authentication/controllers/user_controller.ts index 6c4139b2e..899912ab7 100644 --- a/src/server/authentication/controllers/user.ts +++ b/src/server/authentication/controllers/user_controller.ts @@ -1,4 +1,4 @@ -import { default as User, UserModel, AuthToken } from "../models/User"; +import { default as User, DashUserModel, AuthToken } from "../models/user_model"; import { Request, Response, NextFunction } from "express"; import * as passport from "passport"; import { IVerifyOptions } from "passport-local"; @@ -71,11 +71,7 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => { User.findOne({ email }, (err, existingUser) => { if (err) { return next(err); } if (existingUser) { - if (existingUser) { - // existingUser.update({ $set: { email: please_work } }, (err, res) => { }); - } - req.flash("errors", "Account with that email address already exists."); - return res.redirect("/signup"); + return res.redirect("/login"); } user.save((err) => { if (err) { return next(err); } @@ -83,7 +79,7 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => { if (err) { return next(err); } - res.redirect("/"); + res.redirect("/home"); }); }); }); @@ -122,25 +118,41 @@ export let postLogin = (req: Request, res: Response, next: NextFunction) => { return res.redirect("/signup"); } - passport.authenticate("local", (err: Error, user: UserModel, info: IVerifyOptions) => { + passport.authenticate("local", (err: Error, user: DashUserModel, info: IVerifyOptions) => { if (err) { return next(err); } if (!user) { return res.redirect("/signup"); } req.logIn(user, (err) => { if (err) { return next(err); } - req.flash("success", "Success! You are logged in."); res.redirect("/home"); }); })(req, res, next); }; +export let getWorkspaces = (req: Request, res: Response) => { + const user: DashUserModel = req.user; + if (!user) { + return res.redirect("/login"); + } + res.render("workspace.pug", { + ids: user.allWorkspaceIds + }); +} + /** * GET /logout * Invokes the logout function on the request * and destroys the user's current session. */ export let getLogout = (req: Request, res: Response) => { + const dashUser: DashUserModel | undefined = req.user; + if (dashUser) { + dashUser.update({ $set: { didSelectSessionWorkspace: false } }, () => { }) + console.log("UPDATED :)"); + } else { + console.log("NO USER BY LOGOUT"); + } req.logout(); const sess = req.session; if (sess) { @@ -170,7 +182,7 @@ export let postForgot = function (req: Request, res: Response, next: NextFunctio }) }, function (token: string, done: any) { - User.findOne({ email }, function (err, user: UserModel) { + User.findOne({ email }, function (err, user: DashUserModel) { if (!user) { // NO ACCOUNT WITH SUBMITTED EMAIL return res.redirect('/forgot'); @@ -182,7 +194,7 @@ export let postForgot = function (req: Request, res: Response, next: NextFunctio }); }); }, - function (token: Uint16Array, user: UserModel, done: any) { + function (token: Uint16Array, user: DashUserModel, done: any) { const smtpTransport = nodemailer.createTransport({ service: 'Gmail', auth: { @@ -211,7 +223,7 @@ export let postForgot = function (req: Request, res: Response, next: NextFunctio } export let getReset = function (req: Request, res: Response) { - User.findOne({ passwordResetToken: req.params.token, passwordResetExpires: { $gt: Date.now() } }, function (err, user: UserModel) { + User.findOne({ passwordResetToken: req.params.token, passwordResetExpires: { $gt: Date.now() } }, function (err, user: DashUserModel) { if (!user || err) { return res.redirect('/forgot'); } @@ -225,7 +237,7 @@ export let getReset = function (req: Request, res: Response) { export let postReset = function (req: Request, res: Response) { async.waterfall([ function (done: any) { - User.findOne({ passwordResetToken: req.params.token, passwordResetExpires: { $gt: Date.now() } }, function (err, user: UserModel) { + User.findOne({ passwordResetToken: req.params.token, passwordResetExpires: { $gt: Date.now() } }, function (err, user: DashUserModel) { if (!user || err) { return res.redirect('back'); } @@ -254,7 +266,7 @@ export let postReset = function (req: Request, res: Response) { }); }); }, - function (user: UserModel, done: any) { + function (user: DashUserModel, done: any) { const smtpTransport = nodemailer.createTransport({ service: 'Gmail', auth: { diff --git a/src/server/authentication/models/User.ts b/src/server/authentication/models/user_model.ts index 433e2f6c3..dfd104ef8 100644 --- a/src/server/authentication/models/User.ts +++ b/src/server/authentication/models/user_model.ts @@ -15,12 +15,15 @@ mongoose.connection.on('error', function (error) { mongoose.connection.on('disconnected', function () { console.log('connection closed'); }); -export type UserModel = mongoose.Document & { +export type DashUserModel = mongoose.Document & { email: string, password: string, passwordResetToken: string | undefined, passwordResetExpires: Date | undefined, - tokens: AuthToken[], + + allWorkspaceIds: Array<String>, + activeWorkspaceId: String, + didSelectSessionWorkspace: Boolean, profile: { name: string, @@ -46,12 +49,19 @@ const userSchema = new mongoose.Schema({ passwordResetToken: String, passwordResetExpires: Date, - userDocumentId: String, + allWorkspaceIds: { + type: Array, + default: [] + }, + activeWorkspaceId: String, + didSelectSessionWorkspace: { + type: Boolean, + default: false + }, facebook: String, twitter: String, google: String, - tokens: Array, profile: { name: String, @@ -66,7 +76,7 @@ const userSchema = new mongoose.Schema({ * Password hash middleware. */ userSchema.pre("save", function save(next) { - const user = this as UserModel; + const user = this as DashUserModel; if (!user.isModified("password")) { return next(); } bcrypt.genSalt(10, (err, salt) => { if (err) { return next(err); } @@ -78,7 +88,7 @@ userSchema.pre("save", function save(next) { }); }); -const comparePassword: comparePasswordFunction = function (this: UserModel, candidatePassword, cb) { +const comparePassword: comparePasswordFunction = function (this: DashUserModel, candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, (err: mongoose.Error, isMatch: boolean) => { cb(err, isMatch); }); diff --git a/src/server/index.ts b/src/server/index.ts index 50f01fe31..6136b8d94 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -17,7 +17,7 @@ import * as bcrypt from "bcrypt-nodejs"; import { Document } from '../fields/Document'; import * as io from 'socket.io' import * as passportConfig from './authentication/config/passport'; -import { getLogin, postLogin, getSignup, postSignup, getLogout, getEntry, postReset, getForgot, postForgot, getReset } from './authentication/controllers/user'; +import { getLogin, postLogin, getSignup, postSignup, getLogout, getEntry, postReset, getForgot, postForgot, getReset, getWorkspaces } from './authentication/controllers/user_controller'; const config = require('../../webpack.config'); const compiler = webpack(config); const port = 1050; // default port to listen @@ -34,7 +34,7 @@ const MongoStore = require('connect-mongo')(session); const mongoose = require('mongoose'); import { performance } from 'perf_hooks' import * as path from 'path' -import User, { UserModel } from './authentication/models/User'; +import User, { DashUserModel } from './authentication/models/user_model'; const mongoUrl = 'mongodb://localhost:27017/Dash'; mongoose.connect(mongoUrl) @@ -81,27 +81,48 @@ app.use((req, res, next) => { // /home defines destination after a successful log in app.get("/home", (req, res) => { // if user is not logged in, redirect to log in page - if (!req.user) { - res.redirect("/login"); - return; + const dashUser: DashUserModel = req.user; + if (!dashUser) { + return res.redirect("/login"); } // otherwise, connect them to Dash // TODO: store and manage users' workspaces + if (dashUser.allWorkspaceIds.length > 0) { + if (!dashUser.didSelectSessionWorkspace) { + return res.redirect("/workspaces"); + } + } else { + console.log("OK, UPDATED TO TRUE"); + dashUser.update({ $set: { didSelectSessionWorkspace: true } }, () => { }) + } res.sendFile(path.join(__dirname, '../../deploy/index.html')); }); -app.get("/getUserDocId", (req, res) => { - if (!req.user) { +app.get("/workspaces", getWorkspaces); + +app.get("/getActiveWorkspaceId", (req, res) => { + const dashUser: DashUserModel = req.user; + if (!dashUser) { + return; + } + res.send(dashUser.activeWorkspaceId || ""); +}); + +app.post("/setActiveWorkspaceId", (req, res) => { + const dashUser: DashUserModel = req.user; + if (!dashUser) { return; } - res.send(req.user.userDocumentId || ""); + console.log(`Updating active workspace ID to ${req.body.target}`); + dashUser.update({ $set: { activeWorkspaceId: req.body.target } }, () => { }); }) -app.post("/setUserDocId", (req, res) => { - if (!req.user) { +app.post("/addWorkspaceId", (req, res) => { + const dashUser: DashUserModel = req.user; + if (!dashUser) { return; } - req.user.update({ $set: { userDocumentId: req.body.userDocumentId } }, () => { }); + dashUser.update({ $push: { allWorkspaceIds: req.body.target } }, () => { }); }) // anyone attempting to navigate to localhost at this port will |