add api
Signed-off-by: garionion <github@entr0py.de>
This commit is contained in:
parent
bb249e63fc
commit
e7cd3a4e6b
11
package-lock.json
generated
11
package-lock.json
generated
|
@ -7,6 +7,7 @@
|
|||
"": {
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"idb-keyval": "^5.0.4",
|
||||
"vue": "^3.0.5",
|
||||
"vue-router": "^4.0.5"
|
||||
},
|
||||
|
@ -997,6 +998,11 @@
|
|||
"postcss": "^8.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/idb-keyval": {
|
||||
"version": "5.0.4",
|
||||
"resolved": "https://registry.npmjs.org/idb-keyval/-/idb-keyval-5.0.4.tgz",
|
||||
"integrity": "sha512-qS0kplHuadZujoE90ze0NUkhW0/Fbfib7d+mYNMXNEn45NSh2NWY3fBewoX4GZUsKkGHBgc8JiAwMx0zrfL3LQ=="
|
||||
},
|
||||
"node_modules/indexes-of": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/indexes-of/-/indexes-of-1.0.1.tgz",
|
||||
|
@ -2945,6 +2951,11 @@
|
|||
"dev": true,
|
||||
"requires": {}
|
||||
},
|
||||
"idb-keyval": {
|
||||
"version": "5.0.4",
|
||||
"resolved": "https://registry.npmjs.org/idb-keyval/-/idb-keyval-5.0.4.tgz",
|
||||
"integrity": "sha512-qS0kplHuadZujoE90ze0NUkhW0/Fbfib7d+mYNMXNEn45NSh2NWY3fBewoX4GZUsKkGHBgc8JiAwMx0zrfL3LQ=="
|
||||
},
|
||||
"indexes-of": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/indexes-of/-/indexes-of-1.0.1.tgz",
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
"format": "npx prettier --write ."
|
||||
},
|
||||
"dependencies": {
|
||||
"idb-keyval": "^5.0.4",
|
||||
"vue": "^3.0.5",
|
||||
"vue-router": "^4.0.5"
|
||||
},
|
||||
|
|
52
src/api/index.ts
Normal file
52
src/api/index.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import { LoginResponse, token } from "./types";
|
||||
import { get, set } from "idb-keyval";
|
||||
import { unref } from "vue";
|
||||
|
||||
const url = "http://192.168.178.5:8080/api";
|
||||
let token: token;
|
||||
let ready = false;
|
||||
|
||||
export async function init() {
|
||||
if (ready) return;
|
||||
|
||||
await get("token").then((val: token) => {
|
||||
if (val !== undefined) {
|
||||
token = val;
|
||||
}
|
||||
});
|
||||
ready = true;
|
||||
}
|
||||
|
||||
export async function isLoggedIn(): Promise<boolean> {
|
||||
await init();
|
||||
return token !== undefined && token.expiration > Date.now();
|
||||
}
|
||||
|
||||
export async function login(user: string, password: string): Promise<boolean> {
|
||||
await init();
|
||||
let response = await fetch(`${url}/auth`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ username: user, password: password }),
|
||||
});
|
||||
const login: LoginResponse = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
if (login.success) {
|
||||
token = {
|
||||
jwt: login.token,
|
||||
expiration: Date.parse(login.expiration),
|
||||
};
|
||||
set("token", token).catch((error) => console.error(error));
|
||||
return true;
|
||||
} else {
|
||||
return Promise.reject(new Error(login.message));
|
||||
}
|
||||
} else {
|
||||
const error = new Error(login.message);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
11
src/api/types.ts
Normal file
11
src/api/types.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
export interface token {
|
||||
jwt: string;
|
||||
expiration: number;
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
expiration: string;
|
||||
message: string;
|
||||
success: boolean;
|
||||
token: string;
|
||||
}
|
Loading…
Reference in a new issue