Basic login

This commit is contained in:
Jimmy Allen 2022-01-19 00:47:11 +13:00
parent 91551e711d
commit ebaf09d045
2 changed files with 56 additions and 1 deletions

46
src/components/api.ts Normal file
View File

@ -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();

View File

@ -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 (
<div class="app-home">