Skip to content

Commit 63f8570

Browse files
authored
Merge pull request #21 from microsoft/u/bsap/demo/showUserName
Demo App: store and use user name to lookup consents
2 parents d7a0e50 + c4a05a9 commit 63f8570

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

packages/demo/src/components/GetStarted.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export default function GetStarted(): JSX.Element {
243243
.trim()
244244
.toLowerCase()
245245
.replace(/\s+/g, '.');
246-
await login(subjectId);
246+
await login({ id: subjectId, name: formData.name });
247247
} catch (err) {
248248
console.error('Login failed:', err);
249249
alert('Unable to login. Please contact support.');

packages/demo/src/components/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export default function Home(): JSX.Element {
163163
size="large"
164164
onClick={() => {
165165
if (currentUser) {
166-
void navigate(`/profile/${currentUser.id}`);
166+
void navigate(`/profile/${currentUser.name}`);
167167
} else {
168168
void navigate('/get-started');
169169
}

packages/demo/src/utils/AuthContext.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type ReactNode,
1010
} from 'react';
1111
import type { ProfileData } from '@open-source-consent/ui';
12+
import type { BasicUserInfo } from './userManagement.js';
1213
import {
1314
getCurrentUserId,
1415
login as storeLogin,
@@ -19,7 +20,7 @@ import {
1920
interface AuthContextType {
2021
currentUser: ProfileData | null;
2122
isLoading: boolean;
22-
login(subjectId: string): Promise<void>;
23+
login(userInfo: BasicUserInfo): Promise<void>;
2324
logout(): void;
2425
}
2526

@@ -81,10 +82,10 @@ export default function AuthProvider({
8182
}, [loadProfile]);
8283

8384
const login = useCallback(
84-
async (subjectId: string) => {
85+
async (userInfo: BasicUserInfo) => {
8586
setIsLoading(true);
86-
storeLogin(subjectId);
87-
await loadProfile(subjectId);
87+
storeLogin(userInfo);
88+
await loadProfile(userInfo.id);
8889
},
8990
[loadProfile],
9091
);

packages/demo/src/utils/userManagement.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,38 @@ import type { ProfileData } from '@open-source-consent/ui';
88
* This framework does not provide auth or user management.
99
*/
1010

11+
export type BasicUserInfo = {
12+
id: string;
13+
name: string;
14+
};
15+
1116
const USER_ID_STORAGE_KEY = 'currentUserId';
17+
const USER_NAME_STORAGE_KEY = 'currentUserName';
18+
19+
function getCurrentUserName(): string | null {
20+
return localStorage.getItem(USER_NAME_STORAGE_KEY);
21+
}
1222

1323
export function getCurrentUserId(): string | null {
1424
return localStorage.getItem(USER_ID_STORAGE_KEY);
1525
}
1626

17-
export function login(userId: string): void {
18-
localStorage.setItem(USER_ID_STORAGE_KEY, userId);
27+
export function login(user: BasicUserInfo): void {
28+
localStorage.setItem(USER_ID_STORAGE_KEY, user.id);
29+
localStorage.setItem(USER_NAME_STORAGE_KEY, user.name);
1930
}
2031

2132
export function logout(): void {
2233
localStorage.removeItem(USER_ID_STORAGE_KEY);
34+
localStorage.removeItem(USER_NAME_STORAGE_KEY);
2335
}
2436

2537
export async function fetchUserProfile(
2638
userId: string,
2739
): Promise<ProfileData | null> {
2840
return {
2941
id: userId,
30-
name: `${userId}`,
42+
name: getCurrentUserName() || `${userId}`,
3143
email: `${userId.replace(/[^a-zA-Z0-9]/g, '-')}@example.com`,
3244
role: { id: 'self', label: 'Myself' },
3345
consents: [], // These are fetched from the API

0 commit comments

Comments
 (0)