Skip to content

Commit 413e0be

Browse files
committed
🐛(frontend) fix redirection after login
The redirection after login was not working properly. The user was redirected to the home page instead of the page he was trying to access.
1 parent a2a184b commit 413e0be

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

src/frontend/apps/e2e/__tests__/app-impress/common.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { Page, expect } from '@playwright/test';
22

33
export const keyCloakSignIn = async (page: Page, browserName: string) => {
4-
const title = await page.locator('h1').first().textContent({
5-
timeout: 5000,
6-
});
7-
84
const login = `user-e2e-${browserName}`;
95
const password = `password-e2e-${browserName}`;
106

117
if (await page.getByLabel('Restart login').isVisible()) {
128
await page.getByRole('textbox', { name: 'password' }).fill(password);
139

1410
await page.click('input[type="submit"]', { force: true });
15-
} else if (title?.includes('Sign in to your account')) {
11+
} else {
1612
await page.getByRole('textbox', { name: 'username' }).fill(login);
1713

1814
await page.getByRole('textbox', { name: 'password' }).fill(password);

src/frontend/apps/e2e/__tests__/app-impress/doc-routing.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from '@playwright/test';
22

3-
import { keyCloakSignIn } from './common';
3+
import { keyCloakSignIn, mockedDocument } from './common';
44

55
test.describe('Doc Routing', () => {
66
test.beforeEach(async ({ page }) => {
@@ -43,9 +43,12 @@ test.describe('Doc Routing: Not loggued', () => {
4343
page,
4444
browserName,
4545
}) => {
46+
await mockedDocument(page, { link_reach: 'public' });
4647
await page.goto('/docs/mocked-document-id/');
48+
await expect(page.locator('h2').getByText('Mocked document')).toBeVisible();
49+
await page.getByRole('button', { name: 'Login' }).click();
4750
await keyCloakSignIn(page, browserName);
48-
await expect(page).toHaveURL(/\/docs\/mocked-document-id\/$/);
51+
await expect(page.locator('h2').getByText('Mocked document')).toBeVisible();
4952
});
5053

5154
test('The homepage redirects to login.', async ({ page }) => {

src/frontend/apps/impress/src/core/auth/Auth.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import { useAuthStore } from './useAuthStore';
1717
const regexpUrlsAuth = [/\/docs\/$/g, /^\/$/g];
1818

1919
export const Auth = ({ children }: PropsWithChildren) => {
20-
const { initAuth, initiated, authenticated, login } = useAuthStore();
21-
const { asPath } = useRouter();
20+
const { initAuth, initiated, authenticated, login, getAuthUrl } =
21+
useAuthStore();
22+
const { asPath, replace } = useRouter();
2223

2324
const [pathAllowed, setPathAllowed] = useState<boolean>(
2425
!regexpUrlsAuth.some((regexp) => !!asPath.match(regexp)),
@@ -41,6 +42,18 @@ export const Auth = ({ children }: PropsWithChildren) => {
4142
login();
4243
}, [authenticated, pathAllowed, login, initiated]);
4344

45+
// Redirect to the path before login
46+
useEffect(() => {
47+
if (!authenticated) {
48+
return;
49+
}
50+
51+
const authUrl = getAuthUrl();
52+
if (authUrl) {
53+
void replace(authUrl);
54+
}
55+
}, [authenticated, getAuthUrl, replace]);
56+
4457
if ((!initiated && pathAllowed) || (!authenticated && !pathAllowed)) {
4558
return (
4659
<Box $height="100vh" $width="100vw" $align="center" $justify="center">

src/frontend/apps/impress/src/core/auth/useAuthStore.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ interface AuthStore {
1111
initAuth: () => void;
1212
logout: () => void;
1313
login: () => void;
14+
setAuthUrl: (url: string) => void;
15+
getAuthUrl: () => string | undefined;
1416
userData?: User;
1517
}
1618

@@ -20,22 +22,13 @@ const initialState = {
2022
userData: undefined,
2123
};
2224

23-
export const useAuthStore = create<AuthStore>((set) => ({
25+
export const useAuthStore = create<AuthStore>((set, get) => ({
2426
initiated: initialState.initiated,
2527
authenticated: initialState.authenticated,
2628
userData: initialState.userData,
27-
2829
initAuth: () => {
2930
getMe()
3031
.then((data: User) => {
31-
// If a path is stored in the local storage, we redirect to it
32-
const path_auth = localStorage.getItem(PATH_AUTH_LOCAL_STORAGE);
33-
if (path_auth) {
34-
localStorage.removeItem(PATH_AUTH_LOCAL_STORAGE);
35-
window.location.replace(path_auth);
36-
return;
37-
}
38-
3932
set({ authenticated: true, userData: data });
4033
})
4134
.catch(() => {})
@@ -44,15 +37,26 @@ export const useAuthStore = create<AuthStore>((set) => ({
4437
});
4538
},
4639
login: () => {
47-
// If we try to access a specific page and we are not authenticated
48-
// we store the path in the local storage to redirect to it after login
49-
if (window.location.pathname !== '/') {
50-
localStorage.setItem(PATH_AUTH_LOCAL_STORAGE, window.location.pathname);
51-
}
40+
get().setAuthUrl(window.location.pathname);
5241

5342
window.location.replace(`${baseApiUrl()}authenticate/`);
5443
},
5544
logout: () => {
5645
window.location.replace(`${baseApiUrl()}logout/`);
5746
},
47+
// If we try to access a specific page and we are not authenticated
48+
// we store the path in the local storage to redirect to it after login
49+
setAuthUrl() {
50+
if (window.location.pathname !== '/') {
51+
localStorage.setItem(PATH_AUTH_LOCAL_STORAGE, window.location.pathname);
52+
}
53+
},
54+
// If a path is stored in the local storage, we return it then remove it
55+
getAuthUrl() {
56+
const path_auth = localStorage.getItem(PATH_AUTH_LOCAL_STORAGE);
57+
if (path_auth) {
58+
localStorage.removeItem(PATH_AUTH_LOCAL_STORAGE);
59+
return path_auth;
60+
}
61+
},
5862
}));

0 commit comments

Comments
 (0)