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

Commit

Permalink
fix anonymous and authenticated #18
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jun 4, 2015
1 parent 424357d commit 1f1d740
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 79 deletions.
53 changes: 15 additions & 38 deletions src/util/anonymous.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,26 @@
import React from 'react';
import debug from 'debug';
import { listener } from 'web-framework';
import { listener } from '../decorator/listener';

const log = debug('util:@anonymous');

export default function anonymous(homeState?: string) {
export default function anonymous(Home = 'Home') {
return function (Component) {

const home = homeState || 'Home';

return function(Component) {

@listener
class AnonymousWrapper extends React.Component {

static willTransitionTo(transition) {
const app = transition.context;
if (app.isAuthenticated()) {
log(`user is authenticated, redirecting to ${home}...`);
transition.redirect(home);
}
};

checkAuthentication() {
if (this.props.app.isAuthenticated()) {
const { nextPath = home } = this.props.router.getCurrentQuery();
log('user is authenticated, redirecting to %s...', nextPath);
this.props.router.transitionTo(nextPath);
}
}

componentWillReceiveProps() {
this.checkAuthentication();
}

componentWillMount() {
this.checkAuthentication();
listener(function () {
if (this.props.app.isAuthenticated()) {
const { nextPath = Home } = this.props.router.getCurrentQuery();
log('listening... user is authenticated, redirecting to %s...', nextPath);
this.props.router.transitionTo(nextPath);
}
})(Component);

render() {
return <Component {...this.props} />;
Component.willTransitionTo = function (transition) {
const app = transition.context;
if (app.isAuthenticated()) {
log('transition to `%s` aborted (user is authenticated), redirecting to %s...', transition.path, Home);
transition.redirect(Home);
}
};

}

return AnonymousWrapper;
};

}
59 changes: 18 additions & 41 deletions src/util/authenticated.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,28 @@
import React from 'react';
import debug from 'debug';
import { listener } from 'web-framework';
import { listener } from '../decorator/listener';

const log = debug('util:@authenticated');

export default function authenticated(loginState?: string) {
export default function authenticated(Login = 'Login') {
return function (Component) {

const login = loginState || 'Login';

return function(Component) {

@listener
class AuthenticatedWrapper extends React.Component {

static willTransitionTo(transition) {
const app = transition.context;
if (!app.isAuthenticated()) {
log(`transition to \`%s\` aborted (user is not authenticated), redirecting to ${login}...`, transition.path);
transition.redirect(login, {}, {
nextPath: transition.path
});
}
};

checkAuthentication() {
const app = this.props.app;
if (!app.isAuthenticated()) {
log(`user is not authenticated, redirecting to ${login}...`);
this.props.router.transitionTo(login);
}
listener(function () {
const app = this.props.app;
if (!app.isAuthenticated()) {
log('listening... user is not authenticated, redirecting to %s...', Login);
this.props.router.transitionTo(Login);
}

componentWillReceiveProps() {
this.checkAuthentication();
})(Component);

Component.willTransitionTo = function (transition) {
const app = transition.context;
if (!app.isAuthenticated()) {
log('transition to `%s` aborted (user is not authenticated), redirecting to %s...', transition.path, Login);
transition.redirect(Login, {}, {
nextPath: transition.path
});
}
};

componentWillMount() {
this.checkAuthentication();
}

render() {
return <Component {...this.props} />;
}

}

return AuthenticatedWrapper;
};

}

0 comments on commit 1f1d740

Please sign in to comment.