const React = require('react'); const createClass = require('create-react-class'); const Nav = require('naturalcrit/nav/nav.jsx'); const jwt = require('jwt-simple'); const Account = createClass({ displayName : 'AccountNavItem', getInitialState : function() { return { url : '' }; }, componentDidMount : function(){ if(typeof window !== 'undefined'){ this.setState({ url : window.location.href }); } }, handleLogout : function(){ if(confirm('Are you sure you want to log out?')) { // Reset divider position window.localStorage.removeItem('naturalcrit-pane-split'); // Clear login cookie document.cookie = `nc_session=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;samesite=lax;${window.domain ? `domain=${window.domain}` : ''}`; window.location = '/'; }; }, localLogin : 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); 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 {global.account.username} brews logout ; } // Logged out // LOCAL ONLY if(global.config.local) { return login ; }; // Logged out // Production site return login ; } }); module.exports = Account;