From 27d3cd364ee9f5c87e5f90bf470983b890354ffc Mon Sep 17 00:00:00 2001 From: Jonatan Granqvist Date: Tue, 24 Feb 2026 09:09:08 +0100 Subject: [PATCH] add auth anonymous plugin --- apps/web/src/lib/auth-client.ts | 5 ++++- packages/auth/src/index.ts | 3 ++- packages/db/src/schema/auth.ts | 29 ++++++++++++++++++++--------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/apps/web/src/lib/auth-client.ts b/apps/web/src/lib/auth-client.ts index 8baa342..3b12ab6 100644 --- a/apps/web/src/lib/auth-client.ts +++ b/apps/web/src/lib/auth-client.ts @@ -1,3 +1,6 @@ import { createAuthClient } from "better-auth/react"; +import { anonymousClient } from "better-auth/client/plugins"; -export const authClient = createAuthClient({}); +export const authClient = createAuthClient({ + plugins: [anonymousClient()], +}); diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts index bcd382d..2736925 100644 --- a/packages/auth/src/index.ts +++ b/packages/auth/src/index.ts @@ -2,6 +2,7 @@ import { db } from "@zendegi/db"; import * as schema from "@zendegi/db/schema/auth"; import { env } from "@zendegi/env/server"; import { betterAuth } from "better-auth"; +import { anonymous } from "better-auth/plugins"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { tanstackStartCookies } from "better-auth/tanstack-start"; @@ -15,7 +16,7 @@ export const auth = betterAuth({ emailAndPassword: { enabled: true, }, - plugins: [tanstackStartCookies()], + plugins: [tanstackStartCookies(), anonymous()], advanced: { database: { generateId: "uuid", diff --git a/packages/db/src/schema/auth.ts b/packages/db/src/schema/auth.ts index e2a996b..bcedee8 100644 --- a/packages/db/src/schema/auth.ts +++ b/packages/db/src/schema/auth.ts @@ -16,11 +16,14 @@ export const user = pgTable("user", { email: text("email").notNull().unique(), emailVerified: boolean("email_verified").default(false).notNull(), image: text("image"), - createdAt: timestamp("created_at").defaultNow().notNull(), - updatedAt: timestamp("updated_at") + createdAt: timestamp("created_at", { withTimezone: true }) + .defaultNow() + .notNull(), + updatedAt: timestamp("updated_at", { withTimezone: true }) .defaultNow() .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), + isAnonymous: boolean("is_anonymous").default(false), }); export const session = pgTable( @@ -29,10 +32,12 @@ export const session = pgTable( id: uuid("id") .default(sql`pg_catalog.gen_random_uuid()`) .primaryKey(), - expiresAt: timestamp("expires_at").notNull(), + expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(), token: text("token").notNull().unique(), - createdAt: timestamp("created_at").defaultNow().notNull(), - updatedAt: timestamp("updated_at") + createdAt: timestamp("created_at", { withTimezone: true }) + .defaultNow() + .notNull(), + updatedAt: timestamp("updated_at", { withTimezone: true }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), ipAddress: text("ip_address"), @@ -58,12 +63,18 @@ export const account = pgTable( accessToken: text("access_token"), refreshToken: text("refresh_token"), idToken: text("id_token"), - accessTokenExpiresAt: timestamp("access_token_expires_at"), - refreshTokenExpiresAt: timestamp("refresh_token_expires_at"), + accessTokenExpiresAt: timestamp("access_token_expires_at", { + withTimezone: true, + }), + refreshTokenExpiresAt: timestamp("refresh_token_expires_at", { + withTimezone: true, + }), scope: text("scope"), password: text("password"), - createdAt: timestamp("created_at").defaultNow().notNull(), - updatedAt: timestamp("updated_at") + createdAt: timestamp("created_at", { withTimezone: true }) + .defaultNow() + .notNull(), + updatedAt: timestamp("updated_at", { withTimezone: true }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), },