diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/server/authentication/controllers/user.ts | 45 | ||||
-rw-r--r-- | src/server/index.ts | 61 |
2 files changed, 64 insertions, 42 deletions
diff --git a/src/server/authentication/controllers/user.ts b/src/server/authentication/controllers/user.ts index feb5ba4aa..72fdd5137 100644 --- a/src/server/authentication/controllers/user.ts +++ b/src/server/authentication/controllers/user.ts @@ -5,12 +5,34 @@ import { IVerifyOptions } from "passport-local"; import "../config/passport"; import * as request from "express-validator"; const flash = require("express-flash"); +import * as path from 'path' import * as session from "express-session"; import * as pug from 'pug'; /** + * GET / + * Whenever a user navigates to the root of Dash + * (doesn't specify a sub-route), redirect to login. + */ +export let getEntry = (req: Request, res: Response) => { + res.redirect("/login"); +} + +export let getHome = (req: Request, res: Response) => { + // if user is not logged in, redirect to log in page + if (!req.user) { + res.redirect("/login"); + return; + } + // otherwise, connect them to Dash + // TODO: store and manage users' workspaces + res.sendFile(path.join(__dirname, '../../deploy/index.html')); +} + +/** * GET /signup - * Signup page. + * Directs user to the signup page + * modeled by signup.pug in views */ export let getSignup = (req: Request, res: Response) => { if (req.user) { @@ -57,7 +79,7 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => { if (err) { return next(err); } if (existingUser) { if (existingUser) { - existingUser.update({ $set: { email : please_work } }, (err, res) => {}); + existingUser.update({ $set: { email: please_work } }, (err, res) => { }); } req.flash("errors", "Account with that email address already exists."); return res.redirect("/signup"); @@ -72,7 +94,7 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => { }); }); }); - + }; @@ -92,6 +114,7 @@ export let getLogin = (req: Request, res: Response) => { /** * POST /login * Sign in using email and password. + * On failure, redirect to login page */ export let postLogin = (req: Request, res: Response, next: NextFunction) => { req.assert("email", "Email is not valid").isEmail(); @@ -117,4 +140,18 @@ export let postLogin = (req: Request, res: Response, next: NextFunction) => { res.redirect("/home"); }); })(req, res, next); -};
\ No newline at end of file +}; + +/** + * GET /logout + * Invokes the logout function on the request + * and destroys the user's current session. + */ +export let getLogout = (req: Request, res: Response) => { + req.logout(); + const sess = req.session; + if (sess) { + sess.destroy((err) => { if (err) { console.log(err); } }); + } + res.redirect('/login'); +}
\ No newline at end of file diff --git a/src/server/index.ts b/src/server/index.ts index 3f7f73b39..d097b4aec 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -3,7 +3,6 @@ const app = express() import * as webpack from 'webpack' import * as wdm from 'webpack-dev-middleware'; import * as whm from 'webpack-hot-middleware'; -import * as path from 'path' import * as passport from 'passport'; import { MessageStore, Message, SetFieldArgs, GetFieldArgs, Transferable } from "./Message"; import { Client } from './Client'; @@ -17,7 +16,7 @@ import { ObjectID } from 'mongodb'; import { Document } from '../fields/Document'; import * as io from 'socket.io' import * as passportConfig from './authentication/config/passport'; -import { getLogin, postLogin, getSignup, postSignup } from './authentication/controllers/user'; +import { getLogin, postLogin, getSignup, postSignup, getLogout, getEntry, getHome } from './authentication/controllers/user'; const config = require('../../webpack.config'); const compiler = webpack(config); const port = 1050; // default port to listen @@ -46,6 +45,9 @@ mongoose.connection.on('connected', function () { console.log("connected"); }) +// SESSION MANAGEMENT AND AUTHENTICATION MIDDLEWARE +// ORDER OF IMPORTS MATTERS + app.use(cookieParser("secret")); app.use(session({ secret: `${c.randomBytes(64)}`, @@ -68,33 +70,34 @@ app.use((req, res, next) => { next(); }); +// AUTHENTICATION ROUTING + +// *** +// Look for the definitions of these get and post +// functions in the exports of user.ts + +// /home defines destination after a successful log in +app.get("/home", getHome); + +// anyone attempting to navigate to localhost at this port will +// first have to login +app.get("/", getEntry); + +// Sign Up app.get("/signup", getSignup); -// app.post('/signup', passport.authenticate('local-signup', { -// successRedirect : '/profile', // redirect to the secure profile section -// failureRedirect : '/signup', // redirect back to the signup page if there is an error -// failureFlash : true // allow flash messages -// })); app.post("/signup", postSignup); + +// Log In app.get("/login", getLogin); app.post("/login", postLogin); +// Log Out +app.get('/logout', getLogout); +// *** let FieldStore: ObservableMap<FIELD_ID, Field> = new ObservableMap(); -// define a route handler for the default home page -app.get("/home", (req, res) => { - if (!req.user) { - res.redirect("/login"); - return; - } - res.sendFile(path.join(__dirname, '../../deploy/index.html')); -}); - -app.get("/", (req, res) => { - res.redirect("/login"); -}); - app.get("/hello", (req, res) => { res.send("<p>Hello</p>"); }) @@ -104,24 +107,6 @@ app.get("/delete", (req, res) => { res.redirect("/"); }); -app.get('/logout', function(req, res){ - req.logout(); - const sess = req.session; - if (sess) { - sess.destroy((err) => { - if (err) { - console.log("ERRRRRRROOOOOOOOORRRRRRRR IN LOG OUT"); - console.log(err); - return; - } - // return res.send({ authenticated: req.isAuthenticated() }); - }); - res.redirect('/login'); - } else { - res.redirect('/'); - } -}); - app.use(wdm(compiler, { publicPath: config.output.publicPath })) |