'Bad request 400 react + django
I am receiving error 400 bad request while trying to authenticate react with django rest framework
Here is Signup.js
import React from "react";
import {
Button,
Form,
Grid,
Header,
Message,
Segment,
} from "semantic-ui-react";
import { connect } from "react-redux";
import { NavLink, Navigate } from "react-router-dom";
import { authSignup } from "../store/actions/auth";
class RegistrationForm extends React.Component {
constructor (props) {
super(props);
this.state = {
username: "",
email: "",
password1: "",
password2: "",
userType: "",
is_student : true
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
};
handleSubmit = e => {
e.preventDefault();
const { username, email, password1, password2, userType, is_student } = this.state;
this.props.signup(username, email, password1, password2, userType, is_student);
const signupFormValid =
!username?.length ||
!email?.length ||
!password1?.length ||
!password2?.length ||
!userType?.length ;
};
handleChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
render() {
const { username, email, password1, password2, userType } = this.state;
const { error, loading, token } = this.props;
if (token) {
return <Navigate to="/" />;
}
return (
<Grid
textAlign="center"
style={{ height: "100vh" }}
verticalAlign="middle"
>
<Grid.Column style={{ maxWidth: 450 }}>
<Header as="h2" color="teal" textAlign="center">
Signup to your account
</Header>
{error && <p>{this.props.error.message}</p>}
<React.Fragment>
<Form size="large" method='post' onSubmit={this.handleSubmit}>
<Segment stacked>
<Form.Input
onChange={this.handleChange}
value={username}
name="username"
fluid
icon="user"
iconPosition="left"
placeholder="Username"
/>
<Form.Input
onChange={this.handleChange}
value={email}
name="email"
fluid
icon="mail"
iconPosition="left"
placeholder="E-mail address"
/>
<Form.Input
onChange={this.handleChange}
fluid
value={password1}
name="password1"
icon="lock"
iconPosition="left"
placeholder="Password"
type="password"
/>
<Form.Input
onChange={this.handleChange}
fluid
value={password2}
name="password2"
icon="lock"
iconPosition="left"
placeholder="Confirm password"
type="password"
/>
<Form.Input name="userType" value={userType} required={true} onChange={this.handleChange} >
<select>
<option value="">Please select a user type !</option>
<option value="student">Student</option>
<option value="teacher">Teacher</option>
</select>
</Form.Input>
<Form.Field>
<Button
color="teal"
fluid
size="large"
loading={loading}
disabled={loading}
>
Signup
</Button>
</Form.Field>
</Segment>
</Form>
<Message>
Already have an account? <NavLink to="/login">Login</NavLink>
</Message>
</React.Fragment>
</Grid.Column>
</Grid>
);
}
}
const mapStateToProps = state => {
return {
loading: state.auth.loading,
error: state.auth.error,
token: state.auth.token
};
};
const mapDispatchToProps = dispatch => {
return {
signup: (username, email, password1, password2, is_student) =>
dispatch(authSignup(username, email, password1, password2, is_student))
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(RegistrationForm);
Here is actions/auth.js
import axios from "axios";
import { env } from "process";
import * as actionTypes from "./actionTypes";
export const authStart = () => {
return {
type: actionTypes.AUTH_START
};
};
export const authSuccess = user => {
return {
type: actionTypes.AUTH_SUCCESS,
user
};
};
export const authFail = error => {
return {
type: actionTypes.AUTH_FAIL,
error: error
};
};
export const logout = () => {
localStorage.removeItem("user");
return {
type: actionTypes.AUTH_LOGOUT
};
};
export const checkAuthTimeout = expirationTime => {
return dispatch => {
setTimeout(() => {
dispatch(logout());
}, expirationTime * 1000);
};
};
export const authLogin = (username, password) => {
return dispatch => {
dispatch(authStart());
axios
.post(`${process.env.REACT_APP_API}/auth/jwt/create/`, {
username: username,
password: password
})
.then(res => {
const token = res.data.key;
const expirationDate = new Date(new Date().getTime() + 3600 * 1000);
localStorage.setItem("token", token);
localStorage.setItem("expirationDate", expirationDate);
dispatch(authSuccess(token));
dispatch(checkAuthTimeout(3600));
})
.catch(err => {
dispatch(authFail(err));
});
};
};
export const authSignup = (username, email, password1, password2, is_student) => {
return dispatch => {
dispatch(authStart());
const user = {
username,
email,
password1,
password2,
is_student,
is_teacher: !is_student
}
axios
.post(`${process.env.REACT_APP_API}/auth/user/`, user)
.then(res => {
const user = {
token : res.data.key,
username,
is_student,
is_teacher: !is_student,
expirationDate : new Date(new Date().getTime() + 3600 * 1000)
}
localStorage.setItem("user", JSON.stringify(user));
dispatch(authSuccess(user));
dispatch(checkAuthTimeout(3600));
})
.catch(err => {
dispatch(authFail(err));
});
};
};
export const authCheckState = () => {
return dispatch => {
const user = JSON.parse(localStorage.getItem("user"));
if (user === undefined || user === null) {
dispatch(logout());
} else {
const expirationDate = new Date(user.getItem("expirationDate"));
if (expirationDate <= new Date()) {
dispatch(logout());
} else {
dispatch(authSuccess(user));
dispatch(
checkAuthTimeout(
(expirationDate.getTime() - new Date().getTime()) / 1000
)
);
}
}
};
};
When I click on signup I get this: POST http://localhost:8000/api/auth/user/ 400 (Bad Request)
Do you guys have any idea on whats the problem?
Thanks a lot in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
