Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit bc8cfce

Browse files
authored
Merge pull request #6 from clerkinc/post-clean-up
Post clean up
2 parents dbeae2a + 3479959 commit bc8cfce

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

apps/expo/src/_app.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import { StatusBar } from "expo-status-bar";
22
import React from "react";
33
import { SafeAreaProvider } from "react-native-safe-area-context";
4-
import { TRPCAuthContext } from "./utils/trpc";
4+
import { TRPCProvider } from "./utils/trpc";
55

66
import { HomeScreen } from "./screens/home";
77
import { SignInScreen } from "./screens/signin";
88
import { ClerkProvider, SignedIn, SignedOut } from "@clerk/clerk-expo";
99
import { tokenCache } from "./utils/cache";
1010

11-
const clerk_frontend_api = "clerk.open.sawfly-58.lcl.dev";
11+
// Find this in your Dashboard.
12+
const clerk_frontend_api = "YOUR_CLERK_FRONTEND_API";
1213

1314
export const App = () => {
1415
return (
1516
<ClerkProvider frontendApi={clerk_frontend_api} tokenCache={tokenCache}>
1617
<SignedIn>
17-
<TRPCAuthContext>
18+
<TRPCProvider>
1819
<SafeAreaProvider>
1920
<HomeScreen />
2021
<StatusBar />
2122
</SafeAreaProvider>
22-
</TRPCAuthContext>
23+
</TRPCProvider>
2324
</SignedIn>
2425
<SignedOut>
2526
<SignInScreen />

apps/expo/src/utils/trpc.tsx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import Constants from "expo-constants";
99
* A wrapper for your app that provides the TRPC context.
1010
* Use only in _app.tsx
1111
*/
12-
import React, { useEffect } from "react";
12+
import React from "react";
1313
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
1414
import { httpBatchLink } from "@trpc/client";
1515
import { transformer } from "@acme/api/transformer";
1616
import { useAuth } from "@clerk/clerk-expo";
17-
import { Text } from "react-native";
1817

1918
/**
2019
* A set of typesafe hooks for consuming your API.
@@ -35,17 +34,18 @@ const getBaseUrl = () => {
3534

3635
export const TRPCProvider: React.FC<{
3736
children: React.ReactNode;
38-
authToken: string | null;
39-
}> = ({ children, authToken }) => {
37+
}> = ({ children }) => {
38+
const { getToken } = useAuth();
4039
const [queryClient] = React.useState(() => new QueryClient());
4140
const [trpcClient] = React.useState(() =>
4241
trpc.createClient({
4342
transformer,
4443
links: [
4544
httpBatchLink({
46-
headers() {
45+
async headers() {
46+
const authToken = await getToken();
4747
return {
48-
Authorization: authToken || "",
48+
Authorization: authToken,
4949
};
5050
},
5151
url: `${getBaseUrl()}/api/trpc`,
@@ -60,19 +60,3 @@ export const TRPCProvider: React.FC<{
6060
</trpc.Provider>
6161
);
6262
};
63-
64-
export const TRPCAuthContext: React.FC<{ children: React.ReactNode }> = ({
65-
children,
66-
}) => {
67-
const { getToken } = useAuth();
68-
const [authToken, setAuthToken] = React.useState<string | null>(null);
69-
useEffect(() => {
70-
getToken().then((token) => {
71-
setAuthToken(token);
72-
});
73-
}, []);
74-
75-
if (!authToken) return <Text>Loading</Text>;
76-
77-
return <TRPCProvider authToken={authToken}>{children}</TRPCProvider>;
78-
};

packages/api/src/router/post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { router, publicProcedure } from "../trpc";
1+
import { router, publicProcedure, protectedProcedure } from "../trpc";
22
import { z } from "zod";
33

44
export const postRouter = router({
@@ -8,7 +8,7 @@ export const postRouter = router({
88
byId: publicProcedure.input(z.string()).query(({ ctx, input }) => {
99
return ctx.prisma.post.findFirst({ where: { id: input } });
1010
}),
11-
create: publicProcedure
11+
create: protectedProcedure
1212
.input(z.object({ title: z.string(), content: z.string() }))
1313
.mutation(({ ctx, input }) => {
1414
return ctx.prisma.post.create({ data: input });

0 commit comments

Comments
 (0)