Skip to content

Commit c5037e7

Browse files
authored
feat: add user profile example (#4)
1 parent f1e7204 commit c5037e7

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

sdk-explorer/src/App.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { SanityMonogram } from '@sanity/logos'
22
import { AuthBoundary } from '@sanity/sdk-react/components'
33
import { useAuthState, useLogOut } from '@sanity/sdk-react/hooks'
4-
import { Button, Card, Flex, Text } from '@sanity/ui'
4+
import { Button, Card, Flex, Inline, Text } from '@sanity/ui'
55
import { BrowserRouter, Link, Route, Routes } from 'react-router'
66
import PreviewGrid from './document-collections/PreviewGrid/PreviewGrid'
77
import PreviewList from './document-collections/PreviewList/PreviewList'
88
import Home from './Home'
9+
import UserProfile from './users/UserProfile/UserProfile'
910

1011
export default function App() {
1112
const authState = useAuthState()
@@ -39,9 +40,16 @@ export default function App() {
3940
</Flex>
4041
</Link>
4142
</Text>
42-
{authState?.type === 'logged-in' && (
43-
<Button mode='ghost' text='Log out' onClick={logOut} />
44-
)}
43+
<Inline space={4}>
44+
<Link to='/'>
45+
<Text weight='medium' size={1}>
46+
Home
47+
</Text>
48+
</Link>
49+
{authState?.type === 'logged-in' && (
50+
<Button mode='ghost' text='Log out' onClick={logOut} />
51+
)}
52+
</Inline>
4553
</Flex>
4654
</Card>
4755
<AuthBoundary>
@@ -55,6 +63,7 @@ export default function App() {
5563
path='/document-collections/preview-grid'
5664
element={<PreviewGrid />}
5765
/>
66+
<Route path='/users/user-profile' element={<UserProfile />} />
5867
</Routes>
5968
</AuthBoundary>
6069
</Card>

sdk-explorer/src/Home.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ export default function Home() {
4747
</Stack>
4848
</Stack>
4949
</Card>
50+
51+
<Card padding={4} radius={2} shadow={3}>
52+
<Stack space={4}>
53+
<Text as='h2' size={4} weight='medium'>
54+
Users
55+
</Text>
56+
57+
<Stack space={3}>
58+
<ExampleCard
59+
to='/users/user-profile'
60+
title='User profile'
61+
description='Information about the currently authenticated user'
62+
hooks={['useAuthState', 'useCurrentUser']}
63+
styling='Sanity UI'
64+
/>
65+
</Stack>
66+
</Stack>
67+
</Card>
5068
</Stack>
5169
</Container>
5270
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { AuthStateType } from '@sanity/sdk'
2+
import { useAuthState, useCurrentUser } from '@sanity/sdk-react/hooks'
3+
import { Avatar, Card, Container, Flex, Grid, Stack, Text } from '@sanity/ui'
4+
5+
export default function UserProfile() {
6+
const { type } = useAuthState()
7+
const user = useCurrentUser()
8+
9+
return type === AuthStateType.LOGGED_IN && user ? (
10+
<Container width={1}>
11+
<Card padding={4} margin={2} radius={3} shadow={3}>
12+
<Flex justify='center' marginBottom={5}>
13+
<Avatar src={user?.profileImage} size={3} />
14+
</Flex>
15+
<Text as='h2' size={4} weight='medium' align='center'>
16+
{user?.name}
17+
</Text>
18+
<Stack space={3} marginTop={5} marginBottom={4}>
19+
<Grid columns={2} gap={4} as='dl'>
20+
<Text as='dt' align='right' muted>
21+
Email address
22+
</Text>
23+
<Text as='dd'>{user?.email}</Text>
24+
<Text as='dt' align='right' muted>
25+
Roles
26+
</Text>
27+
<Text as='dd'>
28+
{user?.roles.map((role) => role.title).join(', ')}
29+
</Text>
30+
<Text as='dt' align='right' muted>
31+
Provider
32+
</Text>
33+
<Text as='dd'>{user?.provider}</Text>
34+
</Grid>
35+
</Stack>
36+
</Card>
37+
</Container>
38+
) : (
39+
<Container padding={5}>
40+
<Text as='h2' size={4} align='center'>
41+
Nobody home! Try logging in?
42+
</Text>
43+
</Container>
44+
)
45+
}

0 commit comments

Comments
 (0)