🛂(frontend) redirect to correct url after login

If a user wanted to access a doc but was not logged in,
they would be redirected to the login page.
After logging in, they would be redirected to the home page.
This change makes it so that they are
redirected to the doc they originally wanted to access.

Usefull from the mail sent to the user to access the doc
they were invited to.
This commit is contained in:
Anthony LC
2024-08-16 14:51:42 +02:00
committed by Anthony LC
parent a925b0bedf
commit 0512af273c
9 changed files with 58 additions and 52 deletions

View File

@@ -147,7 +147,7 @@ export const mockedDocument = async (page: Page, json: object) => {
if (request.method().includes('GET') && !request.url().includes('page=')) {
await route.fulfill({
json: {
id: 'b0df4343-c8bd-4c20-9ff6-fbf94fc94egg',
id: 'mocked-document-id',
content: '',
title: 'Mocked document',
accesses: [],

View File

@@ -7,19 +7,6 @@ test.beforeEach(async ({ page }) => {
});
test.describe('Doc Editor', () => {
test('checks the Doc Editor interact correctly', async ({
page,
browserName,
}) => {
const randomDoc = await createDoc(page, 'doc-editor', browserName, 1);
await expect(page.locator('h2').getByText(randomDoc[0])).toBeVisible();
await page.locator('.ProseMirror.bn-editor').click();
await page.locator('.ProseMirror.bn-editor').fill('Hello World');
await expect(page.getByText('Hello World')).toBeVisible();
});
test('checks the Doc is connected to the webrtc server', async ({
page,
browserName,

View File

@@ -122,22 +122,14 @@ test.describe('Doc Export', () => {
// Add a list
await page.locator('.bn-block-outer').last().click();
await page.keyboard.press('Enter');
await page.locator('.bn-block-outer').last().fill('Test List 1');
await page.getByText('Test List 1').dblclick();
await page.locator('.bn-block-outer').last().fill('/');
await page.getByText('Bullet List').click();
await page
.getByRole('button', {
name: 'Paragraph',
})
.click();
await page
.getByRole('menuitem', {
name: 'Bullet List',
})
.click();
await page
.locator('.bn-block-content[data-content-type="bulletListItem"]')
.locator('.bn-block-content[data-content-type="bulletListItem"] p')
.last()
.click();
.fill('Test List 1');
// eslint-disable-next-line playwright/no-wait-for-timeout
await page.waitForTimeout(300);
await page.keyboard.press('Enter');
await page
.locator('.bn-block-content[data-content-type="bulletListItem"] p')
@@ -155,22 +147,14 @@ test.describe('Doc Export', () => {
// Add a number list
await page.locator('.bn-block-outer').last().click();
await page.keyboard.press('Enter');
await page.locator('.bn-block-outer').last().fill('Test Number 1');
await page.getByText('Test Number 1').dblclick();
await page.locator('.bn-block-outer').last().fill('/');
await page.getByText('Numbered List').click();
await page
.getByRole('button', {
name: 'Paragraph',
})
.click();
await page
.getByRole('menuitem', {
name: 'Numbered List',
})
.click();
await page
.locator('.bn-block-content[data-content-type="numberedListItem"]')
.locator('.bn-block-content[data-content-type="numberedListItem"] p')
.last()
.click();
.fill('Test Number 1');
// eslint-disable-next-line playwright/no-wait-for-timeout
await page.waitForTimeout(300);
await page.keyboard.press('Enter');
await page
.locator('.bn-block-content[data-content-type="numberedListItem"] p')

View File

@@ -1,10 +1,12 @@
import { expect, test } from '@playwright/test';
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
import { keyCloakSignIn } from './common';
test.describe('Doc Routing', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
test('checks alias docs url with homepage', async ({ page }) => {
await expect(page).toHaveURL('/');
@@ -14,9 +16,9 @@ test.describe('Doc Routing', () => {
await expect(buttonCreateHomepage).toBeVisible();
await page.goto('/docs');
await page.goto('/docs/');
await expect(buttonCreateHomepage).toBeVisible();
await expect(page).toHaveURL(/\/docs$/);
await expect(page).toHaveURL(/\/docs\/$/);
});
test('checks 404 on docs/[id] page', async ({ page }) => {
@@ -33,3 +35,16 @@ test.describe('Doc Routing', () => {
});
});
});
test.describe('Doc Routing: Not loggued', () => {
test.use({ storageState: { cookies: [], origins: [] } });
test('checks redirect to a doc after login', async ({
page,
browserName,
}) => {
await page.goto('/docs/mocked-document-id/');
await keyCloakSignIn(page, browserName);
await expect(page).toHaveURL(/\/docs\/mocked-document-id\/$/);
});
});

View File

@@ -2,11 +2,11 @@ import { expect, test } from '@playwright/test';
import { keyCloakSignIn } from './common';
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
test.describe('Header', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
test('checks all the elements are visible', async ({ page }) => {
const header = page.locator('header').first();

View File

@@ -4,6 +4,7 @@ import { PropsWithChildren, useEffect } from 'react';
import { Box } from '@/components';
import { useAuthStore } from './useAuthStore';
export const Auth = ({ children }: PropsWithChildren) => {
const { authenticated, initAuth } = useAuthStore();

View File

@@ -0,0 +1 @@
export const PATH_AUTH_LOCAL_STORAGE = 'docs-path-auth';

View File

@@ -3,6 +3,7 @@ import { create } from 'zustand';
import { baseApiUrl } from '@/core/conf';
import { User, getMe } from './api';
import { PATH_AUTH_LOCAL_STORAGE } from './conf';
interface AuthStore {
authenticated: boolean;
@@ -23,9 +24,26 @@ export const useAuthStore = create<AuthStore>((set) => ({
initAuth: () => {
getMe()
.then((data: User) => {
// If a path is stored in the local storage, we redirect to it
const path_auth = localStorage.getItem(PATH_AUTH_LOCAL_STORAGE);
if (path_auth) {
localStorage.removeItem(PATH_AUTH_LOCAL_STORAGE);
window.location.replace(path_auth);
return;
}
set({ authenticated: true, userData: data });
})
.catch(() => {
// If we try to access a specific page and we are not authenticated
// we store the path in the local storage to redirect to it after login
if (window.location.pathname !== '/') {
localStorage.setItem(
PATH_AUTH_LOCAL_STORAGE,
window.location.pathname,
);
}
window.location.replace(new URL('authenticate/', baseApiUrl()).href);
});
},