React 16x - قسمت 29 - احراز هویت و اعتبارسنجی کاربران - بخش 4 - محافظت از مسیرها
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۰۹/۲۸ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<Route
path="/movies"
render={props => <Movies {...props} user={this.state.currentUser} />}
/> <Route path="/movies/:id" component={MovieForm} /> <Route
path="/movies/:id"
render={props => {
if (!this.state.currentUser) {
return <Redirect to="/login" />;
}
return <MovieForm {...props} />;
}}
/> import React from "react";
import { Route, Redirect } from "react-router-dom";
import * as auth from "../../services/authService";
const ProtectedRoute = ({ path, component: Component, render, ...rest }) => {
return (
<Route
{...rest}
render={props => {
if (!auth.getCurrentUser())
return (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
);
return Component ? <Component {...props} /> : render(props);
}}
/>
);
};
export default ProtectedRoute; import ProtectedRoute from "./components/common/protectedRoute";
// ...
<ProtectedRoute path="/movies/:id" component={MovieForm} /> return (
<Route
{...rest}
render={props => {
console.log(props);
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/> doSubmit = async () => {
try {
const { data } = this.state;
await auth.login(data.username, data.password);
const { state } = this.props.location;
window.location = state ? state.from.pathname : "/";
} catch (ex) {
//... import { Redirect } from "react-router-dom";
//...
render() {
if (auth.getCurrentUser()) return <Redirect to="/" />; class App extends Component {
state = {};
componentDidMount() {
const currentUser = userService.getCurrentUser();
this.setState({ currentUser });
console.log("CUUSer", currentUser);
}
render() {
const { currentUser } = this.state;
return (
<React.Fragment>
<ToastContainer />
<Switch>
<Route
path="/dashboard"
render={props => {
console.log("CCCCCCCCC", currentUser);
if (!currentUser) {
return <Redirect to="/account/login" />;
}
return <DashboardPage {...props} />;
}}
/>
<Route path="/account/registercode" component={RegisterCode} />
<Route path="/account/login" component={LoginForm} />
<Route path="/notfound" component={NotFound} />
<Route path="/logout" component={Logout} />
<Route path="/" exact component={HomePage} />
<Redirect to="/notfound" />
</Switch>
</React.Fragment>
);
}
}
export default App; const ProtectedRoute = ({ path, component: Component, render, ...rest }) => {
const history = useHistory();
const user = auth.getCurrentUser();
useEffect(() => {
const beforePath = history.location.pathname;
if (user && beforePath.toLowerCase() !== '/activation') {
if (user.status <= 0) {
history.push({
pathname: "/Activation"
});
}
}
// componentWillUnmount
return () => {
};
}); const ProtectedRoute = ( {children,roles }) => {
const isLoggedIn=authService.isLoggedIn();
if (!isLoggedIn) {
return <Navigate to="/login" replace />;
}
if(roles)
{
//checkRoles
if(result_roles===false)
return <Navigate to="/login" replace />;
}
return children;
};
export default ProtectedRoute <Routes>
<Route path="/product/new" element={
<ProtectedRoute roles={["hesabdar", "anbardar"]}>
<AdminTemplate>
<NewProduct/>
</AdminTemplate>
</ProtectedRoute>
}/>
</Routes>