front: redirect to last visited page after login

This commit is contained in:
AdrienLSH
2024-01-19 10:54:14 +01:00
parent e1e6e6c1ab
commit 60a3e02420
4 changed files with 23 additions and 17 deletions

View File

@ -2,7 +2,7 @@ import { client, navigateTo } from "../../index.js";
import { clear, fill_errors } from "../../utils/formUtils.js";
import AbstractNonAuthentifiedView from "../abstracts/AbstractNonAuthentified.js";
async function login()
async function login(redirectTo = '/home')
{
clear('innerHTML', ['username', 'password', 'login']);
@ -23,7 +23,7 @@ async function login()
if (response.status == 200) {
await client.notice.disconnect();
await client.notice.connect();
navigateTo("/home");
navigateTo(redirectTo);
} else {
let error = await response.json();
fill_errors(error, "innerHTML");
@ -31,8 +31,9 @@ async function login()
}
export default class extends AbstractNonAuthentifiedView {
constructor(params) {
super(params, "Login", "/home");
constructor(params, lastUrlBeforeLogin = '/home') {
super(params, "Login", lastUrlBeforeLogin);
this.redirectTo = lastUrlBeforeLogin;
}
async postInit()
@ -40,15 +41,15 @@ export default class extends AbstractNonAuthentifiedView {
let usernameField = document.getElementById('usernameInput');
usernameField.addEventListener('keydown', ev => {
if (ev.key === 'Enter')
login();
login(this.redirectTo);
});
usernameField.focus();
let passwordField = document.getElementById('passwordInput');
passwordField.addEventListener('keydown', ev => {
if (ev.key === 'Enter')
login();
login(this.redirectTo);
});
document.getElementById('loginButton').onclick = login;
document.getElementById('loginButton').onclick = _ => login(this.redirectTo);
}
async getHtml() {