-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change login middleware logic (#739)
* change login middleware logic * fixes * refix with write prettier version * add user to forced login routes * fix regex * typo and reorder * add no login
- Loading branch information
1 parent
71337ed
commit 8eb6756
Showing
5 changed files
with
61 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import { shouldMiddlewareEnforceLogin } from './shouldMiddlewareEnforceLogin'; | ||
import { testOrganism } from '../../vitest.setup.ts'; | ||
|
||
const otherOrganism = 'otherOrganism'; | ||
const configuredOrganisms = [testOrganism, otherOrganism]; | ||
|
||
describe('shouldMiddlewareEnforceLogin', () => { | ||
test('should return false if not specified', () => { | ||
expectNoLogin('/someRoute'); | ||
}); | ||
|
||
test('should return false for empty string', () => { | ||
expectNoLogin(''); | ||
}); | ||
|
||
test('should return true on routes which should force login', () => { | ||
expectForceLogin('/user'); | ||
expectForceLogin('/user/someUsername'); | ||
expectForceLogin(`/${testOrganism}/revise`); | ||
expectForceLogin(`/${testOrganism}/submit`); | ||
}); | ||
|
||
test('should return false for various public routes', () => { | ||
expectNoLogin(`/${testOrganism}/search`); | ||
expectNoLogin(`/`); | ||
expectNoLogin(`/${testOrganism}`); | ||
expectNoLogin(`/${testOrganism}/sequences/id_002156`); | ||
}); | ||
|
||
function expectForceLogin(path: string) { | ||
expect(shouldMiddlewareEnforceLogin(path, configuredOrganisms), path).toBe(true); | ||
} | ||
|
||
function expectNoLogin(path: string) { | ||
expect(shouldMiddlewareEnforceLogin(path, configuredOrganisms), path).toBe(false); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const enforcedLoginRoutesCache: Record<string, RegExp[]> = {}; | ||
|
||
function getEnforcedLoginRoutes(configuredOrganisms: string[]) { | ||
const cacheKey = configuredOrganisms.join(''); | ||
if (!(cacheKey in enforcedLoginRoutesCache)) { | ||
const organismSpecificRoutes = configuredOrganisms.flatMap((organism) => [ | ||
new RegExp(`^/${organism}/revise`), | ||
new RegExp(`^/${organism}/submit`), | ||
new RegExp(`^/${organism}/user`), | ||
]); | ||
|
||
enforcedLoginRoutesCache[cacheKey] = [new RegExp('^/user/?'), ...organismSpecificRoutes]; | ||
} | ||
return enforcedLoginRoutesCache[cacheKey]; | ||
} | ||
|
||
export function shouldMiddlewareEnforceLogin(pathname: string, configuredOrganisms: string[]) { | ||
return getEnforcedLoginRoutes(configuredOrganisms).some((route) => route.test(pathname)); | ||
} |