Skip to content

Instantly share code, notes, and snippets.

@srph
Created February 14, 2017 19:54
Show Gist options
  • Select an option

  • Save srph/32dcc558bd82a0dbdd8bd42511d07d6c to your computer and use it in GitHub Desktop.

Select an option

Save srph/32dcc558bd82a0dbdd8bd42511d07d6c to your computer and use it in GitHub Desktop.
react: permissions

what

snippets of the permission decorators (aka high-order components) for our trello-like app at @TARAAI.

usage

import React from 'react';
import * as Permission from '?';

class DashboardHomeView extends React.Component {
 // ...
}

export default Permission.auth(DashboardHomeView);
import React from 'react';
import store from 'app/store';
import history from 'app/history';
/**
* Make page accessibly only by logged in users
* @TODO: Redirect to page originally being visited by user.
*/
export default function auth(Component) {
return class AuthPermissionComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
// So view doesn't flickr if we finally confirm
// that user isn't a guest.
resolved: false
};
}
componentDidMount() {
if (store.getState().auth.token == null) {
history.push('/login');
} else {
this.setState({ resolved: true });
}
}
render() {
return this.state.resolved && <Component {...this.props} />;
}
}
}
import React from 'react';
import store from 'app/store';
import history from 'app/history';
/**
* Make page accessibly only by guest users
*/
export default function guest(Component) {
return class GuestPermissionComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
// So view doesn't flickr if we finally confirm
// that user isn't a guest.
resolved: false
};
}
componentWillMount() {
if (store.getState().auth.token != null) {
history.push('/');
} else {
this.setState({ resolved: true });
}
}
render() {
return this.state.resolved && <Component {...this.props} />;
}
}
}
import auth from './auth';
import guest from './guest';
// @NOTE: For some reason, Buble won't compile if
// written as: `export auth;`
exports.auth = auth;
exports.guest = guest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment