mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
Add sign-out in the level editor header (me endpoint, CSRF logout form)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -287,3 +287,16 @@ pre.mono {
|
||||
max-height: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* Compte connecté (éditeur protégé par une connexion) : nom et bouton de déconnexion. */
|
||||
.author-account {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 0.5rem 0.75rem;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.author-account-name {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ export class AuthorApp {
|
||||
#store;
|
||||
#catalogs;
|
||||
#root;
|
||||
#account = null; // { username, csrf } si l'éditeur est protégé par une connexion (backend Django), sinon null
|
||||
#knownIds = [];
|
||||
#model = emptyLevelModel();
|
||||
#loadedId = null;
|
||||
@@ -64,6 +65,7 @@ export class AuthorApp {
|
||||
}
|
||||
|
||||
async run() {
|
||||
this.#account = await this.#loadAccount();
|
||||
this.#knownIds = (await this.#lessons.listLevels()).map((level) => level.id);
|
||||
const draft = await this.#store.get(DRAFT_KEY).catch(() => undefined);
|
||||
if (draft) {
|
||||
@@ -78,6 +80,38 @@ export class AuthorApp {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Auteur connecté, si l'éditeur est servi derrière la connexion du backend Django. Sans backend (essai local avec
|
||||
* `make dev`, serveur statique), la requête échoue ou renvoie 404 : aucun bouton de déconnexion n'est alors affiché.
|
||||
*/
|
||||
async #loadAccount() {
|
||||
try {
|
||||
const response = await fetch(new URL('api/auth/me/', document.baseURI), { cache: 'no-store', credentials: 'same-origin' });
|
||||
if (!response.ok || !(response.headers.get('content-type') ?? '').includes('application/json')) {
|
||||
return null;
|
||||
}
|
||||
const data = await response.json();
|
||||
return typeof data.username === 'string' && typeof data.csrf === 'string' ? data : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Formulaire de déconnexion (vrai POST protégé par CSRF : la page de connexion s'affiche ensuite). */
|
||||
#accountControl() {
|
||||
if (!this.#account) {
|
||||
return null;
|
||||
}
|
||||
const t = this.#t;
|
||||
return el(
|
||||
'form',
|
||||
{ class: 'author-account', method: 'post', action: new URL('api/auth/logout/', document.baseURI).href, 'aria-label': t('author.account.label') },
|
||||
el('input', { type: 'hidden', name: 'csrfmiddlewaretoken', value: this.#account.csrf }),
|
||||
el('span', { class: 'author-account-name', text: t('author.account.signed_in', { name: this.#account.username }) }),
|
||||
el('button', { type: 'submit', class: 'btn', id: 'author-logout', text: t('author.account.logout') }),
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Construction de l'interface -----------------------------------------------------------------------
|
||||
|
||||
#t = (key, params) => this.#i18n.t(key, params);
|
||||
@@ -120,6 +154,7 @@ export class AuthorApp {
|
||||
languages,
|
||||
el('a', { class: 'btn', href: './', text: t('author.toolbar.back') }),
|
||||
),
|
||||
this.#accountControl(),
|
||||
this.#status,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -150,5 +150,8 @@
|
||||
"author.kind.drawing_has_line.help": "A line between two points, either direction: “0,0 100,0” or with a colour “0,0 100,0 red”. Tolerance 0.5.",
|
||||
"author.kind.drawing_colors": "Drawing: colours used",
|
||||
"author.kind.drawing_colors.help": "Colours that must all appear, comma-separated: “red,blue”.",
|
||||
"lint.drawing_value_invalid": "Malformed drawing test value (see the test type help; colours: black, red, green, blue, orange, purple, gray)."
|
||||
"lint.drawing_value_invalid": "Malformed drawing test value (see the test type help; colours: black, red, green, blue, orange, purple, gray).",
|
||||
"author.account.label": "Account",
|
||||
"author.account.signed_in": "Signed in: {name}",
|
||||
"author.account.logout": "Sign out"
|
||||
}
|
||||
|
||||
@@ -150,5 +150,8 @@
|
||||
"author.kind.drawing_has_line.help": "Un trait entre deux points, dans un sens ou dans l'autre : « 0,0 100,0 » ou avec couleur « 0,0 100,0 red ». Tolérance 0,5.",
|
||||
"author.kind.drawing_colors": "Dessin : couleurs utilisées",
|
||||
"author.kind.drawing_colors.help": "Couleurs qui doivent toutes apparaître, séparées par des virgules : « red,blue ».",
|
||||
"lint.drawing_value_invalid": "Valeur de test de dessin mal formée (voir l'aide du type de test ; couleurs : black, red, green, blue, orange, purple, gray)."
|
||||
"lint.drawing_value_invalid": "Valeur de test de dessin mal formée (voir l'aide du type de test ; couleurs : black, red, green, blue, orange, purple, gray).",
|
||||
"author.account.label": "Compte",
|
||||
"author.account.signed_in": "Connecté : {name}",
|
||||
"author.account.logout": "Se déconnecter"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user