Skip to content

Instantly share code, notes, and snippets.

View _checkAndRedirect.js
_checkAndRedirect() {
const { dispatch, user } = this.props;
const { role, redirectTo } = options;
if (!user || !user.role === role) {
dispatch(push(redirectTo || '/signin'));
}
}
View routes_3.js
<Route path="/app" component={requiresAuth(App, { role: 'user', redirectTo: '/signin' })}>
<Route name="dashboard" path="dashboard" component={Dashboard} />
<Route name="invoices" path="user-profile" component={UserProfile} />
</Route>
<Route path="/config" component={requiresAuth(Config, { role: 'admin', redirectTo: '/app' })}>
<Route name="page" path="page" component={PageConfig} />
</Route>
View routes_2.js
const routes = (
<Route path="">
<Route path="/signup" component={SignUp} />
<Route path="/signin" component={SignIn} />
<Route path="/welcome-on-board" component={Welcome} />
<Route path="/forgot-password" component={ForgotPassword} />
<Route path="/signout" component={SignOut} /> /* App component is wrapped
with requiresAuth() */
<Route path="/app" component={requiresAuth(App)}>
View auth_comp.js
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { push } from 'react-router-redux';
export default function requiresAuth(Component) {
class AuthenticatedComponent extends React.Component {
static propTypes = {
user: PropTypes.object,
dispatch: PropTypes.func.isRequired,
};
componentDidMount() {
View routes.js
const routes = (
<Route path="">
{' '}
<Route path="/signup" component={SignUp} />{' '}
<Route path="/signin" component={SignIn} />{' '}
<Route path="/welcome-on-board" component={Welcome} />{' '}
<Route path="/forgot-password" component={ForgotPassword} />{' '}
<Route path="/signout" component={SignOut} />{' '}
<Route path="/" component={App}>
{' '}
@alexbeletsky
alexbeletsky / example.js
Created Mar 8, 2017
API endpoint as promise chain
View example.js
import domain from '../../domain';
const Handler = {
getGroups: (request, reply) => {
const groups = domain.groups(request, reply);
return Promise.resolve()
.then(groups.getGroups)
.then(groups.transformMany)
.then(groups.reply);
@alexbeletsky
alexbeletsky / app.js
Last active Feb 28, 2017
Express.js 4.0 REST API Auth Example
View app.js
const express = require('express');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const morgan = require('morgan');
const cors = require('cors');
const health = require('express-ping');
const config = require('./config');
const middleware = require('./source/middleware');
const logger = require('./source/utils/logger');
View 1_outside.js
import React, { Component, PropTypes } from 'react';
class OutsideClick extends Component {
static propTypes = {
children: PropTypes.any,
onClick: PropTypes.func.isRequired,
id: PropTypes.string,
tag: PropTypes.string,
className: PropTypes.string
}
@alexbeletsky
alexbeletsky / auth.js
Last active Jun 7, 2016
Redux Thunk Async Action
View auth.js
import { actionRequest, requestFlow } from '../utils/axios';
import { push } from 'react-router-redux';
import constants from '../constants';
export function requestAuthUrl() {
return (dispatch) => {
const onBefore = () => {
dispatch({ type: constants.ACCOUNT_REQUESTING_AUTH_URL });
};
View gist:e932a07d3f554447a950
ERROR in ./source/components/SignUpForm/index.js
Module build failed: SyntaxError: /Users/alexanderbeletsky/Development/Projects/blogfoster/app/source/components/SignUpForm/index.js: A semicolon is required after a class property (79:3)
77 | onSignUp: PropTypes.func,
78 | onCleanupErrors: PropTypes.func
> 79 | }
| ^
80 |
81 | componentWillMount() {