Skip to content

Commit

Permalink
feat: Show device labels in devtools
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Mar 28, 2023
1 parent 4dcc0ea commit 3292923
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The only external requirement is a PostgreSQL database.

## Documentation

- [Cryptography](./docs/cryptography)
- [Cryptography](./docs/docs/cryptography)

## Contributing

Expand Down
23 changes: 22 additions & 1 deletion packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,30 @@ export class Client {
}

public async getEnrolledDevices() {
return listDevicesResponseBody.parse(
await this.sodium.ready
if (this.#state.state !== 'loaded') {
throw new Error('Account locked: cannot list enrolled devices')
}
const devices = listDevicesResponseBody.parse(
await this.#apiCall('GET', '/v1/auth/devices')
)
const deviceLabelCipher = getDeviceLabelCipher(
this.sodium,
this.#state.identity.userId,
this.#state.identity.keychainBaseKey
)
try {
return devices.map(device => ({
...device,
label: device.label
? z
.string()
.parse(decrypt(this.sodium, device.label, deviceLabelCipher))
: undefined,
}))
} finally {
this.sodium.memzero(deviceLabelCipher.key)
}
}

// Key Ops --
Expand Down
39 changes: 29 additions & 10 deletions packages/devtools/src/tabs/DevicesTab.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Badge, BoxProps, Button, Flex, Stack, Text } from '@chakra-ui/react'
import {
Badge,
Box,
BoxProps,
Button,
Flex,
Stack,
Text,
} from '@chakra-ui/react'
import {
useE2ESDKClient,
useE2ESDKClientIdentity,
Expand Down Expand Up @@ -40,16 +48,27 @@ const YourDevicesSection = (props: BoxProps) => {
<Stack spacing={4} px={4}>
{devices.map(device => (
<Flex key={device.id} alignItems="baseline">
<Text fontFamily="mono" fontSize="xs">
{device.id}
</Text>
{client.currentDeviceId === device.id && (
<Badge fontSize="2xs" colorScheme="green" ml={2}>
Current
</Badge>
)}
<Box>
<Text fontWeight="medium" fontSize="xs">
{device.label ?? 'Unnamed device'}
</Text>
<Text fontFamily="mono" fontSize="xs" color="gray.500">
{device.id}
{client.currentDeviceId === device.id && (
<Badge
fontSize="2xs"
colorScheme="green"
ml={2}
fontFamily="body"
>
Current
</Badge>
)}
</Text>
</Box>
<Text ml="auto" fontSize="xs" color="gray.500">
{device.sessions.length} session(s)
{device.sessions.length || 'no'} session
{device.sessions.length > 1 ? 's' : ''}
</Text>
</Flex>
))}
Expand Down

0 comments on commit 3292923

Please sign in to comment.