diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index af6fbda..ba8ef78 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -22,6 +22,7 @@ import TournamentCreateView from "./views/tournament/TournamentCreateView.js"; let client = new Client(location.protocol + "//" + location.host) let lastView = undefined +let lastPageUrlBeforeLogin = undefined const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$"); @@ -106,12 +107,14 @@ const router = async(uri) => { if (lastView !== undefined) await lastView.leavePage(); - const view = new match.route.view(getParams(match)); + const view = new match.route.view(getParams(match), lastPageUrlBeforeLogin); if (view instanceof AbstractRedirectView && await view.redirect()) return 1; lastView = view; + if (uri !== '/login' && uri !== '/register' && uri !== '/logout') + lastPageUrlBeforeLogin = uri; if (await renderView(view)) return 1; diff --git a/frontend/static/js/views/accounts/LoginView.js b/frontend/static/js/views/accounts/LoginView.js index 6f2042a..6ecc3e1 100644 --- a/frontend/static/js/views/accounts/LoginView.js +++ b/frontend/static/js/views/accounts/LoginView.js @@ -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() { diff --git a/frontend/static/js/views/accounts/LogoutView.js b/frontend/static/js/views/accounts/LogoutView.js index fb28eaa..e60ffce 100644 --- a/frontend/static/js/views/accounts/LogoutView.js +++ b/frontend/static/js/views/accounts/LogoutView.js @@ -3,13 +3,14 @@ import AbstractAuthentifiedView from "../abstracts/AbstractAuthentifiedView.js"; export default class extends AbstractAuthentifiedView { - constructor(params) { + constructor(params, lastPageUrl = '/login') { super(params, "Logout"); + this.lastPageUrl = lastPageUrl; } async postInit() { await client.logout(); await client.notice.disconnect(); - navigateTo("/login") + navigateTo(this.lastPageUrl); } } diff --git a/frontend/static/js/views/accounts/RegisterView.js b/frontend/static/js/views/accounts/RegisterView.js index 17b9ae8..9696feb 100644 --- a/frontend/static/js/views/accounts/RegisterView.js +++ b/frontend/static/js/views/accounts/RegisterView.js @@ -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 register() +async function register(redirectTo = '/home') { let username = document.getElementById("usernameInput").value; let password = document.getElementById("passwordInput").value; @@ -20,7 +20,7 @@ async function register() if (response_data == null) { - navigateTo("/home"); + navigateTo(redirectTo); return; } @@ -29,8 +29,9 @@ async function register() } export default class extends AbstractNonAuthentifiedView { - constructor(params) { - super(params, "Register", "/home"); + constructor(params, lastUrlBeforeLogin = '/home') { + super(params, "Register", lastUrlBeforeLogin); + this.redirectTo = lastUrlBeforeLogin; } async postInit() @@ -38,15 +39,15 @@ export default class extends AbstractNonAuthentifiedView { let usernameField = document.getElementById('usernameInput'); usernameField.addEventListener('keydown', ev => { if (ev.key === 'Enter') - register(); + register(this.redirectTo); }); usernameField.focus(); let passwordField = document.getElementById('passwordInput'); passwordField.addEventListener('keydown', ev => { if (ev.key === 'Enter') - register(); + register(this.redirectTo); }); - document.getElementById("registerButton").onclick = register; + document.getElementById("registerButton").onclick = _ => register(this.redirectTo); } async getHtml() {