From ea42d10ddf9dc83d61c214bfc0b7f7b4ef0dc236 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 23 Nov 2023 17:26:09 +0100 Subject: [PATCH] init login --- frontend/static/js/api/client.js | 32 ++++++++++++++++ frontend/static/js/index.js | 12 +++++- .../static/js/views/accounts/LoginView.js | 38 +++++++++++++++++++ frontend/templates/index.html | 1 + 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 frontend/static/js/api/client.js create mode 100644 frontend/static/js/views/accounts/LoginView.js diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js new file mode 100644 index 0000000..8aa9405 --- /dev/null +++ b/frontend/static/js/api/client.js @@ -0,0 +1,32 @@ +class Client +{ + constructor(url) + { + this._url = url; + this._token = undefined; + } + + get isAuthentificate() + { + return this.token != undefined; + } + + async _post(uri, data) + { + let response = await fetch(this._url + uri, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }); + return response; + } + + async login(username, password) + { + return this._post("/api/accounts/login", {username: username, password: password}) + } +} + +export {Client} \ No newline at end of file diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index f63c27c..d408ae9 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -1,8 +1,13 @@ +import LoginView from "./views/accounts/LoginView.js"; import Dashboard from "./views/Dashboard.js"; import Posts from "./views/Posts.js"; import PostView from "./views/PostView.js"; import Settings from "./views/Settings.js"; +import { Client } from "./api/client.js"; + +let client = new Client(location.protocol + "//" + location.host) + const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$"); const getParams = match => { @@ -24,7 +29,8 @@ const router = async () => { { path: "/", view: Dashboard }, { path: "/posts", view: Posts }, { path: "/posts/:id", view: PostView }, - { path: "/settings", view: Settings } + { path: "/settings", view: Settings }, + { path: "/login", view: LoginView }, ]; // Test each route for potential match @@ -60,4 +66,6 @@ document.addEventListener("DOMContentLoaded", () => { }); router(); -}); \ No newline at end of file +}); + +export { client } \ No newline at end of file diff --git a/frontend/static/js/views/accounts/LoginView.js b/frontend/static/js/views/accounts/LoginView.js new file mode 100644 index 0000000..1f83d90 --- /dev/null +++ b/frontend/static/js/views/accounts/LoginView.js @@ -0,0 +1,38 @@ +import AbstractView from "../AbstractView.js"; +import { client } from "../../index.js"; + +async function login() +{ + let username = document.getElementById("username").value; + let password = document.getElementById("password").value; + + let response = await client.login(username, password); + let errors = await response.json(); + + errors.user.forEach(error => { + console.log(error) + }); +} + +export default class extends AbstractView { + constructor(params) { + super(params); + this.setTitle("Login"); + document.body.addEventListener("click", e => { + e.preventDefault(); + if (e.target.type == "button") + { + login(); + } + }); + } + + async getHtml() { + return ` + + + + + `; + } +} \ No newline at end of file diff --git a/frontend/templates/index.html b/frontend/templates/index.html index 6fcaec3..6dbc23c 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -12,6 +12,7 @@ Dashboard Posts Settings + Login