fix: connexion

This commit is contained in:
2023-11-29 16:05:49 +01:00
parent b5b54a98ba
commit 6dc0293455
9 changed files with 88 additions and 27 deletions

View File

@ -1,26 +1,27 @@
import { Accounts } from "./accounts.js";
function extract_token(response)
{
let cookies = response.headers.get("set-cookie");
if (cookies == null)
return null;
let token = cookies.slice(cookies.indexOf("=") + 1, cookies.indexOf(';'))
return token;
}
class Client
{
constructor(url)
{
this._url = url;
this.accounts = new Accounts(this);
this._token = undefined;
this._logged = undefined;
}
isAuthentificate()
async isAuthentificate()
{
return this.token != undefined;
if (this._logged == undefined)
this.logged = await this._test_logged();
return this.logged;
}
async _get(uri)
{
let response = await fetch(this._url + uri, {
method: "GET",
});
return response;
}
async _post(uri, data)
@ -29,19 +30,35 @@ class Client
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
body: JSON.stringify(data),
});
let token = extract_token(response);
if (token != null)
this.token = token;
return response;
}
async login(username, password)
{
let response = await this._post("/api/accounts/login", {username: username, password: password})
return response
let data = await response.json();
if (data == "user connected")
{
this.logged = true;
return null;
}
return data;
}
async logout()
{
await this._get("/api/accounts/logout");
this.logged = false;
}
async _test_logged()
{
let response = await this._get("/api/accounts/logged");
let data = await response.json();
return data === "True";
}
}

View File

@ -6,6 +6,7 @@ import Settings from "./views/Settings.js";
import Chat from "./views/Chat.js";
import HomeView from "./views/HomeView.js";
import RegisterView from "./views/accounts/RegisterView.js";
import LogoutView from "./views/accounts/LogoutView.js";
import { Client } from "./api/client.js";
@ -34,6 +35,7 @@ const router = async () => {
{ path: "/posts/:id", view: PostView },
{ path: "/settings", view: Settings },
{ path: "/login", view: LoginView },
{ path: "/logout", view: LogoutView },
{ path: "/register", view: RegisterView },
{ path: "/chat", view: Chat },
{ path: "/home", view: HomeView },
@ -55,7 +57,6 @@ const router = async () => {
result: [location.pathname]
};
}
const view = new match.route.view(getParams(match));
document.querySelector("#app").innerHTML = await view.getHtml();

View File

@ -4,14 +4,18 @@ import { client, navigateTo } from "../index.js";
export default class extends AbstractView {
constructor(params) {
super(params);
if (client.isAuthentificate() == false)
navigateTo("/home");
this.setTitle("register");
this.setTitle("Home");
}
async getHtml() {
if (await client.isAuthentificate() === false)
{
navigateTo("/login");
return;
}
return `
<h1>HOME</h1>
<a href="/logout" class="nav__link" data-link>Logout</a>
`;
}
}

View File

@ -6,10 +6,9 @@ async function login()
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
let response = await client.login(username, password);
let response_data = await response.json();
let response_data = await client.login(username, password);
if (response_data == "user connected")
if (response_data == null)
{
navigateTo("/home");
return;
@ -36,6 +35,11 @@ export default class extends AbstractView {
async postInit()
{
if (await client.isAuthentificate())
{
navigateTo("/home")
return;
}
document.getElementById("button").onclick = login;
}

View File

@ -0,0 +1,13 @@
import { client, navigateTo } from "../../index.js";
import AbstractView from "../AbstractView.js";
export default class extends AbstractView
{
constructor(params) {
super(params);
this.setTitle("Logout");
if (client.logged)
client.logout();
navigateTo("/login")
}
}

View File

@ -1,5 +1,5 @@
import AbstractView from "../AbstractView.js";
import { client } from "../../index.js";
import { client, navigateTo } from "../../index.js";
async function register()
{
@ -30,6 +30,11 @@ export default class extends AbstractView {
async postInit()
{
if (client.isAuthentificate())
{
navigateTo("/home")
return;
}
document.getElementById("button").onclick = register;
}