From c67a25ca2c098e2e079926770bc8b0cbe9c315f6 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 29 Mar 2018 11:40:47 -0700 Subject: [PATCH] fixed auth, fallback, and pages routes --- server/routes/auth/index.js | 19 +++++---- server/routes/auth/login.js | 42 +++++++++---------- server/routes/auth/logout.js | 8 ++-- server/routes/auth/signup.js | 16 ++++--- server/routes/auth/user.js | 14 +++---- server/routes/fallback/index.js | 5 +++ server/routes/fallback/sendReactApp.js | 7 ++++ server/routes/pages/index.js | 18 ++++---- server/routes/pages/sendEmbedPage.js | 12 +++--- server/routes/pages/sendReactApp.js | 6 +-- server/server.js | 16 ++----- server/{passport => speechPassport}/index.js | 0 .../local-login.js | 0 .../local-signup.js | 0 speech.js | 2 +- 15 files changed, 81 insertions(+), 84 deletions(-) create mode 100644 server/routes/fallback/index.js create mode 100644 server/routes/fallback/sendReactApp.js rename server/{passport => speechPassport}/index.js (100%) rename server/{passport => speechPassport}/local-login.js (100%) rename server/{passport => speechPassport}/local-signup.js (100%) diff --git a/server/routes/auth/index.js b/server/routes/auth/index.js index 455d79a6..a36dd78a 100644 --- a/server/routes/auth/index.js +++ b/server/routes/auth/index.js @@ -1,11 +1,12 @@ -const signup = require('./signup'); -const login = require('./login'); -const logout = require('./logout'); -const user = require('./user'); +const speechPassport = require('speechPassport'); +const handleSignupRequest = require('./signup'); +const handleLoginRequest = require('./login'); +const handleLogoutRequest = require('./logout'); +const handleUserRequest = require('./user'); -module.exports = { - signup, - login, - logout, - user, +module.exports = (app) => { + app.post('/signup', speechPassport.authenticate('local-signup'), handleSignupRequest); + app.post('/login', handleLoginRequest); + app.get('/logout', handleLogoutRequest); + app.get('/user', handleUserRequest); }; diff --git a/server/routes/auth/login.js b/server/routes/auth/login.js index 88a9bc4f..fcc4782d 100644 --- a/server/routes/auth/login.js +++ b/server/routes/auth/login.js @@ -1,28 +1,28 @@ -const login = (passport) => { - return (req, res, next) => { - passport.authenticate('local-login', (err, user, info) => { +const speechPassport = require('speechPassport'); + +const login = (req, res, next) => { + speechPassport.authenticate('local-login', (err, user, info) => { + if (err) { + return next(err); + } + if (!user) { + return res.status(400).json({ + success: false, + message: info.message, + }); + } + req.logIn(user, (err) => { if (err) { return next(err); } - if (!user) { - return res.status(400).json({ - success: false, - message: info.message, - }); - } - req.logIn(user, (err) => { - if (err) { - return next(err); - } - return res.status(200).json({ - success : true, - channelName : req.user.channelName, - channelClaimId: req.user.channelClaimId, - shortChannelId: req.user.shortChannelId, - }); + return res.status(200).json({ + success : true, + channelName : req.user.channelName, + channelClaimId: req.user.channelClaimId, + shortChannelId: req.user.shortChannelId, }); - })(req, res, next); - }; + }); + })(req, res, next); }; module.exports = login; diff --git a/server/routes/auth/logout.js b/server/routes/auth/logout.js index 7ee5997a..13d945b1 100644 --- a/server/routes/auth/logout.js +++ b/server/routes/auth/logout.js @@ -1,8 +1,6 @@ -const logout = () => { - return (req, res) => { - req.logout(); - res.status(200).json({success: true, message: 'you successfully logged out'}); - }; +const logout = (req, res) => { + req.logout(); + res.status(200).json({success: true, message: 'you successfully logged out'}); }; module.exports = logout; diff --git a/server/routes/auth/signup.js b/server/routes/auth/signup.js index 9c7d9da7..8db397cb 100644 --- a/server/routes/auth/signup.js +++ b/server/routes/auth/signup.js @@ -1,12 +1,10 @@ -const signup = () => { - return (req, res) => { - res.status(200).json({ - success : true, - channelName : req.user.channelName, - channelClaimId: req.user.channelClaimId, - shortChannelId: req.user.shortChannelId, - }); - }; +const signup = (req, res) => { + res.status(200).json({ + success : true, + channelName : req.user.channelName, + channelClaimId: req.user.channelClaimId, + shortChannelId: req.user.shortChannelId, + }); }; module.exports = signup; diff --git a/server/routes/auth/user.js b/server/routes/auth/user.js index 21dc607b..9e776bbd 100644 --- a/server/routes/auth/user.js +++ b/server/routes/auth/user.js @@ -1,11 +1,9 @@ -const user = () => { - return (req, res) => { - if (req.user) { - res.status(200).json({success: true, data: req.user}); - } else { - res.status(401).json({success: false, message: 'user is not logged in'}); - } - }; +const user = (req, res) => { + if (req.user) { + res.status(200).json({success: true, data: req.user}); + } else { + res.status(401).json({success: false, message: 'user is not logged in'}); + } }; module.exports = user; diff --git a/server/routes/fallback/index.js b/server/routes/fallback/index.js new file mode 100644 index 00000000..35073607 --- /dev/null +++ b/server/routes/fallback/index.js @@ -0,0 +1,5 @@ +const handlePageRequest = require('./sendReactApp'); + +module.exports = (app) => { + app.get('*', handlePageRequest); +}; diff --git a/server/routes/fallback/sendReactApp.js b/server/routes/fallback/sendReactApp.js new file mode 100644 index 00000000..b6aac94b --- /dev/null +++ b/server/routes/fallback/sendReactApp.js @@ -0,0 +1,7 @@ +const handlePageRender = require('helpers/handlePageRender.jsx'); + +const sendReactApp = (req, res) => { + handlePageRender(req, res); +}; + +module.exports = sendReactApp; diff --git a/server/routes/pages/index.js b/server/routes/pages/index.js index 63d73e34..5d4c27ae 100644 --- a/server/routes/pages/index.js +++ b/server/routes/pages/index.js @@ -1,11 +1,13 @@ -const sendReactApp = require('./sendReactApp'); -const sendEmbedPage = require('./sendEmbedPage'); +const handlePageRequest = require('./sendReactApp'); +const handleEmbedRequest = require('./sendEmbedPage'); const redirect = require('./redirect'); - - -module.exports = { - sendReactApp, - sendEmbedPage, - redirect, +module.exports = (app) => { + app.get('/', handlePageRequest); + app.get('/login', handlePageRequest); + app.get('/about', handlePageRequest); + app.get('/trending', redirect('/popular')); + app.get('/popular', handlePageRequest); + app.get('/new', handlePageRequest); + app.get('/embed/:claimId/:name', handleEmbedRequest); // route to send embedable video player (for twitter) }; diff --git a/server/routes/pages/sendEmbedPage.js b/server/routes/pages/sendEmbedPage.js index e1869173..c5e16460 100644 --- a/server/routes/pages/sendEmbedPage.js +++ b/server/routes/pages/sendEmbedPage.js @@ -1,12 +1,10 @@ const { details: { host } } = require('siteConfig.js'); -const sendEmbedPage = () => { - return ({ params }, res) => { - const claimId = params.claimId; - const name = params.name; - // get and render the content - res.status(200).render('embed', { layout: 'embed', host, claimId, name }); - }; +const sendEmbedPage = ({ params }, res) => { + const claimId = params.claimId; + const name = params.name; + // get and render the content + res.status(200).render('embed', { layout: 'embed', host, claimId, name }); }; module.exports = sendEmbedPage; diff --git a/server/routes/pages/sendReactApp.js b/server/routes/pages/sendReactApp.js index ea6fa019..b6aac94b 100644 --- a/server/routes/pages/sendReactApp.js +++ b/server/routes/pages/sendReactApp.js @@ -1,9 +1,7 @@ const handlePageRender = require('helpers/handlePageRender.jsx'); -const sendReactApp = () => { - return (req, res) => { - handlePageRender(req, res); - }; +const sendReactApp = (req, res) => { + handlePageRender(req, res); }; module.exports = sendReactApp; diff --git a/server/server.js b/server/server.js index a577536b..e732ca4f 100644 --- a/server/server.js +++ b/server/server.js @@ -4,11 +4,8 @@ const bodyParser = require('body-parser'); const expressHandlebars = require('express-handlebars'); const Handlebars = require('handlebars'); const helmet = require('helmet'); -const passport = require('passport'); -const { serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js'); const cookieSession = require('cookie-session'); const http = require('http'); -// logging dependencies const logger = require('winston'); function Server () { @@ -41,20 +38,15 @@ function Server () { }); // configure passport - passport.serializeUser(serializeSpeechUser); - passport.deserializeUser(deserializeSpeechUser); - const localSignupStrategy = require('./passport/local-signup.js'); - const localLoginStrategy = require('./passport/local-login.js'); - passport.use('local-signup', localSignupStrategy); - passport.use('local-login', localLoginStrategy); + const speechPassport = require('speechPassport'); // initialize passport app.use(cookieSession({ name : 'session', keys : [this.sessionKey], maxAge: 24 * 60 * 60 * 1000, // i.e. 24 hours })); - app.use(passport.initialize()); - app.use(passport.session()); + app.use(speechPassport.initialize()); + app.use(speechPassport.session()); // configure handlebars & register it with express app const hbs = expressHandlebars.create({ @@ -80,7 +72,7 @@ function Server () { this.server = http.Server(this.app); }; this.start = () => { - const db = require('./models/index'); + const db = require('./models/'); // sync sequelize db.sequelize.sync() // start the server diff --git a/server/passport/index.js b/server/speechPassport/index.js similarity index 100% rename from server/passport/index.js rename to server/speechPassport/index.js diff --git a/server/passport/local-login.js b/server/speechPassport/local-login.js similarity index 100% rename from server/passport/local-login.js rename to server/speechPassport/local-login.js diff --git a/server/passport/local-signup.js b/server/speechPassport/local-signup.js similarity index 100% rename from server/passport/local-signup.js rename to server/speechPassport/local-signup.js diff --git a/speech.js b/speech.js index 6a92d8d8..9fdc04b4 100644 --- a/speech.js +++ b/speech.js @@ -11,7 +11,7 @@ const logger = require('./config/loggerConfig.js'); const mysql = require('./config/mysqlConfig'); const site = require('./config/siteConfig'); const slack = require('./config/slackConfig.js'); -const passport = require('./server/passport/'); +const passport = require('./server/speechPassport/'); const models = require('./server/models/'); const exports = {