Skip to content

Commit 0bf54ab

Browse files
committed
user-authentification, hook
1 parent 2ec9a90 commit 0bf54ab

7 files changed

Lines changed: 236 additions & 15 deletions

File tree

‎modules/dcache-view-2/package-lock.json‎

Lines changed: 162 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎modules/dcache-view-2/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
},
1818
"devDependencies": {
1919
"@eslint/js": "^10.0.1",
20+
"@testing-library/react": "^16.3.3",
2021
"@types/node": "^24.13.3",
21-
"@types/react": "^19.2.18",
22+
"@types/react": "^19.3.0",
2223
"@types/react-dom": "^19.2.7",
2324
"@vitejs/plugin-react": "^6.1.1",
2425
"eslint": "^10.10.0",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useState, useEffect } from 'react';
2+
import type { User } from '../workers/types.ts';
3+
4+
export function useAuth(): { user: User | null, error: Error | null } {
5+
const [user, setUser] = useState<User | null>(null);
6+
const [error, setError] = useState<Error |null>(null);
7+
8+
useEffect(() => {
9+
const worker = new Worker(
10+
new URL('../workers/user-authentification.ts', import.meta.url),
11+
{type: 'module'}
12+
);
13+
14+
worker.onmessage = (e: MessageEvent<User>) => {
15+
setUser(e.data);
16+
worker.terminate();
17+
};
18+
19+
worker.onerror = (e) => {
20+
console.warn(e);
21+
setError(new Error(e.message))
22+
worker.terminate();
23+
};
24+
return () => worker.terminate();
25+
}, []);
26+
27+
return { user, error };
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { test, expect, vi } from 'vitest';
2+
import { renderHook, waitFor } from '@testing-library/react';
3+
import { useAuth } from '../../hooks/useAuth';
4+
import type { User } from "../../workers/types.ts";
5+
6+
const authenticatedUser: User = {
7+
status: "AUTHENTICATED",
8+
uid: null,
9+
gids: null,
10+
roles: null,
11+
unassertedRoles: null,
12+
home: null,
13+
root: null,
14+
username: null,
15+
email: null,
16+
}
17+
18+
vi.stubGlobal('Worker', class {
19+
onmessage: ((e: MessageEvent) => void) | null = null;
20+
constructor() {
21+
setTimeout(() => {
22+
this.onmessage?.({ data: authenticatedUser } as MessageEvent);
23+
}, 0);
24+
}
25+
terminate() {}
26+
});
27+
28+
test('returns user when authenticated', async () => {
29+
const { result } = renderHook(() => useAuth());
30+
31+
expect(result.current?.user).toBeNull(); // initial state
32+
33+
await waitFor(() => {
34+
expect(result.current?.user?.status).toBe('AUTHENTICATED');
35+
});
36+
});

‎modules/dcache-view-2/src/test/workers/authentification.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, test, expect, vi, beforeEach } from 'vitest';
2-
import { getUser, sendUser } from '../../workers/authentification';
3-
import type { User } from '../../workers/types';
2+
import { getUser, sendUser } from '../../workers/user-authentification.ts';
3+
import type { User } from '../../workers/types.ts';
44

55
const authenticatedUser: User = {
66
status: 'AUTHENTICATED',

‎modules/dcache-view-2/src/workers/download.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { WorkerMessageData, WorkerResponse } from "./types";
1+
import type { WorkerMessageData, WorkerResponse } from "./types.ts";
22

33
export function buildRequest(e: MessageEvent<WorkerMessageData>): Request {
44
const headers = new Headers({
@@ -34,10 +34,8 @@ export async function processResponse(response: Response, e: MessageEvent<Worker
3434
}
3535
}
3636

37-
self.addEventListener('message', (e: MessageEvent<WorkerMessageData>) => {
38-
37+
self.onmessage = (e: MessageEvent<WorkerMessageData>) => {
3938
fetch(buildRequest(e))
4039
.then(response => processResponse(response, e))
4140
.then(data => self.postMessage(data));
42-
43-
}, false);
41+
};

modules/dcache-view-2/src/workers/authentification.ts renamed to modules/dcache-view-2/src/workers/user-authentification.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import type { User } from './types.ts'
22

33
export async function getUser(response: Response): Promise<User> {
4-
console.log("IS USER RESOURCE STILL CALLED ");
54
if (response.status !== 200) {
65
throw new Error(`Looks like there was a problem. Status Code: ${response.status}`);
76
}
87
return response.json();
98
}
109

1110
export function sendUser(user: User) {
12-
console.log("AUTHENTICATED user resource: ");
1311
if (user.status === "AUTHENTICATED") {
1412
self.postMessage(user);
1513
}
@@ -27,4 +25,6 @@ export function authenticate() {
2725
fetch('/api/v1/user', init)
2826
.then(response => getUser(response))
2927
.then(user => sendUser(user));
30-
}
28+
}
29+
30+
authenticate()

0 commit comments

Comments
 (0)