added redirects
This commit is contained in:
@@ -11,10 +11,46 @@ import Login from './components/login/login';
|
||||
|
||||
import CreateAccount from './components/login/createAccount';
|
||||
import "./App.css";
|
||||
import { Redirect } from "react-router-dom";
|
||||
|
||||
class ProtectedRoute extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { auth: false };
|
||||
}
|
||||
render() {
|
||||
const url = "/simcompanies/API/testlogin"
|
||||
const checkLogin = async (url) => {
|
||||
const response = await fetch(url);
|
||||
|
||||
if (response.status == "200") {
|
||||
this.setState = {
|
||||
auth: true
|
||||
}
|
||||
} else {
|
||||
this.setState = {
|
||||
auth: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkLogin(url)
|
||||
const { component: Component, ...props } = this.props
|
||||
|
||||
return (
|
||||
<Route
|
||||
{...props}
|
||||
render={props => (
|
||||
this.state.authenticated ?
|
||||
<Component {...props} /> :
|
||||
<Redirect to='/login' />
|
||||
)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
class App extends Component {
|
||||
|
||||
render() {
|
||||
const theme = createMuiTheme({
|
||||
palette: {
|
||||
@@ -29,6 +65,7 @@ class App extends Component {
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
//change basename in browserrouter if uri changes
|
||||
return (
|
||||
<ThemeProvider theme={theme}>
|
||||
@@ -36,10 +73,8 @@ class App extends Component {
|
||||
<Switch>
|
||||
<Route exact path="/login" component={Login} />
|
||||
<Route exact path="/login/createuser" component={CreateAccount} />
|
||||
<Route path="/*" component={Navbar} />
|
||||
<ProtectedRoute path="/*" component={Navbar} />
|
||||
</Switch>
|
||||
|
||||
|
||||
</BrowserRouter>
|
||||
</ThemeProvider>
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import Button from '@material-ui/core/Button';
|
||||
import Logo from "../../img/logo.png";
|
||||
import TextField from '@material-ui/core/TextField';
|
||||
import Grid from '@material-ui/core/Grid';
|
||||
import { Redirect } from "react-router-dom";
|
||||
|
||||
const url = "/simcompanies/API/user/create"
|
||||
|
||||
@@ -70,6 +71,10 @@ const putCreateAccount = async (url, data) => {
|
||||
referrerPolicy: 'no-referrer',
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
|
||||
if (response.status == "200") {
|
||||
return <Redirect to={"/login"} />
|
||||
}
|
||||
}
|
||||
|
||||
export default function CreateAccount() {
|
||||
|
||||
@@ -10,6 +10,7 @@ import Logo from "../../img/logo.png";
|
||||
import TextField from '@material-ui/core/TextField';
|
||||
import Grid from '@material-ui/core/Grid';
|
||||
import Link1 from '@material-ui/core/Link';
|
||||
import { Redirect } from "react-router-dom";
|
||||
|
||||
const url = "/simcompanies/API/user/login"
|
||||
const useStyles = makeStyles(theme => ({
|
||||
@@ -70,18 +71,14 @@ const postLogin = async (url, data) => {
|
||||
referrerPolicy: 'no-referrer',
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
}
|
||||
|
||||
const checkLogin = async (url) => {
|
||||
const response = await fetch(url);
|
||||
|
||||
if (response.status == "200") {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
return <Redirect to={"/"} />
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
export default function Login() {
|
||||
const classes = useStyles();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user