From fca54b30ddb657f7de4a1ef5e3f744175fcf7a83 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Thu, 26 Sep 2024 00:45:26 +0200 Subject: [PATCH] add login refer --- main.ts | 3 ++- public/login.js | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index 5d27d7d..16fe1a7 100644 --- a/main.ts +++ b/main.ts @@ -149,7 +149,8 @@ function authMiddleware(): oak.Middleware { return async (ctx, next) => { const token = await ctx.cookies.get("Authorization"); if (!token || !await db.sessionWhereToken(token)) { - ctx.response.redirect("/login.html"); + const path = encodeURIComponent(ctx.request.url.pathname); + ctx.response.redirect(`/login.html?refer=${path}`); return; } await next(); diff --git a/public/login.js b/public/login.js index e4474f2..5840bd2 100644 --- a/public/login.js +++ b/public/login.js @@ -18,6 +18,11 @@ document.querySelector("#login").onsubmit = async (event) => { loginErrorSpan.hidden = false; return; } - window.location.pathname = "/"; + const params = new URLSearchParams(window.location.search); + if (!params.has("refer")) { + window.location.pathname = "/"; + return; + } + window.location.pathname = decodeURIComponent(params.get("refer")); };