feat(auth): add lastSeenAt field to user schema and tests

This commit is contained in:
RainbowBird
2026-08-17 21:57:25 +08:00
parent bd790098d2
commit 5ea7bb1083
5 changed files with 52 additions and 8 deletions
+11
View File
@@ -542,6 +542,17 @@ export function createAuth(
},
user: {
// Keep this server-managed activity field in Better Auth's declared
// schema so `better-auth generate` preserves it in auth-shared.
// Session creation updates it below; clients must never supply it.
additionalFields: {
lastSeenAt: {
type: 'date',
required: false,
input: false,
returned: true,
},
},
changeEmail: {
enabled: true,
// NOTICE:
+26
View File
@@ -3,6 +3,7 @@ import type { AuthEnv } from '../env'
import { generateKeyPairSync } from 'node:crypto'
import { getAuthTables } from 'better-auth/db'
import { decodeJwt, decodeProtectedHeader, importSPKI, jwtVerify } from 'jose'
import { describe, expect, it, vi } from 'vitest'
@@ -59,6 +60,31 @@ describe('createAuth', () => {
expect(auth.options.account?.accountLinking?.allowDifferentEmails).toBe(true)
})
it('registers lastSeenAt as a server-managed Better Auth user field', () => {
const auth = createAuth({} as unknown as AuthDatabase, {
PUBLIC_URL: 'http://localhost:3000',
AUTH_GOOGLE_CLIENT_ID: 'google-client',
AUTH_GOOGLE_CLIENT_SECRET: 'google-secret',
AUTH_GITHUB_CLIENT_ID: 'github-client',
AUTH_GITHUB_CLIENT_SECRET: 'github-secret',
BETTER_AUTH_SECRET: 'test-secret-test-secret-test-secret',
ADDITIONAL_TRUSTED_ORIGINS: [],
} as unknown as AuthEnv)
expect(auth.options.user?.additionalFields?.lastSeenAt).toMatchObject({
type: 'date',
required: false,
input: false,
returned: true,
})
expect(getAuthTables(auth.options).user.fields.lastSeenAt).toMatchObject({
type: 'date',
required: false,
input: false,
returned: true,
})
})
it('asks social providers to show the account picker during OAuth authorization', () => {
const auth = createAuth({} as unknown as AuthDatabase, {
PUBLIC_URL: 'http://localhost:3000',