Im building a small application where i have used ReactJs for the front end and node express with jwt and passport in back end. Im using react router v4 and i have configured the applications routes like this.
import React,{Component} from 'react';
import Login from './login/login';
import Signup from './signup/signup';
import Account from './account/account';
import Navigation from './navigation/navigation';
import Newsfeeds from './newsfeed/feedcontainer';
import Test from './test';
import NotMatch from './nomatch/Notmatch';
import injectTapEventPlugin from 'react-tap-event-plugin';
import createBrowserHistory from 'history/createBrowserHistory';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
const history = createBrowserHistory({});
injectTapEventPlugin();
let hasToken = () => {
const token = localStorage.getItem('jwtToken');
return (token == undefined);
}
class App extends Component{
render(){
return(
<MuiThemeProvider>
<Router history={history}>
<Switch>
<Route path="/login" exact component={Login}/>
<Route path="/signup" exact component={Signup}/>
<Route path="/test" exact component={Test}/>
<Navigation>
<Route path="/account" exact render={() => (hasToken() ? (<Account />) : (<Redirect to="/login"/>))}/>
<Route path="/home" exact render={() => (hasToken() ? (<Newsfeeds />) : (<Redirect to="/login"/>))}/>
</Navigation>
<Route component={NotMatch}/>
</Switch>
</Router>
</MuiThemeProvider>
);
}
}
export default App;
The code for logout
<Link to="/login"><MenuItem primaryText="Logout" onTouchTap={e=>{localStorage.clear();}}></MenuItem></Link>
When ever i try to logout(clear my localstorage and redirect to login route) it gets stuck in loop and the app breaks. How to avoid this?
How to restrict/authenticate routes. When already logged switch to home page when user tries to move to /login or /signup routes.
Related
i build my website and creat all component like homepage, dashboard, login, etc. and when i login to dashboard and create other menu after i save, the page back to login screen or homepage again, after i create a newcode it always back to homepage or login screen again, how to avoid?
parent class include reducer
import React from 'react'
import { render } from 'react-dom'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import { Route} from 'react-router-dom';
import {Router, Switch} from 'react-router';
import { createHashHistory } from 'history';
import LandingLayout from './layouts/LandingLayout.js';
import App from './containers/App'
import reducer from './reducers'
import Login from './containers/Login'
import Browse from './containers/Browse'
import Home from './containers/Home'
import About from './containers/About'
import Contact from './containers/Contact'
import Input from './containers/Input'
import Detail from './containers/Detail'
const store = createStore(reducer);
const customHistory = createHashHistory();
render(
<Provider store = {store}>
<Router history={customHistory}>
<div>
<Switch>
<Route exact path='/' render={() => <LandingLayout><App /></LandingLayout>}/>
<Route exact path='/Home' render={() => <LandingLayout><Home /></LandingLayout>}/>
<Route exact path='/Input' render={() => <LandingLayout><Input /></LandingLayout>}/>
<Route exact path='/Detail' render={() => <LandingLayout><Detail /></LandingLayout>}/>
<Route exact path='/About' render={() => <LandingLayout><About /></LandingLayout>}/>
<Route exact path='/Contact' render={() => <LandingLayout><Contact /></LandingLayout>}/>
<Route exact path='/login' render={() => <LandingLayout><Login /></LandingLayout>}/>
<Route exact path='/browse' render={() => <LandingLayout><Browse /></LandingLayout>} />
</Switch>
</div>
</Router>
</Provider>,
document.getElementById('root')
)
There are two solutions for this case:
1) After login use target="_blank" to open a new tab that cannot back to prev page :)
2) Use history package: history.replace('/dashboard')
React Router is changing the url but is not loading the component.
And once the url is changed and if the page gets refreshed, the correct component corresponding to the url is shown.
This is the code :
import React from "react";
import { Route, Switch } from "react-router-dom"
import { Link, BrowserRouter as Router } from 'react-router-dom'
import {Home} from '../src/Views/Home';
import {Test} from './Views/Home';
import App from '../../../src/App'
const Root = () => (
<Router>
<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/test" component={Test} />
</Switch>
</Router>
);
export default Root;
There are no console errors.
I have my application that works with a hash router with routes like "login" "register" "dashboard" and I would like to integrate react-admin. I would like to use react admin on my /admin route.
What I do:
App.js
import React, {Component} from 'react';
import {HashRouter as Router, Route} from 'react-router-dom';
import {Provider} from 'react-redux'
import Store from './store/configureStore'
import RegisterView from "./view/RegisterView"
import LoginView from "./view/LoginView"
import DashboardView from "./view/DashboardView"
import AdminView from "./view/AdminView"
import jsonServerProvider from "ra-data-json-server";
import { Admin, Resource, BooleanField } from "react-admin";
import { UserList, UserEdit, UserCreate } from './users';
const dataProvider =
jsonServerProvider("https://jsonplaceholder.typicode.com");
class App extends Component {
// Provider store is used to use the store of the redux
// Router is used to define the route and the component to display
render() {
return (
<div>
<Provider store={Store}>
<Router basename="/">
<div>
<Route exact path="/" component={LoginView}>
</Route>
<Route exact path="/register" component={RegisterView}>
</Route>
<Route exact path="/login" component={LoginView}>
</Route>
<Route exact path="/dashboard" component={DashboardView}>
<Route exact path="/admin" component={AdminView}>
<Admin dataProvider={dataProvider}>
<Resource
name="users"
list={UserList}
edit={UserEdit}
create={UserCreate}
/>
</Admin>
</Route>
</div>
</Router>
</Provider>
</div>
);
}
}
export default App;
it displays my react-admin but with my other components (display bug). I would like the react-admin part only in my /admin routes.
For me, the section should only be working in the /admin because i gave this path to my route.
What don't I understand?
import React, {Component} from 'react';
import {HashRouter as Router, Route} from 'react-router-dom';
import {Provider} from 'react-redux'
import Store from './store/configureStore'
import Register from "./view/RegisterView"
import Login from "./view/LoginView"
import Dashboard from "./view/DashboardView"
class App extends Component {
constructor() {
super();
}
render() {
return (
<div>
<Provider store={Store}>
<Router basename="/">
<div>
<Route exact path="/register" component={Register}>
</Route>
<Route exact path="/login" component={Login}>
</Route>
<Route exact path="/dashboard" component={Dashboard}>
</Route>
</div>
</Router>
</Provider>
</div>
);
}
}
export default App;
I have my App.js where i give my different route. When the Login is successful i want to go to my /dashboard route. (but we can access it only when we are logged in).
In my Login component i would like to change the route.
I try it to change it like that:
this.props.history.push("/dashboard");
but history is undefined.
I read some tutorial on google and they tell me that i can use:
onEnter={requireAuth}
in my /dashboard route
I don't know how i can change my route in my login component
I expect to login and to come in my /dashboard route (but only if i'm connected).
I am trying to make my app redirect the users to a login page if it does not detect a Meteor.userId(), and to the home page if they do have one. The problem I am struggling with right now is getting a simple console.log function to run when a user visits a route (regardless of the path). How would I do this from a file the manages all the routes and components without having to insert a boilerplate code in each component.
import { Meteor } from "meteor/meteor"
import React from "react";
import { withRouter, Switch, BrowserRouter, Route, Redirect, Link } from "react-router-dom";
import { Tracker } from "meteor/tracker";
import Login from "../ui/authentication/Login";
import Signup from "../ui/authentication/Signup";
import Home from "../ui/Home";
import searchNotes from "../ui/searchNotes"
import Note from "../ui/Note";
import fullSize from "../ui/fullSize"
import userProfile from "../ui/userProfile";
import AddNote from "../ui/AddNote";
import questions from "../ui/questions"
import NotFound from "../ui/NotFound";
export default class Routes extends React.Component{
componentDidMount() {
console.log("rendering page")
}
render(){
return (
<div>
<BrowserRouter>
<Switch>
<Login path="/login" />
<Signup path="/signup" />
<Route path="/" component={Home} exact/>
<Route path={`/searchNotes/:subject`} component={Note} />
<Route path={`/searchNotes`} component={searchNotes} />
<Route path={`/fullSize/:noteId`} component={fullSize}/>
<AddNote path="/addNote"/>
<Route path="/questions" component={questions} />
<Route component={userProfile} path={`/users/:userId`} />
<NotFound />
</Switch>
</BrowserRouter>
</div>
)
}
}
Also, I tried doing export default withRouter(Routes) so I can change the redirect people to new pages, but when I do this it returns an error in the browser saying, "Uncaught TypeError: Cannot read property 'route' of undefined" If you know how to fix that, it would be much appreciated. Thanks!