mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-02 21:32:42 +00:00
Merge pull request #2052 from G-Ambatte/addLocalLogin-#269
[LOCAL ONLY] Add passwordless login
This commit is contained in:
@@ -33,10 +33,12 @@ const Homebrew = createClass({
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
getInitialState : function(){
|
getInitialState : function() {
|
||||||
global.version = this.props.version;
|
|
||||||
global.account = this.props.account;
|
global.account = this.props.account;
|
||||||
|
global.version = this.props.version;
|
||||||
global.enable_v3 = this.props.enable_v3;
|
global.enable_v3 = this.props.enable_v3;
|
||||||
|
global.config = this.props.config;
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +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 request = require('superagent');
|
||||||
|
|
||||||
const Account = createClass({
|
const Account = createClass({
|
||||||
displayName : 'AccountNavItem',
|
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(){
|
render : function(){
|
||||||
|
// Logged in
|
||||||
if(global.account){
|
if(global.account){
|
||||||
return <Nav.dropdown>
|
return <Nav.dropdown>
|
||||||
<Nav.item
|
<Nav.item
|
||||||
@@ -64,6 +87,16 @@ const Account = createClass({
|
|||||||
</Nav.dropdown>;
|
</Nav.dropdown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Logged out
|
||||||
|
// LOCAL ONLY
|
||||||
|
if(global.config.local) {
|
||||||
|
return <Nav.item color='teal' icon='fas fa-sign-in-alt' onClick={this.localLogin}>
|
||||||
|
login
|
||||||
|
</Nav.item>;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Logged out
|
||||||
|
// Production site
|
||||||
return <Nav.item href={`https://www.naturalcrit.com/login?redirect=${this.state.url}`} color='teal' icon='fas fa-sign-in-alt'>
|
return <Nav.item href={`https://www.naturalcrit.com/login?redirect=${this.state.url}`} color='teal' icon='fas fa-sign-in-alt'>
|
||||||
login
|
login
|
||||||
</Nav.item>;
|
</Nav.item>;
|
||||||
|
|||||||
@@ -4,5 +4,6 @@
|
|||||||
"secret" : "secret",
|
"secret" : "secret",
|
||||||
"web_port" : 8000,
|
"web_port" : 8000,
|
||||||
"enable_v3" : true,
|
"enable_v3" : true,
|
||||||
|
"local_environments" : ["docker", "local"],
|
||||||
"publicUrl" : "https://homebrewery.naturalcrit.com"
|
"publicUrl" : "https://homebrewery.naturalcrit.com"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -259,9 +259,30 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{
|
|||||||
return 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
|
//Render the page
|
||||||
const templateFn = require('./../client/template.js');
|
const templateFn = require('./../client/template.js');
|
||||||
app.use((req, res)=>{
|
app.use((req, res)=>{
|
||||||
|
// Create configuration object
|
||||||
|
const configuration = {
|
||||||
|
local : isLocalEnvironment,
|
||||||
|
environment : nodeEnv
|
||||||
|
};
|
||||||
const props = {
|
const props = {
|
||||||
version : require('./../package.json').version,
|
version : require('./../package.json').version,
|
||||||
publicUrl : config.get('publicUrl') ?? '',
|
publicUrl : config.get('publicUrl') ?? '',
|
||||||
@@ -270,7 +291,8 @@ app.use((req, res)=>{
|
|||||||
brews : req.brews,
|
brews : req.brews,
|
||||||
googleBrews : req.googleBrews,
|
googleBrews : req.googleBrews,
|
||||||
account : req.account,
|
account : req.account,
|
||||||
enable_v3 : config.get('enable_v3')
|
enable_v3 : config.get('enable_v3'),
|
||||||
|
config : configuration
|
||||||
};
|
};
|
||||||
const title = req.brew ? req.brew.title : '';
|
const title = req.brew ? req.brew.title : '';
|
||||||
templateFn('homebrew', title, props)
|
templateFn('homebrew', title, props)
|
||||||
|
|||||||
Reference in New Issue
Block a user