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