feat: 集成 Better Auth 服务端
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
import { createAuthClient } from 'better-auth/react'
|
||||||
|
|
||||||
|
export const authClient = createAuthClient()
|
||||||
@@ -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 })
|
||||||
|
})
|
||||||
@@ -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()],
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user