Skip to content

Commit 06739a9

Browse files
committed
feat: use prpc query to fetch logged in user
1 parent c1b8bac commit 06739a9

File tree

5 files changed

+70
-53
lines changed

5 files changed

+70
-53
lines changed

.changeset/moody-hairs-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-jd-app": patch
3+
---
4+
5+
feat: use prpc query to fetch logged in user

src/helpers/packages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const packages = {
1313
// prisma
1414
"@prisma/client": "^4.10.1",
1515
// prpc
16-
"@prpc/solid": "^0.2.23",
16+
"@prpc/solid": "^0.2.24",
1717
"@prpc/vite": "^0.3.14",
1818
// trpc
1919
"@tanstack/solid-query": "^5.0.0-alpha.16",

src/installers/pRPC/utils/getQueries.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import { z } from "zod";${
1414
: "rateLimitMW"
1515
} } from "./middleware";`
1616
: ""
17+
}${
18+
useAuth
19+
? `\nimport { getSession } from "@solid-auth/base";
20+
import { authOptions } from "../auth";`
21+
: ""
1722
}
1823
1924
export const helloQuery = query$({
@@ -30,6 +35,15 @@ export const helloQuery = query$({
3035
},
3136
key: "protected-1",
3237
middlewares: [authMw],
38+
});
39+
40+
export const meQuery = query$({
41+
queryFn: async ({ request$ }) => {
42+
return {
43+
info: await getSession(request$, authOptions),
44+
};
45+
},
46+
key: "me",
3347
});`
3448
: ""
3549
}${

template/index/with-auth-prpc-tw.tsx

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { type VoidComponent } from "solid-js";
1+
import { Show, type VoidComponent } from "solid-js";
22
import { A } from "solid-start";
3-
import { helloQuery } from "rpc/queries";
4-
import { createSession, signOut, signIn } from "@solid-auth/base/client";
3+
import { helloQuery, meQuery } from "rpc/queries";
4+
import { signOut, signIn } from "@solid-auth/base/client";
55

66
const Home: VoidComponent = () => {
77
const hello = helloQuery(() => ({
@@ -50,29 +50,28 @@ const Home: VoidComponent = () => {
5050
export default Home;
5151

5252
const AuthShowcase: VoidComponent = () => {
53-
const sessionData = createSession();
53+
const session = meQuery();
5454
return (
55-
<div class="flex flex-col items-center justify-center gap-4">
56-
<p class="text-center text-2xl text-white">
57-
{sessionData().status === "authenticated" && (
58-
<span>
59-
Logged in as{" "}
60-
{sessionData().data?.user?.name ?? sessionData().data?.user?.email}
61-
</span>
62-
)}
63-
</p>
64-
<button
65-
class="rounded-full bg-white/10 px-10 py-3 font-semibold text-white no-underline transition hover:bg-white/20"
66-
onClick={() => {
67-
if (sessionData().status === "authenticated") {
68-
void signOut();
69-
} else {
70-
void signIn("github");
71-
}
72-
}}
73-
>
74-
{sessionData().status === "authenticated" ? "Sign out" : "Sign in"}
75-
</button>
76-
</div>
55+
<Show when={session.data}>
56+
<div class="flex flex-col items-center justify-center gap-4">
57+
<p class="text-center text-2xl text-white">
58+
{session.data?.info && (
59+
<span>Logged in as {session.data.info?.user?.name}</span>
60+
)}
61+
</p>
62+
<button
63+
class="rounded-full bg-white/10 px-10 py-3 font-semibold text-white no-underline transition hover:bg-white/20"
64+
onClick={() => {
65+
if (session.data?.info) {
66+
void signOut().then(() => session.refetch());
67+
} else {
68+
void signIn("github");
69+
}
70+
}}
71+
>
72+
{session.data?.info ? "Sign out" : "Sign in"}
73+
</button>
74+
</div>
75+
</Show>
7776
);
7877
};

template/index/with-auth-prpc.tsx

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import styles from "./index.module.css";
2-
import { type VoidComponent } from "solid-js";
2+
import { type VoidComponent, Show } from "solid-js";
33
import { A } from "solid-start";
4-
import { helloQuery } from "rpc/queries";
5-
import { createSession, signOut, signIn } from "@solid-auth/base/client";
4+
import { helloQuery, meQuery } from "rpc/queries";
5+
import { signOut, signIn } from "@solid-auth/base/client";
66

77
const Home: VoidComponent = () => {
88
const hello = helloQuery(() => ({
@@ -51,29 +51,28 @@ const Home: VoidComponent = () => {
5151
export default Home;
5252

5353
const AuthShowcase: VoidComponent = () => {
54-
const sessionData = createSession();
54+
const session = meQuery();
5555
return (
56-
<div class={styles.authContainer}>
57-
<p class={styles.showcaseText}>
58-
{sessionData().status === "authenticated" && (
59-
<span>
60-
Logged in as{" "}
61-
{sessionData().data?.user?.name ?? sessionData().data?.user?.email}
62-
</span>
63-
)}
64-
</p>
65-
<button
66-
class={styles.loginButton}
67-
onClick={() => {
68-
if (sessionData().status === "authenticated") {
69-
void signOut();
70-
} else {
71-
void signIn("github");
72-
}
73-
}}
74-
>
75-
{sessionData().status === "authenticated" ? "Sign out" : "Sign in"}
76-
</button>
77-
</div>
56+
<Show when={session.data}>
57+
<div class={styles.authContainer}>
58+
<p class={styles.showcaseText}>
59+
{session.data?.info && (
60+
<span>Logged in as {session.data.info?.user?.name}</span>
61+
)}
62+
</p>
63+
<button
64+
class={styles.loginButton}
65+
onClick={() => {
66+
if (session.data?.info) {
67+
void signOut().then(() => session.refetch());
68+
} else {
69+
void signIn("github");
70+
}
71+
}}
72+
>
73+
{session.data?.info ? "Sign out" : "Sign in"}
74+
</button>
75+
</div>
76+
</Show>
7877
);
7978
};

0 commit comments

Comments
 (0)