mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 16:22:44 +00:00
Shift JWT generation to app.js
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
const React = require('react');
|
||||
const createClass = require('create-react-class');
|
||||
const Nav = require('naturalcrit/nav/nav.jsx');
|
||||
const jwt = require('jwt-simple');
|
||||
const request = require('superagent');
|
||||
|
||||
const Account = createClass({
|
||||
displayName : 'AccountNavItem',
|
||||
@@ -29,17 +29,22 @@ const Account = createClass({
|
||||
};
|
||||
},
|
||||
|
||||
localLogin : function(){
|
||||
localLogin : async function(){
|
||||
const username = prompt('Enter username:');
|
||||
if(!username) {return;};
|
||||
|
||||
const payload = {
|
||||
username : username,
|
||||
issued : new Date()
|
||||
};
|
||||
const expiry = new Date;
|
||||
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}` : ''}`;
|
||||
window.location.reload(true);
|
||||
|
||||
@@ -259,6 +259,23 @@ app.get('/print/:id', asyncHandler(async (req, res, 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
|
||||
const templateFn = require('./../client/template.js');
|
||||
app.use((req, res)=>{
|
||||
@@ -268,10 +285,8 @@ app.use((req, res)=>{
|
||||
environment : config.get('node_env')
|
||||
};
|
||||
// Add local only items to configuration object
|
||||
const localEnvironments = config.get('local_environments');
|
||||
if(localEnvironments.includes(configuration.environment)){
|
||||
configuration.local = true;
|
||||
configuration.secret = config.get('secret');
|
||||
};
|
||||
const props = {
|
||||
version : require('./../package.json').version,
|
||||
|
||||
Reference in New Issue
Block a user