mirror of
https://github.com/ZwareBear/awx.git
synced 2026-03-21 16:23:36 -05:00
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
import axios from 'axios';
|
|
|
|
import * as constant from './endpoints';
|
|
|
|
const CSRF_COOKIE_NAME = 'csrftoken';
|
|
const CSRF_HEADER_NAME = 'X-CSRFToken';
|
|
|
|
const LOGIN_CONTENT_TYPE = 'application/x-www-form-urlencoded';
|
|
|
|
class APIClient {
|
|
constructor () {
|
|
this.http = axios.create({
|
|
xsrfCookieName: CSRF_COOKIE_NAME,
|
|
xsrfHeaderName: CSRF_HEADER_NAME,
|
|
});
|
|
}
|
|
|
|
/* eslint class-methods-use-this: ["error", { "exceptMethods": ["getCookie"] }] */
|
|
getCookie () {
|
|
return document.cookie;
|
|
}
|
|
|
|
isAuthenticated () {
|
|
let authenticated = false;
|
|
|
|
const parsed = (`; ${this.getCookie()}`).split('; userLoggedIn=');
|
|
|
|
if (parsed.length === 2) {
|
|
authenticated = parsed.pop().split(';').shift() === 'true';
|
|
}
|
|
|
|
return authenticated;
|
|
}
|
|
|
|
async login (username, password, redirect = constant.API_CONFIG) {
|
|
const un = encodeURIComponent(username);
|
|
const pw = encodeURIComponent(password);
|
|
const next = encodeURIComponent(redirect);
|
|
|
|
const data = `username=${un}&password=${pw}&next=${next}`;
|
|
const headers = { 'Content-Type': LOGIN_CONTENT_TYPE };
|
|
|
|
await this.http.get(constant.API_LOGIN, { headers });
|
|
await this.http.post(constant.API_LOGIN, data, { headers });
|
|
}
|
|
|
|
BaseGet = (endpoint) => this.http.get(endpoint);
|
|
|
|
}
|
|
|
|
export default new APIClient();
|