mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-05 18:52:38 +00:00
Shift JWT generation to app.js
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
const React = require('react');
|
const React = require('react');
|
||||||
const createClass = require('create-react-class');
|
const createClass = require('create-react-class');
|
||||||
const Nav = require('naturalcrit/nav/nav.jsx');
|
const Nav = require('naturalcrit/nav/nav.jsx');
|
||||||
const jwt = require('jwt-simple');
|
const request = require('superagent');
|
||||||
|
|
||||||
const Account = createClass({
|
const Account = createClass({
|
||||||
displayName : 'AccountNavItem',
|
displayName : 'AccountNavItem',
|
||||||
@@ -29,17 +29,22 @@ const Account = createClass({
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
localLogin : function(){
|
localLogin : async function(){
|
||||||
const username = prompt('Enter username:');
|
const username = prompt('Enter username:');
|
||||||
if(!username) {return;};
|
if(!username) {return;};
|
||||||
|
|
||||||
const payload = {
|
|
||||||
username : username,
|
|
||||||
issued : new Date()
|
|
||||||
};
|
|
||||||
const expiry = new Date;
|
const expiry = new Date;
|
||||||
expiry.setFullYear(expiry.getFullYear() + 1);
|
expiry.setFullYear(expiry.getFullYear() + 1);
|
||||||
const token = jwt.encode(payload, global.config.secret);
|
|
||||||
|
const token = await request.post('/login')
|
||||||
|
.send({ username })
|
||||||
|
.then((response)=>{
|
||||||
|
return response.body;
|
||||||
|
})
|
||||||
|
.catch((err)=>{
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
if(!token) return;
|
||||||
|
|
||||||
document.cookie = `nc_session=${token};expires=${expiry};path=/;samesite=lax;${window.domain ? `domain=${window.domain}` : ''}`;
|
document.cookie = `nc_session=${token};expires=${expiry};path=/;samesite=lax;${window.domain ? `domain=${window.domain}` : ''}`;
|
||||||
window.location.reload(true);
|
window.location.reload(true);
|
||||||
|
|||||||
@@ -259,6 +259,23 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{
|
|||||||
return next();
|
return next();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
const localEnvironments = config.get('local_environments');
|
||||||
|
// Login
|
||||||
|
app.post('/login', (req, res)=>{
|
||||||
|
// Local only
|
||||||
|
if(!localEnvironments.includes(config.get('node_env'))){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
//Render the page
|
||||||
const templateFn = require('./../client/template.js');
|
const templateFn = require('./../client/template.js');
|
||||||
app.use((req, res)=>{
|
app.use((req, res)=>{
|
||||||
@@ -268,10 +285,8 @@ app.use((req, res)=>{
|
|||||||
environment : config.get('node_env')
|
environment : config.get('node_env')
|
||||||
};
|
};
|
||||||
// Add local only items to configuration object
|
// Add local only items to configuration object
|
||||||
const localEnvironments = config.get('local_environments');
|
|
||||||
if(localEnvironments.includes(configuration.environment)){
|
if(localEnvironments.includes(configuration.environment)){
|
||||||
configuration.local = true;
|
configuration.local = true;
|
||||||
configuration.secret = config.get('secret');
|
|
||||||
};
|
};
|
||||||
const props = {
|
const props = {
|
||||||
version : require('./../package.json').version,
|
version : require('./../package.json').version,
|
||||||
|
|||||||
Reference in New Issue
Block a user