Skip to content

Commit

Permalink
[UI] EVEREST-1190 Display logged in user (#1067)
Browse files Browse the repository at this point in the history
* feat: display logged in user

* fix: sub suffix handling

* test: mock jwtDecode

---------

Co-authored-by: Percona Platform Robot <[email protected]>
  • Loading branch information
dianabirs and percona-robot authored Feb 5, 2025
1 parent 40950be commit 3491683
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ui/apps/everest/src/components/app-bar/user-icon/UserIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { useContext, useState } from 'react';
import {
Divider,
FormControlLabel,
FormGroup,
IconButton,
Menu,
MenuItem,
Switch,
Typography,
} from '@mui/material';
import PersonOutlineIcon from '@mui/icons-material/PersonOutline';
import { ColorModeContext } from '@percona/design';
import { AuthContext } from 'contexts/auth';
import { jwtDecode } from 'jwt-decode';

interface UserToken {
preferred_username?: string;
name?: string;
email?: string;
sub: string;
}

const AppBarUserIcon = () => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
Expand All @@ -23,6 +33,16 @@ const AppBarUserIcon = () => {
const handleClose = () => {
setAnchorEl(null);
};
const token = localStorage.getItem('everestToken');
const decoded = jwtDecode(token!) as UserToken;

const preferredUsername = decoded.preferred_username;
const name = decoded.name;
const email = decoded.email;
const sub =
decoded.sub?.substring(0, decoded.sub.indexOf(':')) || decoded.sub;

const userToShow = preferredUsername || name || email || sub;

return (
<>
Expand All @@ -44,6 +64,12 @@ const AppBarUserIcon = () => {
open={Boolean(anchorEl)}
onClose={handleClose}
>
<MenuItem sx={{ cursor: 'text', userSelect: 'text' }}>
<Typography variant="helperText" color="text.secondary">
{userToShow}
</Typography>
</MenuItem>
<Divider />
<MenuItem>
<FormGroup>
<FormControlLabel
Expand Down
4 changes: 4 additions & 0 deletions ui/apps/everest/src/components/drawer/Drawer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { DrawerContextProvider } from 'contexts/drawer/drawer.context';
const queryClient = new QueryClient();
vi.mock('hooks/api/version/useVersion');

vi.mock('jwt-decode', () => ({
jwtDecode: vi.fn(() => ({ sub: '12345', name: 'Test User' })),
}));

const BarAndDrawer = () => (
<QueryClientProvider client={queryClient}>
<DrawerContextProvider>
Expand Down

0 comments on commit 3491683

Please sign in to comment.