Dzen00
Dzen00

Reputation: 1

Creating admin user in MERN

I want to create an admin user in my MERN application, I created a user model on back-end and put isAdmin there. Now I want to protect some routes on back-end. On front-end I need to protect normal user to access admin panel, which is accessible when click on NavLink.

User schema:
User schema

Signup controller:
Signup controller

Front end routes:
Front end routes

Upvotes: 0

Views: 1331

Answers (1)

Moman Raza
Moman Raza

Reputation: 168

you can use switch route of react routes like that.

import React from 'react';
import ReactDOM from 'react-dom';
import {
  BrowserRouter as Router,
  Route,
  browserHistory,
  Switch
} from 'react-router-dom';

import App from './app/App';
import Welcome from './app/Welcome';
import NotFound from './app/NotFound';

const isAdmin = false; // flag of current user login 
const Root = () => (
  <Router history={browserHistory}>
    <Switch>
      {{isAdmin ? <Route exact path="/admin" component={Admin}/> : null }}
       {{isAdmin ? <Route exact path="/admin/new" component={AdminNew}/> : null }}
      <Route component={NotFound}/>
    </Switch>
  </Router>
);

ReactDOM.render(
  <Root/>,
  document.getElementById('root')
);

Upvotes: 1

Related Questions