47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
|
|
class ApiController {
|
|
username: string;
|
|
password: string;
|
|
token: string;
|
|
apiurl: string;
|
|
constructor() {
|
|
this.username = 'test';
|
|
this.password = 'prozac-prowler-stoop-patriot';
|
|
}
|
|
|
|
async getApiurl() {
|
|
let response = await fetch(`${window.location.href}apiurl`)
|
|
this.apiurl = await response.text();
|
|
console.log(this.apiurl);
|
|
}
|
|
|
|
|
|
async login() {
|
|
let promise = await fetch(`${this.apiurl}/token`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
"accept": "application/json",
|
|
},
|
|
body: new URLSearchParams({
|
|
username: this.username,
|
|
password: this.password
|
|
})
|
|
|
|
});
|
|
if(promise.ok) {
|
|
this.token = (await promise.json())['access_token'];
|
|
console.log(this.token);
|
|
}
|
|
}
|
|
setToken(token: string) {
|
|
this.token = token;
|
|
}
|
|
|
|
getToken() :string {
|
|
return this.token;
|
|
}
|
|
}
|
|
|
|
export const Api = new ApiController();
|