I have a login form on my site and it is set up to show every time user wants to open any resource on my webpage.
When user goes to e.g. myweb.page/some/resource it shows him my login form and then - after providing credentials - it should redirect him to the requested url with additional j_security_check part of the url. This is my code so far:
import React from "react";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({[event.target.name]: event.target.value});
}
handleSubmit(event) {
let target = "";
let targetResource = getQueryVariable('resource');
if (targetResource) {
if (targetResource.substr(-1) === '/') {
target = targetResource.substr(0, -1);
} else {
target = targetResource;
}
}
alert(targetResource);
this.state.target = targetResource
}
render() {
return (
<form onSubmit={ this.handleSubmit } action={this.state.target + "/j_security_check"} method="POST">
<label>
email:
<input type="text" name="email" value={this.state.email} onChange={e=>this.handleChange(e)} />
</label>
<input type="hidden" name="resource" value={this.state.target + "/j_security_check"} />
<label>
passwords:
<input type="password" name="password" value={this.state.password} onChange={e=>this.handleChange(e)} />
</label>
<input type="submit" value="Login" />
</form>
);
}
}
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
console.log(query)
var vars = query.split("&");
console.log(vars)
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
console.log(pair);
if(pair[0] == variable){
return pair[1];
}
}
return(false);
}
export default App;
In handleSubmit method there's an alert that prints the correct redirection path, e.g.:
%2Fcorrect%2Furl%2Faddress
but then the webpage reloads and it redirects to:
http://localhost:8080/apps/myLoginForm/undefined/j_security_check
instead of
http://localhost:8080/apps/myLoginForm/correct/url/address/j_security_check
Why is the correct redirection target not passed to the url in the form? Thanks!