Skip to content

Commit d8c10f6

Browse files
committed
client auth admin
1 parent 0471ffd commit d8c10f6

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

src/components/ClientAuth.tsx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const baseClasses = 'p-4 flex flex-col items-center justify-center gap-4'
1111
export function ClientAuth({
1212
children,
1313
}: {
14-
children: React.ReactNode | (() => React.ReactNode)
14+
children:
15+
| React.ReactNode
16+
| ((userQuery: UseQueryResult<any>) => React.ReactNode)
1517
}) {
16-
const userQuery = useQuery(convexQuery(api.auth.getCurrentUser, {}))
17-
1818
return (
19-
<AuthLoadingOrError userQuery={userQuery}>
20-
{() => {
19+
<UserQuery>
20+
{(userQuery) => {
2121
if (!userQuery.data) {
2222
return (
2323
<div className={baseClasses}>
@@ -27,18 +27,16 @@ export function ClientAuth({
2727
)
2828
}
2929

30-
return typeof children === 'function' ? children() : children
30+
return typeof children === 'function' ? children(userQuery) : children
3131
}}
32-
</AuthLoadingOrError>
32+
</UserQuery>
3333
)
3434
}
3535

3636
export function ClientAdminAuth({ children }: { children: React.ReactNode }) {
37-
const userQuery = useQuery(convexQuery(api.auth.getCurrentUser, {}))
38-
3937
return (
4038
<ClientAuth>
41-
{() => {
39+
{(userQuery) => {
4240
const canAdmin = userQuery.data?.capabilities.includes('admin')
4341

4442
if (!canAdmin) {
@@ -56,27 +54,25 @@ export function ClientAdminAuth({ children }: { children: React.ReactNode }) {
5654
)
5755
}
5856

59-
function AuthLoadingOrError(props: {
60-
userQuery: UseQueryResult<any>
61-
children: React.ReactNode | (() => React.ReactNode)
57+
function UserQuery(props: {
58+
children: (userQuery: UseQueryResult<any>) => React.ReactNode
6259
}) {
63-
if (props.userQuery.isLoading) {
60+
const userQuery = useQuery(convexQuery(api.auth.getCurrentUser, {}))
61+
if (userQuery.isLoading) {
6462
return (
6563
<div className={baseClasses}>
6664
<FaSpinner className="animate-spin" />
6765
</div>
6866
)
6967
}
7068

71-
if (props.userQuery.isError) {
69+
if (userQuery.isError) {
7270
return (
7371
<div className={baseClasses}>
74-
<ErrorComponent error={props.userQuery.error} />
72+
<ErrorComponent error={userQuery.error} />
7573
</div>
7674
)
7775
}
7876

79-
return typeof props.children === 'function'
80-
? props.children()
81-
: props.children
77+
return props.children(userQuery)
8278
}

0 commit comments

Comments
 (0)