-
Notifications
You must be signed in to change notification settings - Fork 0
#645 Add A Last Sign-In Column To The Users Page #777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cielbellerose
merged 4 commits into
dev
from
645-add-a-last-sign-in-column-to-the-users-page
Sep 25, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
01b41ad
#645 stamp User.lastLoginAt on sign-in
cielbellerose 9cac4c3
#645 extract DataTable's row comparator into sortRows
cielbellerose 625073c
#645 add a Last sign-in column to the users page
cielbellerose 8d45b25
#645 address review feedback
cielbellerose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import 'server-only'; | ||
|
|
||
| import { APIError } from 'better-auth/api'; | ||
|
|
||
| import { ACCOUNT_DEACTIVATED_ERROR_CODE } from '@/lib/constants'; | ||
| import { prisma } from '@/lib/prisma'; | ||
|
|
||
| // Throws — returning false leaves callers dereferencing a null session's .token. | ||
| export async function assertSessionUserActive(userId: string): Promise<void> { | ||
| const user = await prisma.user.findUnique({ | ||
| where: { id: userId }, | ||
| select: { deletedAt: true }, | ||
| }); | ||
| if (user?.deletedAt) | ||
| throw APIError.from('FORBIDDEN', { | ||
| code: ACCOUNT_DEACTIVATED_ERROR_CODE, | ||
| message: 'This account has been deactivated.', | ||
| }); | ||
| } | ||
|
|
||
| // deletedAt scope is defence in depth: Better Auth already skips `after` when | ||
| // `before` throws, but a deactivated row must never be stamped regardless. | ||
| export async function recordSignIn(userId: string): Promise<void> { | ||
| await prisma.user.updateMany({ | ||
| where: { id: userId, deletedAt: null }, | ||
| data: { lastLoginAt: new Date() }, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20260924232626_add_user_last_login_at/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- AlterTable | ||
| ALTER TABLE "User" ADD COLUMN "lastLoginAt" TIMESTAMP(3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { cleanupFixtures, createTestUser } from '@/tests/helpers/fixtures'; | ||
| import { afterAll, describe, expect, it } from 'vitest'; | ||
|
|
||
| import { getUsersForAdmin } from '@/prisma/data/users'; | ||
|
|
||
| import { | ||
| assertSessionUserActive, | ||
| recordSignIn, | ||
| } from '@/lib/auth/session-hooks'; | ||
| import { ACCOUNT_DEACTIVATED_ERROR_CODE } from '@/lib/constants'; | ||
| import { prisma } from '@/lib/prisma'; | ||
|
|
||
| afterAll(async () => { | ||
| await cleanupFixtures(); | ||
| }); | ||
|
|
||
| describe('recordSignIn', () => { | ||
| it('stamps a null lastLoginAt', async () => { | ||
| const user = await createTestUser(); | ||
| expect(user.lastLoginAt).toBeNull(); | ||
|
|
||
| await recordSignIn(user.id); | ||
|
|
||
| const stamped = await prisma.user.findUniqueOrThrow({ | ||
| where: { id: user.id }, | ||
| }); | ||
| expect(stamped.lastLoginAt).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('overwrites an earlier stamp on a second call', async () => { | ||
| const user = await createTestUser(); | ||
|
|
||
| await recordSignIn(user.id); | ||
| const first = await prisma.user.findUniqueOrThrow({ | ||
| where: { id: user.id }, | ||
| }); | ||
|
|
||
| await recordSignIn(user.id); | ||
| const second = await prisma.user.findUniqueOrThrow({ | ||
| where: { id: user.id }, | ||
| }); | ||
|
|
||
| expect(second.lastLoginAt!.getTime()).toBeGreaterThanOrEqual( | ||
| first.lastLoginAt!.getTime(), | ||
| ); | ||
| }); | ||
|
|
||
| it('leaves a deactivated user untouched', async () => { | ||
| const user = await createTestUser({ deletedAt: new Date() }); | ||
|
|
||
| await recordSignIn(user.id); | ||
|
|
||
| const unchanged = await prisma.user.findUniqueOrThrow({ | ||
| where: { id: user.id }, | ||
| }); | ||
| expect(unchanged.lastLoginAt).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('assertSessionUserActive', () => { | ||
| it('rejects a deactivated user with ACCOUNT_DEACTIVATED_ERROR_CODE', async () => { | ||
| const user = await createTestUser({ deletedAt: new Date() }); | ||
|
|
||
| await expect(assertSessionUserActive(user.id)).rejects.toMatchObject({ | ||
| body: { code: ACCOUNT_DEACTIVATED_ERROR_CODE }, | ||
| }); | ||
| }); | ||
|
|
||
| it('resolves for an active user', async () => { | ||
| const user = await createTestUser(); | ||
| await expect(assertSessionUserActive(user.id)).resolves.toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getUsersForAdmin', () => { | ||
| it('includes lastLoginAt', async () => { | ||
| const user = await createTestUser(); | ||
| await recordSignIn(user.id); | ||
|
|
||
| const list = await getUsersForAdmin(); | ||
| const row = list.find((u) => u.id === user.id); | ||
| expect(row?.lastLoginAt).not.toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.