perf: init auth client
This commit is contained in:
@@ -7,7 +7,7 @@ import superjson from "superjson";
|
|||||||
import { ZodError } from "zod";
|
import { ZodError } from "zod";
|
||||||
|
|
||||||
import type { dbClient } from "@kan/db/client";
|
import type { dbClient } from "@kan/db/client";
|
||||||
// import { auth } from "@kan/auth";
|
import { initAuth } from "@kan/auth";
|
||||||
import { createDrizzleClient } from "@kan/db/client";
|
import { createDrizzleClient } from "@kan/db/client";
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
@@ -26,16 +26,6 @@ interface CreateContextOptions {
|
|||||||
db: dbClient;
|
db: dbClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
// dummy user for testing edge speed
|
|
||||||
const user = {
|
|
||||||
id: "2b97d1f4-82db-415c-8be8-b6b1c1c13cbf",
|
|
||||||
name: "John Doe",
|
|
||||||
email: "john.doe@example.com",
|
|
||||||
emailVerified: true,
|
|
||||||
createdAt: new Date(),
|
|
||||||
updatedAt: new Date(),
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createInnerTRPCContext = (opts: CreateContextOptions) => {
|
export const createInnerTRPCContext = (opts: CreateContextOptions) => {
|
||||||
return {
|
return {
|
||||||
user: opts.user,
|
user: opts.user,
|
||||||
@@ -46,23 +36,25 @@ export const createInnerTRPCContext = (opts: CreateContextOptions) => {
|
|||||||
export const createTRPCContext = async ({
|
export const createTRPCContext = async ({
|
||||||
req,
|
req,
|
||||||
}: FetchCreateContextFnOptions) => {
|
}: FetchCreateContextFnOptions) => {
|
||||||
// const session = await auth.api.getSession({
|
|
||||||
// headers: req.headers,
|
|
||||||
// });
|
|
||||||
|
|
||||||
const db = createDrizzleClient();
|
const db = createDrizzleClient();
|
||||||
|
const auth = initAuth(db);
|
||||||
|
|
||||||
return createInnerTRPCContext({ db, user });
|
const session = await auth.api.getSession({
|
||||||
|
headers: req.headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
return createInnerTRPCContext({ db, user: session?.user });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createNextApiContext = async (req: NextRequest) => {
|
export const createNextApiContext = async (req: NextRequest) => {
|
||||||
// const session = await auth.api.getSession({
|
|
||||||
// headers: req.headers,
|
|
||||||
// });
|
|
||||||
|
|
||||||
const db = createDrizzleClient();
|
const db = createDrizzleClient();
|
||||||
|
const auth = initAuth(db);
|
||||||
|
|
||||||
return createInnerTRPCContext({ db, user: user });
|
const session = await auth.api.getSession({
|
||||||
|
headers: req.headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
return createInnerTRPCContext({ db, user: session?.user });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createRESTContext = async ({ req }: CreateNextContextOptions) => {
|
export const createRESTContext = async ({ req }: CreateNextContextOptions) => {
|
||||||
@@ -72,17 +64,18 @@ export const createRESTContext = async ({ req }: CreateNextContextOptions) => {
|
|||||||
: null;
|
: null;
|
||||||
|
|
||||||
const db = createDrizzleClient();
|
const db = createDrizzleClient();
|
||||||
|
const auth = initAuth(db);
|
||||||
|
|
||||||
if (!accessToken) {
|
if (!accessToken) {
|
||||||
return createInnerTRPCContext({ db, user: null });
|
return createInnerTRPCContext({ db, user: null });
|
||||||
}
|
}
|
||||||
|
|
||||||
// const session = await auth.api.getSession({
|
const session = await auth.api.getSession({
|
||||||
// // @ts-expect-error
|
// @ts-expect-error
|
||||||
// headers: new Headers(req.headers),
|
headers: new Headers(req.headers),
|
||||||
// });
|
});
|
||||||
|
|
||||||
return createInnerTRPCContext({ db, user });
|
return createInnerTRPCContext({ db, user: session?.user });
|
||||||
};
|
};
|
||||||
|
|
||||||
const t = initTRPC
|
const t = initTRPC
|
||||||
|
|||||||
@@ -4,79 +4,80 @@ import { createAuthMiddleware } from "better-auth/api";
|
|||||||
import { apiKey } from "better-auth/plugins";
|
import { apiKey } from "better-auth/plugins";
|
||||||
import { magicLink } from "better-auth/plugins/magic-link";
|
import { magicLink } from "better-auth/plugins/magic-link";
|
||||||
|
|
||||||
import { createDrizzleClient } from "@kan/db/client";
|
import type { dbClient } from "@kan/db/client";
|
||||||
import * as userRepo from "@kan/db/repository/user.repo";
|
import * as userRepo from "@kan/db/repository/user.repo";
|
||||||
import * as schema from "@kan/db/schema";
|
import * as schema from "@kan/db/schema";
|
||||||
import { sendEmail } from "@kan/email";
|
import { sendEmail } from "@kan/email";
|
||||||
import { createStripeClient } from "@kan/stripe";
|
import { createStripeClient } from "@kan/stripe";
|
||||||
|
|
||||||
const db = createDrizzleClient();
|
export const initAuth = (db: dbClient) => {
|
||||||
export const auth = betterAuth({
|
return betterAuth({
|
||||||
secret: process.env.BETTER_AUTH_SECRET!,
|
secret: process.env.BETTER_AUTH_SECRET!,
|
||||||
baseURL: process.env.BETTER_AUTH_BASE_URL!,
|
baseURL: process.env.BETTER_AUTH_BASE_URL!,
|
||||||
trustedOrigins: process.env.BETTER_AUTH_TRUSTED_ORIGINS
|
trustedOrigins: process.env.BETTER_AUTH_TRUSTED_ORIGINS
|
||||||
? process.env.BETTER_AUTH_TRUSTED_ORIGINS.split(",")
|
? process.env.BETTER_AUTH_TRUSTED_ORIGINS.split(",")
|
||||||
: [],
|
: [],
|
||||||
database: drizzleAdapter(db, {
|
database: drizzleAdapter(db, {
|
||||||
provider: "pg",
|
provider: "pg",
|
||||||
schema: {
|
schema: {
|
||||||
...schema,
|
...schema,
|
||||||
user: schema.users,
|
user: schema.users,
|
||||||
},
|
|
||||||
}),
|
|
||||||
socialProviders: {
|
|
||||||
google: {
|
|
||||||
clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
||||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
additionalFields: {
|
|
||||||
stripeCustomerId: {
|
|
||||||
type: "string",
|
|
||||||
required: false,
|
|
||||||
defaultValue: null,
|
|
||||||
input: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
apiKey(),
|
|
||||||
magicLink({
|
|
||||||
sendMagicLink: async ({ email, url }) => {
|
|
||||||
await sendEmail(email, "Sign in to kan.bn", "MAGIC_LINK", {
|
|
||||||
magicLoginUrl: url,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
],
|
socialProviders: {
|
||||||
hooks: {
|
google: {
|
||||||
// after: createAuthMiddleware(async (ctx) => {
|
clientId: process.env.GOOGLE_CLIENT_ID!,
|
||||||
// if (ctx.path.startsWith("/sign-up") || ctx.path.startsWith("/sign-in")) {
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
||||||
// const session = ctx.context.session;
|
},
|
||||||
// if (
|
|
||||||
// session &&
|
|
||||||
// process.env.NEXT_PUBLIC_KAN_ENV === "cloud" &&
|
|
||||||
// !session.user.stripeCustomerId
|
|
||||||
// ) {
|
|
||||||
// const stripe = createStripeClient();
|
|
||||||
// const stripeCustomer = await stripe.customers.create({
|
|
||||||
// email: session.user.email,
|
|
||||||
// metadata: {
|
|
||||||
// userId: session.user.id,
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
// await userRepo.update(db, session.user.id, {
|
|
||||||
// stripeCustomerId: stripeCustomer.id,
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }),
|
|
||||||
},
|
|
||||||
advanced: {
|
|
||||||
cookiePrefix: "kan",
|
|
||||||
database: {
|
|
||||||
generateId: false,
|
|
||||||
},
|
},
|
||||||
},
|
user: {
|
||||||
});
|
additionalFields: {
|
||||||
|
stripeCustomerId: {
|
||||||
|
type: "string",
|
||||||
|
required: false,
|
||||||
|
defaultValue: null,
|
||||||
|
input: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
apiKey(),
|
||||||
|
magicLink({
|
||||||
|
sendMagicLink: async ({ email, url }) => {
|
||||||
|
await sendEmail(email, "Sign in to kan.bn", "MAGIC_LINK", {
|
||||||
|
magicLoginUrl: url,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
hooks: {
|
||||||
|
// after: createAuthMiddleware(async (ctx) => {
|
||||||
|
// if (ctx.path.startsWith("/sign-up") || ctx.path.startsWith("/sign-in")) {
|
||||||
|
// const session = ctx.context.session;
|
||||||
|
// if (
|
||||||
|
// session &&
|
||||||
|
// process.env.NEXT_PUBLIC_KAN_ENV === "cloud" &&
|
||||||
|
// !session.user.stripeCustomerId
|
||||||
|
// ) {
|
||||||
|
// const stripe = createStripeClient();
|
||||||
|
// const stripeCustomer = await stripe.customers.create({
|
||||||
|
// email: session.user.email,
|
||||||
|
// metadata: {
|
||||||
|
// userId: session.user.id,
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
// await userRepo.update(db, session.user.id, {
|
||||||
|
// stripeCustomerId: stripeCustomer.id,
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }),
|
||||||
|
},
|
||||||
|
advanced: {
|
||||||
|
cookiePrefix: "kan",
|
||||||
|
database: {
|
||||||
|
generateId: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { auth } from "./auth";
|
import { initAuth } from "./auth";
|
||||||
import { authClient } from "./clients";
|
import { authClient } from "./clients";
|
||||||
|
|
||||||
export const name = "auth";
|
export const name = "auth";
|
||||||
|
|
||||||
export { auth, authClient };
|
export { initAuth, authClient };
|
||||||
|
|||||||
Reference in New Issue
Block a user