From b2008bb8669afa27e7138a0ff003b8ab621fbad7 Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Fri, 15 Dec 2023 19:17:04 +0100 Subject: [PATCH] Fixed strategy and authentication --- README.md | 0 html/template.html | 2 +- js/api/agent.js | 28 +++++++++---- js/api/config.js | 4 +- js/api/planet.js | 3 ++ js/api/system.js | 3 ++ js/auth/auth.js | 86 +++++++++++++++++++++++--------------- js/commun/commun.js | 13 ++++++ js/commun/strategie.js | 26 ++++++------ js/index.js | 41 ++++++++++++------ js/ui/templeting_engine.js | 20 ++++++--- 11 files changed, 150 insertions(+), 76 deletions(-) create mode 100644 README.md create mode 100644 js/commun/commun.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/html/template.html b/html/template.html index d988745..68962a6 100644 --- a/html/template.html +++ b/html/template.html @@ -7,7 +7,7 @@ - +
diff --git a/js/api/agent.js b/js/api/agent.js index 1d366a2..e240afe 100644 --- a/js/api/agent.js +++ b/js/api/agent.js @@ -1,3 +1,4 @@ +// Copyright © 2023 Entreprise SkamKraft 'use strict'; import { SpaceTraders } from "./config.js" @@ -18,7 +19,7 @@ export class Agent { } export class AgentBuilder { - static async create(symbol, faction, callback, email = "") { + static async create(symbol, faction, callback, error_handler, email = "") { const url = `${SpaceTraders.host}register`; await $.ajax({ url: url, @@ -30,10 +31,13 @@ export class AgentBuilder { let agent = new Agent(reponse.data.agent, reponse.data.token) callback(agent); }, + error: (err) => { + error_handler(["Symbol is invalid."]) + } }); } - static async get(token, callback){ + static async get(token, callback, error_handler){ const url = `${SpaceTraders.host}my/agent`; await $.ajax({ url: url, @@ -46,16 +50,20 @@ export class AgentBuilder { let agent = new Agent(reponse.data, token); callback(agent); }, + error: (err) => { + error_handler(["Token invalide."]); + } }); } static async get_public(symbol, callback) { const url = `${SpaceTraders.host}agents/${symbol}`; - const headers = { Accept: "application/json" }; await $.ajax({ url: url, method: "GET", - headers: headers, + headers: { + Accept: "application/json" + }, success: (reponse) => { let agent = new Agent(reponse.data); callback(agent); @@ -65,12 +73,13 @@ export class AgentBuilder { static async list(limit, page, callback, agents = []) { const url = `${SpaceTraders.host}agents`; - const headers = { Accept: "application/json" }; const data = { limit, page }; await $.ajax({ url: url, method: "GET", - headers: headers, + headers: { + Accept: "application/json" + }, data: data, success: (reponse) => { reponse.data.forEach(agent => { @@ -84,15 +93,16 @@ export class AgentBuilder { static async list_all(callback) { await this.list(1,1, (agents, meta) => { let maxPage = meta.total / 20; - this.r_listing(1, maxPage, [], callback); + this.#r_listing(1, maxPage, [], callback); }); } - static async r_listing(page, maxPage, agents, callback) { + static async #r_listing(page, maxPage, agents, callback) { if (page < maxPage) { this.list(20, page++,() => { setTimeout(() => { - this.r_listing(page++, maxPage, agents, callback); + callback(agents); + this.#r_listing(page++, maxPage, agents, callback); }, 1000); }, agents); } else { diff --git a/js/api/config.js b/js/api/config.js index 7108252..bb262fb 100644 --- a/js/api/config.js +++ b/js/api/config.js @@ -1,3 +1,5 @@ +'use strict'; export const SpaceTraders = { - host: "https://api.spacetraders.io/v2/" + host: "https://api.spacetraders.io/v2/", + limit_max: 20, } \ No newline at end of file diff --git a/js/api/planet.js b/js/api/planet.js index e69de29..5587d30 100644 --- a/js/api/planet.js +++ b/js/api/planet.js @@ -0,0 +1,3 @@ +export class Planet { + +} \ No newline at end of file diff --git a/js/api/system.js b/js/api/system.js index e69de29..0d59cb2 100644 --- a/js/api/system.js +++ b/js/api/system.js @@ -0,0 +1,3 @@ +export class System { + +} \ No newline at end of file diff --git a/js/auth/auth.js b/js/auth/auth.js index 53964e1..bc2033c 100644 --- a/js/auth/auth.js +++ b/js/auth/auth.js @@ -1,36 +1,40 @@ +// Copyright © 2023 Entreprise SkamKraft +'use strict'; import { AgentBuilder } from '../api/agent.js' import Strategie from '../commun/strategie.js'; +let strategies = { + register: [ + { + name: "symbol", + validations: [ + "required", + "max_length|14" + ] + }, + { + name: "faction", + validations: [ + "required" + ] + } + ], + login: [ + { + name: "token", + validations: [ + "required" + ] + } + ] +} + export class Auth { constructor(store = false) { this.store = store; this.validated = () => {}; this.error_handler = () => {}; - this.strategies = { - register: [ - { - name: "symbol", - validations: [ - "required", - "max_lenght|14" - ] - }, - { - name: "faction", - validations: [ - "required" - ] - } - ], - login: [ - { - name: "token", - validations: [ - "required" - ] - } - ] - } + this.strategies = strategies; } done(validated) { @@ -43,24 +47,38 @@ export class Auth { return this; } - login(token) { + async login(token) { let validateur = new Strategie(this.strategies.login); validateur.validate("token", token); - if (validateur.errors) this.error_handler(validateur.errors); + if (validateur.errors.length > 0) this.error_handler(validateur.errors); else { - if (store) localStorage.setItem("token", token); - AgentBuilder.get(token, this.validated); + if (this.store) localStorage.setItem("token", token); + await AgentBuilder.get(token, this.validated, this.error_handler); } } - register(new_agent) { + async relog() { + if(this.is_login()) await AgentBuilder.get(localStorage.getItem("token"), this.validated, this.error_handler); + else return false; + } + + async register(new_agent) { let validateur = new Strategie(this.strategies.register); validateur.validate("symbol", new_agent.symbol); validateur.validate("faction", new_agent.faction); - if (validateur.errors) this.error_handler(validateur.errors); - else { - if (store) localStorage.setItem("token", token); - AgentBuilder.create(new_agent.symbol, new_agent.faction, this.validated); + if (validateur.errors.length > 0) this.error_handler(validateur.errors); + else { + await AgentBuilder.create(new_agent.symbol, new_agent.faction, (agent) => { + if (this.store) localStorage.setItem("token", agent.token); + this.validated(agent); + }, this.error_handler); + } + } + + #is_login() { + if (this.store && localStorage.getItem("token")) { + return true } + return false } } \ No newline at end of file diff --git a/js/commun/commun.js b/js/commun/commun.js new file mode 100644 index 0000000..4cd64a3 --- /dev/null +++ b/js/commun/commun.js @@ -0,0 +1,13 @@ +// Copyright © 2023 Entreprise SkamKraft +'use strict'; +export class Initialzer { + constructor(UI) { + this.UI = UI; + } + + init_menu_link(tag, template) { + this.UI.add_event(tag, "click", () => { + this.UI.render(`templates/${template}`); + }) + } +} \ No newline at end of file diff --git a/js/commun/strategie.js b/js/commun/strategie.js index dbe9bf5..0fb3705 100644 --- a/js/commun/strategie.js +++ b/js/commun/strategie.js @@ -1,3 +1,4 @@ +// Copyright © 2023 Entreprise SkamKraft export default class Strategie { constructor(strategie) { this.strategie = strategie; @@ -5,44 +6,43 @@ export default class Strategie { } validate(name, input) { - this.errors = []; this.strategie.forEach(input_strat => { if(input_strat.name === name) input_strat.validations.forEach((validation) => { let args = validation.split("|"); switch (args[0]) { case "required": - this.test(this.required(input), `${name} is required.`); + this.#test(Strategie.#required(input), `${name} is required.`); break; - case "max_lenght": - this.test(this.max_lenght(input, args[1]), `${name} must have a max lenght of ${args[1]}.`); + case "max_length": + this.#test(Strategie.#max_length(input, parseInt(args[1])), `${name} must have a max lenght of ${args[1]}.`); break; - case "min_lenght": - this.test(this.min_lenght(input, args[1]), `${name} must have a min lenght of ${args[1]}`); + case "min_length": + this.#test(Strategie.#min_length(input, parseInt(args[1])), `${name} must have a min lenght of ${args[1]}`); break; } }); }); } - test(test, error) { + #test(test, error) { if(!test) this.errors.push(error); } - valide_email(input) { + static #valide_email(input) { } - min_lenght(input, lenght) { - if(input.lenght < lenght) return false; + static #min_length(input, length) { + if(input.length < length) return false; return true; } - max_lenght(input, lenght) { - if(input.lenght > args[0]) return false; + static #max_length(input, length) { + if(input.length > length) return false; return true; } - required(input) { + static #required(input) { if (input === undefined || input === null || input === "") return false; return true; } diff --git a/js/index.js b/js/index.js index e5bd147..76e3a20 100644 --- a/js/index.js +++ b/js/index.js @@ -1,19 +1,34 @@ -import {UIRenderer} from "./ui/templeting_engine.js"; +// Copyright © 2023 Entreprise SkamKraft +'use strict'; +import { UIRenderer } from "./ui/templeting_engine.js"; +import { Initialzer } from "./commun/commun.js"; +import { AgentBuilder } from "./api/agent.js"; +import { Auth } from "./auth/auth.js"; +let token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiSEFSRElDSyIsInZlcnNpb24iOiJ2Mi4xLjQiLCJyZXNldF9kYXRlIjoiMjAyMy0xMi0wMiIsImlhdCI6MTcwMjY2Mjc2Mywic3ViIjoiYWdlbnQtdG9rZW4ifQ.PrvaOz3W79acq6RoxryMW53PRRz824_AM0VGLwfXCOsGCOCAIY-rn6-bZTOnLAtp4xPSDqEk4c38oWYAWW59p0iMDDLpur6ONnjT0RjjsQS9zr5BByfBpP36CT23IZSSzk3XxGrFolHJAyU3K1liYfNbsPuNTXlkHGNHq6yMqH4ZQUPFsXEsCkg9cUynkdLw3C39SvWhtJ89oblj_8tQp2k8dxhZemepuXtiI51eFMpv8A0WRAi7pVYo_ajJujY9QDLYn_m5hDZWTlQMIstjPaDl99p2IMweIMO2Q2G-0lKiWQ4sl6VW5tuVrz1HLYU6kyMjFQWNn6kFDE7LWMTrfw"; let UI = new UIRenderer("html"); -UI.render("templates/home.html") +let initer = new Initialzer(UI); +UI.render("templates/home.html"); -function init_menu_link(tag, template) { - UI.add_event(tag, "click", () => { - UI.render(`templates/${template}`); - }) -} +let auth = new Auth(); +auth.done((agent) => { + console.log(agent); +}).fail((errs) => { + errs.forEach(err => { + console.log(err); + }); +}); + +await auth.register({ + symbol: "lkdsjfsjdlfjlk", + faction: "COSMIC" +}); $(document).ready(() => { - init_menu_link("#contracts-link", "contracts.html"); - init_menu_link("#ships-link", "ships.html"); - init_menu_link("#systems-link", "systems.html"); - init_menu_link("#signup-link", "register.html"); - init_menu_link("#signin-link", "login.html"); - init_menu_link(".nav-brand", "home.html"); + initer.init_menu_link("#contracts-link", "contracts.html"); + initer.init_menu_link("#ships-link", "ships.html"); + initer.init_menu_link("#systems-link", "systems.html"); + initer.init_menu_link("#signup-link", "register.html"); + initer.init_menu_link("#signin-link", "login.html"); + initer.init_menu_link(".nav-brand", "home.html"); }); \ No newline at end of file diff --git a/js/ui/templeting_engine.js b/js/ui/templeting_engine.js index 886a52a..d18f79c 100644 --- a/js/ui/templeting_engine.js +++ b/js/ui/templeting_engine.js @@ -1,16 +1,26 @@ +// Copyright © 2023 Entreprise SkamKraft +'use strict'; export class UIRenderer { constructor(path) { this.templatePath = path; } - render(template, tag = "#block-content") { - this.get_template((reponse) => { + + render(template) { + this.#get_template((reponse) => { $('body').html(reponse); - this.get_template((reponse) => { - $(tag).html(reponse); + this.#get_template((reponse) => { + $("block-content").html(reponse); }, template) }); } - get_template(callback, template = "") { + + frag_load(tag, template) { + this.#get_template((reponse) => { + $(tag).html(reponse); + }, template); + } + + #get_template(callback, template = "") { let url = template === "" ? `${this.templatePath}/template.html`: `${this.templatePath}/${template}`; let data = $.ajax(url,{ async: false,