Skip to content

Commit 9f23642

Browse files
Thomjoepio
authored andcommitted
#241 feat: cookie authentication
1 parent 3c85009 commit 9f23642

File tree

10 files changed

+60
-8
lines changed

10 files changed

+60
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This changelog covers all three packages, as they are (for now) updated as a who
77
- Add folders with list & grid views, allow drag & drop uploads #228
88
- Show icons in sidebar
99
- Add scoped search, funded by NGI NLnet Discovery #245
10+
- Add cookie based authentication #241
1011

1112
## v0.32.1
1213

data-browser/src/App.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
urls,
99
} from '@tomic/react';
1010

11+
import { setCookieAuthentication } from './helpers/cookieAuthentication';
1112
import { GlobalStyle, ThemeWrapper } from './styling';
1213
import { AppRoutes } from './routes/Routes';
1314
import { NavWrapper } from './components/Navigation';
@@ -59,7 +60,11 @@ const ErrBoundary = window.bugsnagApiKey
5960

6061
/** Initialize the agent from localstorage */
6162
const agent = initAgentFromLocalStorage();
62-
agent && store.setAgent(agent);
63+
64+
if (agent) {
65+
store.setAgent(agent);
66+
setCookieAuthentication(store, agent);
67+
}
6368

6469
/** Fetch all the Properties and Classes - this helps speed up the app. */
6570
store.fetchResource(urls.properties.getAll);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createAuthentication, Agent, Store } from '@tomic/react';
2+
3+
const ONE_HOUR = 60 * 60 * 1000;
4+
5+
export const setCookie = (name: string, value: string) => {
6+
const expiry = new Date(Date.now() + ONE_HOUR).toISOString();
7+
const encodedValue = encodeURIComponent(value);
8+
9+
document.cookie = `${name}=${encodedValue};expires=${expiry}; path=/`;
10+
};
11+
12+
export const setCookieAuthentication = (store: Store, agent: Agent) => {
13+
createAuthentication(store.getServerUrl(), agent).then(auth => {
14+
setCookie('atomic_session', btoa(JSON.stringify(auth)));
15+
});
16+
};

data-browser/src/routes/SettingsAgent.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { useState } from 'react';
3-
import { Agent } from '@tomic/react';
3+
import { Agent, useStore } from '@tomic/react';
44
import { FaCog, FaEye, FaEyeSlash, FaUser } from 'react-icons/fa';
55

66
import { useSettings } from '../helpers/AppSettings';
@@ -12,6 +12,7 @@ import {
1212
import { ButtonInput, Button } from '../components/Button';
1313
import { Margin } from '../components/Card';
1414
import Field from '../components/forms/Field';
15+
import { setCookieAuthentication } from '../helpers/cookieAuthentication';
1516
import { ResourceInline } from '../views/ResourceInline';
1617
import { ContainerNarrow } from '../components/Containers';
1718
import { AtomicLink } from '../components/AtomicLink';
@@ -28,6 +29,7 @@ const SettingsAgent: React.FunctionComponent = () => {
2829
const [advanced, setAdvanced] = useState(false);
2930
const [secret, setSecret] = useState<string | undefined>(undefined);
3031
const navigate = useNavigate();
32+
const store = useStore();
3133

3234
// When there is an agent, set the advanced values
3335
// Otherwise, reset the secret value
@@ -81,6 +83,7 @@ const SettingsAgent: React.FunctionComponent = () => {
8183
function setAgentIfChanged(oldAgent: Agent | undefined, newAgent: Agent) {
8284
if (JSON.stringify(oldAgent) !== JSON.stringify(newAgent)) {
8385
setAgent(newAgent);
86+
setCookieAuthentication(store, newAgent);
8487
}
8588
}
8689

data-browser/tests/e2e.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ test.describe('data-browser', async () => {
330330
]);
331331
await fileChooser.setFiles(demoFile);
332332
await page.click(`[data-test]:has-text("${demoFileName}")`);
333-
await expect(
334-
await page.locator('[data-test="image-viewer"]'),
335-
).toBeVisible();
333+
const image = await page.locator('[data-test="image-viewer"]');
334+
await expect(image).toBeVisible();
335+
await expect(image).toHaveScreenshot();
336336
});
337337

338338
test('chatroom', async ({ page, browser }) => {
6.92 KB
Loading
6.92 KB
Loading

data-browser/tests/test-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { TestConfig } from './e2e.spec';
2-
const demoFileName = 'logo.svg';
2+
const demoFileName = 'testimage.svg';
33

44
export const testConfig: TestConfig = {
55
demoFileName,
6-
demoFile: `./${demoFileName}`,
6+
demoFile: `./tests/${demoFileName}`,
77
demoInviteName: 'document demo',
88
serverUrl: process.env.SERVER_URL || 'http://localhost:9883',
99
frontEndUrl: 'http://localhost:5173',

data-browser/tests/testimage.svg

Lines changed: 27 additions & 0 deletions
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"lint": "pnpm run -r lint",
3838
"lint-fix": "pnpm run -r lint-fix",
3939
"build": "pnpm run -r build",
40-
"build-server": "pnpm run build && cp -r data-browser/dist/assets/ ../atomic-data-rust/server/app_assets/dist/ && cp data-browser/tests/e2e.spec.ts ../atomic-data-rust/server/e2e_tests/e2e-generated.spec.ts",
40+
"build-server": "pnpm run build && cp -r data-browser/dist/assets/ ../atomic-data-rust/server/app_assets/dist/ && cp data-browser/tests/e2e.spec.ts ../atomic-data-rust/server/e2e_tests/e2e-generated.spec.ts && cp data-browser/tests/testimage.svg ../atomic-data-rust/server/e2e_tests/testimage.svg && cp -r data-browser/tests/e2e.spec.ts-snapshots/ ../atomic-data-rust/server/e2e_tests/e2e-generated.spec.ts-snapshots/",
4141
"test": "pnpm run -r test",
4242
"test-query": "pnpm run --filter @tomic/data-browser test-query",
4343
"start": "pnpm run -r --parallel start",

0 commit comments

Comments
 (0)