This commit is contained in:
Jake McDermott
2018-09-26 20:56:58 -04:00
parent 0986ebef33
commit f8a4b01da5
6 changed files with 1429 additions and 71 deletions

View File

@@ -10,18 +10,20 @@ const API_ORGANIZATIONS = `${API_V2}organizations/`;
const CSRF_COOKIE_NAME = 'csrftoken';
const CSRF_HEADER_NAME = 'X-CSRFToken';
const http = axios.create({
xsrfCookieName: CSRF_COOKIE_NAME,
xsrfHeaderName: CSRF_HEADER_NAME,
});
let authenticated = false; // temporary
class APIClient {
isAuthenticated() {
return authenticated;
constructor () {
this.authenticated = false; // temporary
this.http = axios.create({
xsrfCookieName: CSRF_COOKIE_NAME,
xsrfHeaderName: CSRF_HEADER_NAME,
});
}
login(username, password, redirect = API_CONFIG) {
isAuthenticated () {
return this.authenticated;
}
login (username, password, redirect = API_CONFIG) {
const un = encodeURIComponent(username);
const pw = encodeURIComponent(password);
const next = encodeURIComponent(redirect);
@@ -29,26 +31,26 @@ class APIClient {
const data = `username=${un}&password=${pw}&next=${next}`;
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
return http.get(API_LOGIN, { headers })
.then(() => http.post(API_LOGIN, data, { headers }))
return this.http.get(API_LOGIN, { headers })
.then(() => this.http.post(API_LOGIN, data, { headers }))
.then(res => {
authenticated = true; // temporary
this.authenticated = true; // temporary
return res;
})
});
}
logout() {
return http.delete(API_LOGIN);
logout () {
return this.http.delete(API_LOGIN);
}
getProjects() {
return http.get(API_PROJECTS);
getProjects () {
return this.http.get(API_PROJECTS);
}
getOrganizations() {
return http.get(API_ORGANIZATIONS);
getOrganizations () {
return this.http.get(API_ORGANIZATIONS);
}
};
}
export default new APIClient();

View File

@@ -7,67 +7,59 @@ import api from '../api';
class Login extends Component {
state = {
username: '',
password: '',
redirect: false,
username: '',
password: '',
redirect: false,
};
handleChange = event => this.setState({ [event.target.name]: event.target.value });
handleSubmit = event => {
const { username, password } = this.state;
const { username, password } = this.state;
event.preventDefault();
event.preventDefault();
api.login(username, password)
.then(() => this.setState({ redirect: true }))
.then(() => api.getProjects())
.then(res => console.log(res));
};
.then(() => this.setState({ redirect: true }));
}
render() {
render () {
const { username, password, redirect } = this.state;
if (redirect) {
return (<Redirect to={'/'} />);
}
if (redirect) {
return (<Redirect to="/" />);
}
return (
<div className='column'>
<form onSubmit={this.handleSubmit}>
<div className='field'>
<label className='label'>Username</label>
<div className='control'>
<input
className='input'
type='text'
name='username'
onChange={this.handleChange}
value={username}
required
/>
</div>
</div>
<div className='field'>
<label className='label'>Password</label>
<div className='control'>
<input
className='input'
type='password'
name='password'
onChange={this.handleChange}
value={password}
required
/>
</div>
</div>
<div className='control'>
<Button type='submit'>
Login
</Button>
</div>
</form>
</div>
<form onSubmit={this.handleSubmit}>
<div className="field">
<label htmlFor="username">
Username
<input
id="username"
name="username"
type="text"
onChange={this.handleChange}
value={username}
/>
</label>
</div>
<div>
<label htmlFor="password">
Password
<input
id="password"
name="password"
type="password"
onChange={this.handleChange}
value={password}
/>
</label>
</div>
<Button type="submit">
Login
</Button>
</form>
);
}
}

View File

@@ -1 +1,3 @@
import App from './App';
export default App;