Skip to content

Commit 9221ddd

Browse files
committed
improvement from code-review
1 parent b3d8160 commit 9221ddd

4 files changed

Lines changed: 64 additions & 5 deletions

File tree

bun.lock

Lines changed: 22 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
"vue-demi"
7777
],
7878
"overrides": {
79-
"vue": "3.5.32"
79+
"vue": "3.5.32",
80+
"@noble/hashes": "^1.7.2"
8081
},
8182
"patchedDependencies": {
8283
"vaul-vue@0.4.1": "patches/vaul-vue@0.4.1.patch"

packages/evlog/src/better-auth/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ function extractUserData(
125125
const fields = options?.fields ?? DEFAULT_USER_FIELDS
126126
const data: Record<string, unknown> = {}
127127

128+
if (user.id !== undefined && user.id !== null) {
129+
data.id = user.id
130+
}
131+
128132
for (const field of fields) {
133+
if (field === 'id') continue
129134
const value = user[field]
130135
if (value === undefined || value === null) continue
131136

@@ -297,7 +302,10 @@ export function createAuthMiddleware(
297302
if (options?.onAnonymous) await options.onAnonymous(log)
298303
return false
299304
} catch (err) {
305+
const resolvedIn = Date.now() - start
306+
log.set({ auth: { resolvedIn, identified: false, error: true } } as Record<string, unknown>)
300307
if (isDev) console.warn('[evlog/better-auth] Session resolution failed:', err)
308+
if (options?.onAnonymous) await options.onAnonymous(log)
301309
return false
302310
}
303311
}

packages/evlog/test/better-auth/better-auth.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ describe('identifyUser', () => {
159159
expect(user.emailVerified).toBeUndefined()
160160
})
161161

162+
it('always includes id even when fields omits it', () => {
163+
const log = createMockLogger()
164+
identifyUser(log, createMockSession(), { fields: ['name', 'email'] })
165+
166+
const user = log.setCalls[0].user as Record<string, unknown>
167+
expect(user.id).toBe('usr_123')
168+
expect(user.name).toBe('Hugo Richard')
169+
expect(user.email).toBe('hugo@example.com')
170+
})
171+
162172
it('handles string dates in session', () => {
163173
const log = createMockLogger()
164174
const session = createMockSession({
@@ -270,7 +280,7 @@ describe('createAuthMiddleware', () => {
270280
expect(authData.identified).toBe(false)
271281
})
272282

273-
it('catches errors silently', async () => {
283+
it('catches errors and sets auth metadata with error flag', async () => {
274284
const log = createMockLogger()
275285
const auth = {
276286
api: {
@@ -281,6 +291,27 @@ describe('createAuthMiddleware', () => {
281291

282292
const result = await identify(log, new Headers())
283293
expect(result).toBe(false)
294+
const authData = log.setCalls.find(c => c.auth)?.auth as Record<string, unknown>
295+
expect(authData).toBeDefined()
296+
expect(authData.identified).toBe(false)
297+
expect(authData.error).toBe(true)
298+
expect(typeof authData.resolvedIn).toBe('number')
299+
})
300+
301+
it('calls onAnonymous on error path', async () => {
302+
const log = createMockLogger()
303+
const auth = {
304+
api: {
305+
getSession: vi.fn(() => Promise.reject(new Error('DB down'))),
306+
},
307+
}
308+
const onAnonymous = vi.fn()
309+
const identify = createAuthMiddleware(auth, { onAnonymous })
310+
311+
await identify(log, new Headers())
312+
313+
expect(onAnonymous).toHaveBeenCalledOnce()
314+
expect(onAnonymous).toHaveBeenCalledWith(log)
284315
})
285316

286317
it('passes identify options through', async () => {

0 commit comments

Comments
 (0)