diff --git a/src/components/api.ts b/src/components/api.ts new file mode 100644 index 0000000..a87e157 --- /dev/null +++ b/src/components/api.ts @@ -0,0 +1,46 @@ + +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(); diff --git a/src/components/app-home/app-home.tsx b/src/components/app-home/app-home.tsx index bddc5f9..3b010ce 100644 --- a/src/components/app-home/app-home.tsx +++ b/src/components/app-home/app-home.tsx @@ -1,4 +1,5 @@ -import { Component, h } from '@stencil/core'; +import { Component, h} from '@stencil/core'; +import { Api } from '../api'; @Component({ tag: 'app-home', @@ -6,6 +7,14 @@ import { Component, h } from '@stencil/core'; shadow: true, }) export class AppHome { + constructor() { + this.start() + } + + async start() { + await Api.getApiurl(); + await Api.login(); + } render() { return (