Я создаю реагирующее приложение.Это веб-сайт для отслеживания некоторых данных хранения.в настоящее время я пытаюсь защитить маршруты, чтобы пользователи не могли просто получить к ним доступ, введя URL-адрес страницы и получить прямой доступ к нему.
Я видел какое-то руководство, в котором используются защищенные маршруты, но я не знаюкак это реализовать.
Мой логин.js
import React, { Component } from 'react'
import { login } from './UserFunctions'
class Login extends Component {
constructor() {
super()
this.state = {
email: '',
password: ''
}
this.onChange = this.onChange.bind(this)
this.onSubmit = this.onSubmit.bind(this)
}
onChange(e) {
this.setState({ [e.target.name]: e.target.value })
}
onSubmit(e) {
e.preventDefault()
const user = {
email: this.state.email,
password: this.state.password
}
login(user).then(res => {
if (res) {
this.props.history.push(`/profile`)
}
})
}
render(props) {
return (
<div className="container">
<div className="row">
<div className="col-md-6 mt-5 mx-auto">
<form noValidate onSubmit={this.onSubmit}>
<h1 className="h3 mb-3 font-weight-normal">Please sign in</h1>
<div className="form-group">
<label htmlFor="email">Email Address</label>
<input type="email"
className="form-control"
name="email"
placeholder="Enter Email"
value={this.state.email}
onChange={this.onChange}
/>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input type="password"
className="form-control"
name="password"
placeholder="Enter Password"
value={this.state.password}
onChange={this.onChange}
/>
</div>
<button type="submit"
className="btn btn-lg btn-primary btn-block">
Sign in
</button>
</form>
</div>
</div>
</div>
)
}
}
export default Login
Мой app.js
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Link, Redirect, withRouter } from 'react-router-dom'
import { Home } from './appcomponents/Home';
import { Stockin } from './appcomponents/Stockin';
import { Stockout } from './appcomponents/Stockout';
import { Asrsstock } from './appcomponents/ASRSstock';
import { Productinfo } from './appcomponents/Productinfo';
import { Productin } from './appcomponents/Productin';
import { Productout } from './appcomponents/Productout';
import { Stockreport } from './appcomponents/Stockreport';
import { Enrolling } from './appcomponents/Enrolling';
import { NoMatch } from './appcomponents/NoMatch';
//import { NavigationBar } from './components/NavigationBar';
import { Deleteuser } from './appcomponents/Deleteuser';
import decode from 'jwt-decode';
import Navigationbar from './components/Navbar'
import Landing from './components/Landing'
import Login from './components/Login'
import Register from './components/Register'
import Profile from './components/Profile'
function requireAuth(nextState, replace) {
if (!loggedIn()) {
replace({
pathname: '/login'
})
}
}
class App extends Component {
render() {
return (
<Router>
<div className="App">
<Navigationbar />
<Route exact path="/" component={Landing} />
<div className="container">
<Route exact path="/register" component={Register} />
<Route exact path="/login" component={Login} />
<Route exact path="/profile" component={Profile} />
<Route exact path="/Home" component={Home} />
<Route path="/Stockin" component={Stockin} />
<Route path="/Stockout" component={Stockout} />
<Route path="/Asrsstock" component={Asrsstock} />
<Route path="/Productinfo" component={Productinfo} />
<Route path="/Productin" component={Productin} />
<Route path="/Productout" component={Productout} />
<Route exact path="/Stockreport" component={Stockreport} onEnter={requireAuth} />
<Route path="/Enrolling" component={Enrolling} />
<Route path="/Deleteuser" component={Deleteuser} />
<Route exact path="/Register" component={Register} />
<Route component={NoMatch} />
</div>
</div>
</Router>
);
}
}
export default App;
Я хочу иметь возможность перенаправить неавторизованного пользователя на страницу входа.Так что единственный способ для них получить доступ к защищенным страницам - войти в систему