From 3ee8da0cae572c3a6c81363d1b22fff017890f7c Mon Sep 17 00:00:00 2001 From: pedr Date: Thu, 30 Nov 2023 10:39:49 -0300 Subject: [PATCH] Server: Fixes #9367: Redirect user from login to home if already logged (#9415) --- .../server/src/routes/index/login.test.ts | 34 +++++++++++++++++++ packages/server/src/routes/index/login.ts | 6 +++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/server/src/routes/index/login.test.ts b/packages/server/src/routes/index/login.test.ts index 992b2d32d..c460895ab 100644 --- a/packages/server/src/routes/index/login.test.ts +++ b/packages/server/src/routes/index/login.test.ts @@ -72,4 +72,38 @@ describe('index_login', () => { } }); + test('should redirect if already logged in', async () => { + const user = await createUser(1); + + const context = await doLogin(user.email, '123456'); + const sessionId = cookieGet(context, 'sessionId'); + + const getContext = await koaAppContext({ + sessionId: sessionId, + request: { + method: 'GET', + url: '/login', + }, + }); + + await routeHandler(getContext); + + expect(getContext.response.status).toBe(302); + }); + + test('should not redirect if sessionId is not valid', async () => { + + const getContext = await koaAppContext({ + sessionId: 'no-sense', + request: { + method: 'GET', + url: '/login', + }, + }); + + await routeHandler(getContext); + + expect(getContext.response.status).toBe(200); + }); + }); diff --git a/packages/server/src/routes/index/login.ts b/packages/server/src/routes/index/login.ts index e9285b731..7c1a99dba 100644 --- a/packages/server/src/routes/index/login.ts +++ b/packages/server/src/routes/index/login.ts @@ -8,6 +8,7 @@ import defaultView from '../../utils/defaultView'; import { View } from '../../services/MustacheService'; import limiterLoginBruteForce from '../../utils/request/limiterLoginBruteForce'; import { cookieSet } from '../../utils/cookies'; +import { homeUrl } from '../../utils/urlUtils'; function makeView(error: any = null): View { const view = defaultView('login', 'Login'); @@ -22,7 +23,10 @@ const router: Router = new Router(RouteType.Web); router.public = true; -router.get('login', async (_path: SubPath, _ctx: AppContext) => { +router.get('login', async (_path: SubPath, ctx: AppContext) => { + if (ctx.joplin.owner) { + return redirect(ctx, homeUrl()); + } return makeView(); });