Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
[Client] Update security area
Browse files Browse the repository at this point in the history
- Fix onEnter / onChange use
- Display Logout only if the manager is secured
- Closes #49
  • Loading branch information
DjLeChuck committed Nov 19, 2017
1 parent b6430ff commit 5ca8b4f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
16 changes: 11 additions & 5 deletions client/src/components/Layout.js
Expand Up @@ -12,6 +12,7 @@ import NavDropdown from 'react-bootstrap/lib/NavDropdown';
import NavItem from 'react-bootstrap/lib/NavItem';
import Row from 'react-bootstrap/lib/Row';
import { IndexLinkContainer, LinkContainer } from 'react-router-bootstrap';
import { readCookie } from '../utils';

const languages = {
ar: 'Argentina',
Expand Down Expand Up @@ -101,11 +102,16 @@ const Layout = ({ t, i18n, children }) => {
glyph: 'question-sign',
label: t('Dépannage'),
}];
const logOutEntry = renderMenuEntry({
link: '/logout',
glyph: 'log-out',
label: t('Déconnexion'),
}, 'logout');
let logOutEntry = '';

if (readCookie('recalbox-session')) {
logOutEntry = renderMenuEntry({
link: '/logout',
glyph: 'log-out',
label: t('Déconnexion'),
}, 'logout');
}

const securityEntry = renderMenuEntry({
link: '/security',
glyph: 'lock',
Expand Down
10 changes: 8 additions & 2 deletions client/src/routes.js
Expand Up @@ -20,7 +20,7 @@ import Security from './components/security/Container';
import NotFound from './components/NotFound';
import { get } from './api';

const Auth = async (nextState, replace, callback) => {
const auth = async (replace, callback) => {
const { needAuth } = await get('needAuth');

if (needAuth) {
Expand All @@ -41,7 +41,13 @@ const routes = (
<Route path="/login" component={AnonymousLayout}>
<IndexRoute component={Login} />
</Route>
<Route component={Layout} path="/" onEnter={Auth}>
<Route
component={Layout} path="/"
onEnter={(nextState, replace, callback) => auth(replace, callback)}
onChange={
(prevState, nextState, replace, callback) => auth(replace, callback)
}
>
<IndexRoute component={Home} />
<Route path="audio" component={Audio} />
<Route path="bios" component={Bios} />
Expand Down
18 changes: 18 additions & 0 deletions client/src/utils/index.js
Expand Up @@ -66,3 +66,21 @@ export function promisifyData(...calls) {
err => console.error(err)
);
}

let cookies;

export function readCookie(name) {
if (cookies) {
return cookies[name];
}

const c = document.cookie.split('; ');
cookies = {};

for (let i = c.length-1; i >= 0; i--) {
const C = c[i].split('=');
cookies[C[0]] = C[1];
}

return cookies[name];
}

0 comments on commit 5ca8b4f

Please sign in to comment.