diff --git a/client/homebrew/homebrew.jsx b/client/homebrew/homebrew.jsx index 732c06486..b41c7c59d 100644 --- a/client/homebrew/homebrew.jsx +++ b/client/homebrew/homebrew.jsx @@ -33,10 +33,12 @@ const Homebrew = createClass({ }; }, - getInitialState : function(){ - global.version = this.props.version; + getInitialState : function() { global.account = this.props.account; + global.version = this.props.version; global.enable_v3 = this.props.enable_v3; + global.config = this.props.config; + return {}; }, diff --git a/client/homebrew/navbar/account.navitem.jsx b/client/homebrew/navbar/account.navitem.jsx index be92cea92..7ac687a41 100644 --- a/client/homebrew/navbar/account.navitem.jsx +++ b/client/homebrew/navbar/account.navitem.jsx @@ -1,6 +1,7 @@ const React = require('react'); const createClass = require('create-react-class'); const Nav = require('naturalcrit/nav/nav.jsx'); +const request = require('superagent'); const Account = createClass({ displayName : 'AccountNavItem', @@ -36,7 +37,29 @@ const Account = createClass({ } }, + localLogin : async function(){ + const username = prompt('Enter username:'); + if(!username) {return;} + + const expiry = new Date; + expiry.setFullYear(expiry.getFullYear() + 1); + + const token = await request.post('/local/login') + .send({ username }) + .then((response)=>{ + return response.body; + }) + .catch((err)=>{ + console.warn(err); + }); + if(!token) return; + + document.cookie = `nc_session=${token};expires=${expiry};path=/;samesite=lax;${window.domain ? `domain=${window.domain}` : ''}`; + window.location.reload(true); + }, + render : function(){ + // Logged in if(global.account){ return ; } + // Logged out + // LOCAL ONLY + if(global.config.local) { + return + login + ; + }; + + // Logged out + // Production site return login ; diff --git a/config/default.json b/config/default.json index eded5231d..70c90593e 100644 --- a/config/default.json +++ b/config/default.json @@ -4,5 +4,6 @@ "secret" : "secret", "web_port" : 8000, "enable_v3" : true, + "local_environments" : ["docker", "local"], "publicUrl" : "https://homebrewery.naturalcrit.com" } diff --git a/server/app.js b/server/app.js index fe5c12594..381113a4a 100644 --- a/server/app.js +++ b/server/app.js @@ -259,9 +259,30 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{ return next(); })); +const nodeEnv = config.get('node_env'); +const isLocalEnvironment = config.get('local_environments').includes(nodeEnv); +// Local only +if(isLocalEnvironment){ + // Login + app.post('/local/login', (req, res)=>{ + const username = req.body.username; + if(!username) return; + + const payload = jwt.encode({ username: username, issued: new Date }, config.get('secret')); + return res.json(payload); + }); +} + + + //Render the page const templateFn = require('./../client/template.js'); app.use((req, res)=>{ + // Create configuration object + const configuration = { + local : isLocalEnvironment, + environment : nodeEnv + }; const props = { version : require('./../package.json').version, publicUrl : config.get('publicUrl') ?? '', @@ -270,7 +291,8 @@ app.use((req, res)=>{ brews : req.brews, googleBrews : req.googleBrews, account : req.account, - enable_v3 : config.get('enable_v3') + enable_v3 : config.get('enable_v3'), + config : configuration }; const title = req.brew ? req.brew.title : ''; templateFn('homebrew', title, props)