Skip to content

Commit fce69d1

Browse files
author
Thom
committed
feat: cookie authentication
1 parent 1b4855f commit fce69d1

File tree

7 files changed

+30
-5
lines changed

7 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This changelog covers all three packages, as they are (for now) updated as a who
66

77
- Add folders with list & grid views, allow drag & drop uploads #228
88
- Show icons in sidebar
9+
- Add cookie based authentication #241
910

1011
## v0.32.1
1112

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
@@ -274,9 +274,9 @@ test.describe('data-browser', async () => {
274274
]);
275275
await fileChooser.setFiles(demoFile);
276276
await page.click(`[data-test]:has-text("${demoFileName}")`);
277-
await expect(
278-
await page.locator('[data-test="image-viewer"]'),
279-
).toBeVisible();
277+
const image = await page.locator('[data-test="image-viewer"]');
278+
await expect(image).toBeVisible();
279+
await expect(image).toHaveScreenshot();
280280
});
281281

282282
test('chatroom', async ({ page, browser }) => {
Loading
Loading

0 commit comments

Comments
 (0)