feat: 集成 Better Auth 服务端

This commit is contained in:
2026-03-30 21:26:52 +08:00
parent 50472dbba7
commit 8b754f9fe6
3 changed files with 36 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
import { createAuthClient } from 'better-auth/react'
export const authClient = createAuthClient()
+8
View File
@@ -0,0 +1,8 @@
import { createServerFn } from '@tanstack/react-start'
import { getRequestHeaders } from '@tanstack/react-start/server'
import { auth } from '@/server/auth'
export const getSession = createServerFn({ method: 'GET' }).handler(async () => {
const headers = getRequestHeaders()
return await auth.api.getSession({ headers })
})
+25
View File
@@ -0,0 +1,25 @@
import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { tanstackStartCookies } from 'better-auth/tanstack-start'
import { env } from '@/env'
import * as authSchema from '@/server/auth/schema'
import { getDB } from '@/server/db'
export const auth = betterAuth({
baseURL: env.BETTER_AUTH_URL,
secret: env.BETTER_AUTH_SECRET,
database: drizzleAdapter(getDB(), {
provider: 'pg',
schema: authSchema,
}),
emailAndPassword: { enabled: true },
session: {
expiresIn: 60 * 60 * 24 * 7,
updateAge: 60 * 60 * 24,
cookieCache: {
enabled: true,
maxAge: 5 * 60,
},
},
plugins: [tanstackStartCookies()],
})