Skip to content

Commit ec2d9c0

Browse files
make auth package pure
1 parent a41c268 commit ec2d9c0

File tree

13 files changed

+88
-72
lines changed

13 files changed

+88
-72
lines changed

apps/nextjs/src/app/_components/auth-showcase.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { headers } from "next/headers";
22
import { redirect } from "next/navigation";
33

4-
import { auth, getSession } from "@acme/auth";
54
import { Button } from "@acme/ui/button";
65

6+
import { auth, getSession } from "~/auth";
7+
78
export async function AuthShowcase() {
89
const session = await getSession();
910

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { auth } from "@acme/auth";
1+
import { auth } from "~/auth";
22

33
export const GET = auth.handler;
44
export const POST = auth.handler;

apps/nextjs/src/app/api/trpc/[trpc]/route.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
33

44
import { appRouter, createTRPCContext } from "@acme/api";
55

6+
import { auth } from "~/auth";
7+
68
/**
79
* Configure basic CORS headers
810
* You should extend this to match your needs
@@ -29,6 +31,7 @@ const handler = async (req: NextRequest) => {
2931
req,
3032
createContext: () =>
3133
createTRPCContext({
34+
auth: auth,
3235
headers: req.headers,
3336
}),
3437
onError({ error, path }) {
File renamed without changes.

apps/nextjs/src/auth/server.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import "server-only";
2+
3+
import { cache } from "react";
4+
import { headers } from "next/headers";
5+
6+
import { initAuth } from "@acme/auth";
7+
8+
import { env } from "~/env";
9+
10+
export const auth = initAuth({
11+
baseUrl: env.BETTER_AUTH_URL,
12+
secret: env.AUTH_SECRET,
13+
discordClientId: env.AUTH_DISCORD_ID,
14+
discordClientSecret: env.AUTH_DISCORD_SECRET,
15+
});
16+
17+
export const getSession = cache(async () =>
18+
auth.api.getSession({ headers: await headers() }),
19+
);

apps/nextjs/src/env.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { createEnv } from "@t3-oss/env-nextjs";
22
import { vercel } from "@t3-oss/env-nextjs/presets-zod";
33
import { z } from "zod";
44

5-
import { env as authEnv } from "@acme/auth/env";
5+
import { authEnv } from "@acme/auth/env";
66

77
export const env = createEnv({
8-
extends: [authEnv, vercel()],
8+
extends: [authEnv(), vercel()],
99
shared: {
1010
NODE_ENV: z
1111
.enum(["development", "production", "test"])

apps/nextjs/src/trpc/server.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createTRPCOptionsProxy } from "@trpc/tanstack-react-query";
77
import type { AppRouter } from "@acme/api";
88
import { appRouter, createTRPCContext } from "@acme/api";
99

10+
import { auth } from "../auth";
1011
import { createQueryClient } from "./query-client";
1112

1213
/**
@@ -19,6 +20,7 @@ const createContext = cache(async () => {
1920

2021
return createTRPCContext({
2122
headers: heads,
23+
auth,
2224
});
2325
});
2426

packages/api/src/trpc.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { initTRPC, TRPCError } from "@trpc/server";
1010
import superjson from "superjson";
1111
import { ZodError } from "zod";
1212

13-
import { auth } from "@acme/auth";
13+
import type { Auth } from "@acme/auth";
1414
import { db } from "@acme/db/client";
1515

1616
/**
@@ -26,11 +26,16 @@ import { db } from "@acme/db/client";
2626
* @see https://trpc.io/docs/server/context
2727
*/
2828

29-
export const createTRPCContext = async (opts: { headers: Headers }) => {
30-
const session = await auth.api.getSession({
29+
export const createTRPCContext = async (opts: {
30+
headers: Headers;
31+
auth: Auth;
32+
}) => {
33+
const authApi = opts.auth.api;
34+
const session = await authApi.getSession({
3135
headers: opts.headers,
3236
});
3337
return {
38+
authApi,
3439
session,
3540
db,
3641
};

packages/auth/env.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { createEnv } from "@t3-oss/env-nextjs";
22
import { z } from "zod";
33

4-
export const env = createEnv({
5-
server: {
6-
AUTH_DISCORD_ID: z.string().min(1),
7-
BETTER_AUTH_URL: z.string().min(1),
8-
AUTH_DISCORD_SECRET: z.string().min(1),
9-
AUTH_SECRET:
10-
process.env.NODE_ENV === "production"
11-
? z.string().min(1)
12-
: z.string().min(1).optional(),
13-
NODE_ENV: z.enum(["development", "production"]).optional(),
14-
},
15-
client: {},
16-
experimental__runtimeEnv: {},
17-
skipValidation:
18-
!!process.env.CI || process.env.npm_lifecycle_event === "lint",
19-
});
4+
export function authEnv() {
5+
return createEnv({
6+
server: {
7+
AUTH_DISCORD_ID: z.string().min(1),
8+
BETTER_AUTH_URL: z.string().min(1),
9+
AUTH_DISCORD_SECRET: z.string().min(1),
10+
AUTH_SECRET:
11+
process.env.NODE_ENV === "production"
12+
? z.string().min(1)
13+
: z.string().min(1).optional(),
14+
NODE_ENV: z.enum(["development", "production"]).optional(),
15+
},
16+
experimental__runtimeEnv: {},
17+
skipValidation:
18+
!!process.env.CI || process.env.npm_lifecycle_event === "lint",
19+
});
20+
}

packages/auth/src/auth.ts

-28
This file was deleted.

packages/auth/src/index.rsc.ts

-11
This file was deleted.

packages/auth/src/index.ts

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
1-
import { headers } from "next/headers";
1+
import type { BetterAuthOptions } from "better-auth";
2+
import { expo } from "@better-auth/expo";
3+
import { betterAuth } from "better-auth";
4+
import { drizzleAdapter } from "better-auth/adapters/drizzle";
5+
import { oAuthProxy } from "better-auth/plugins";
26

3-
import { auth } from "./auth";
7+
import { db } from "@acme/db/client";
48

5-
export const getSession = async () =>
6-
auth.api.getSession({
7-
headers: await headers(),
8-
});
9+
export function initAuth(options: {
10+
baseUrl: string;
11+
secret: string | undefined;
912

10-
export * from "./auth";
13+
discordClientId: string;
14+
discordClientSecret: string;
15+
}) {
16+
const config = {
17+
database: drizzleAdapter(db, {
18+
provider: "pg",
19+
}),
20+
baseURL: options.baseUrl,
21+
secret: options.secret,
22+
plugins: [oAuthProxy(), expo()],
23+
socialProviders: {
24+
discord: {
25+
clientId: options.discordClientId,
26+
clientSecret: options.discordClientSecret,
27+
redirectURI: `http://${options.baseUrl}/api/auth/callback/discord`,
28+
},
29+
},
30+
trustedOrigins: ["expo://"],
31+
} satisfies BetterAuthOptions;
32+
33+
return betterAuth(config);
34+
}
35+
36+
export type Auth = ReturnType<typeof initAuth>;
37+
export type Session = Auth["$Infer"]["Session"];

packages/auth/src/middleware.ts

-3
This file was deleted.

0 commit comments

Comments
 (0)