Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react router Link component breaks authorization #2

Open
prakharsingh opened this issue Sep 6, 2016 · 5 comments
Open

react router Link component breaks authorization #2

prakharsingh opened this issue Sep 6, 2016 · 5 comments
Assignees
Labels

Comments

@prakharsingh
Copy link

I am facing a problem with the Link component of React router. If I try to visit a restricted route through Link like as: Profile the AuthorizedComponent fails to check if the route is permitted to the user or not. Any help would be appreciated.
Thanks

@burczu
Copy link
Owner

burczu commented Sep 7, 2016

could you provide more info about your configuration of the routing and the constructor content of the class which extends the AuthorizedComponent? This would be very helpful - thanks.

@prakharsingh
Copy link
Author

prakharsingh commented Sep 7, 2016

Route configuration:

<Router history={browserHistory}>
    <Route path="/" component={ AppContainer }>
      <IndexRoute component={ Home }/>
      <Route component={ Videos } path="/videos" />
      <Route authorize={['public']} component={ RestrictedContainer }>
        <Route component={ ResetPassword } path="/reset-password/:email/:token" />
      </Route>
      <Route authorize={[ 'user', 'super-user' ]} component={ RestrictedContainer }>
        <Route component={ UserDashboard } path="/user/dashboard" />
        <Route component={ UserProfile } path="/user/profile" />
      </Route>
      <Route authorize={[ 'super-user', 'admin' ]} component={ RestrictedContainer }>
        <Route component={ ConferenceDashboard } path="/user/conferences" />
        <Route component={ LiveConference } path="/user/conferences/:id" />
      </Route>
    </Route>
    <Route component={ NoRoute } path="*" />
  </Router>
```, document.getElementById('app'))

-----------------------------------------------------------------------------------------------------------------------------------------
Restricted Container

import React from 'react';
import { AuthorizedComponent } from 'react-router-role-authorization';
import _ from 'lodash';

import SessionStore from '../../stores/SessionStore';
import { Roles } from '../../constants'

class RestrictedContainer extends AuthorizedComponent {
  constructor(props) {
    super(props);

    const sessionUser = SessionStore.getSessionUser();
    const userRole = _.find(Roles, { role: sessionUser ? sessionUser.role : 'public' });

    this.userRoles = [ sessionUser && sessionUser.role ];
    this.notAuthorizedPath = userRole.homePage;
  };

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

export default RestrictedContainer;

-----------------------------------------------------------------------------------------------------------------------------------------
In my navbar I am using React Router's Link component 
<Link to="/user/conferences">Conferences</Link>

-----------------------------------------------------------------------------------------------------------------------------------------

The problem here is that the user can access the route "/user/conferences" through link bypassing Resticted Container.

@burczu
Copy link
Owner

burczu commented Sep 19, 2016

Sorry for the late answer... one more question - what role are in sessionUser and sessionUser.role objects which are assigned to this.userRoles?

@prakharsingh
Copy link
Author

@burczu The session user has one role like: { "role": "user" }. As this.userRoles takes an array asargument threfore I have passed it into array.

@Extra-lightwill
Copy link

@burczu @prakharsingh did you manage to resolve this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants