Repository navigation
Description
Bug report
- I confirm this is a bug with Supabase, not with my own application.
- I confirm I have searched the Docs, GitHub Discussions, and Discord.
Describe the bug
auth.identities.last_sign_in_at is set when an identity is created and never changes afterwards. Signing in again with the same OAuth provider (Google, LinkedIn OIDC) leaves it at the creation time, while the same identity row's updated_at does move to the time of the sign-in.
The Identities docs describe last_sign_in_at as the time the identity was last used to sign in, so the value cannot be used to tell which identity a user just signed in with.
The cause appears to be in internal/api/external.go, createAccountFromExternalIdentity, case models.AccountExists::
identity = decision.Identities[0]
identity.IdentityData = identityData
if terr = tx.UpdateOnly(identity, "identity_data", "last_sign_in_at"); terr != nil {last_sign_in_at is listed as a column to update, but identity.LastSignInAt is never assigned a new value first, so the update writes back the value that was just loaded. The only place that assigns LastSignInAt is NewIdentity in internal/models/identity.go, and BeforeCreate/BeforeUpdate there do not touch it.
In a production project with about 5,200 identities (email, google, linkedin_oidc), not a single row has a last_sign_in_at later than its created_at, even though many of those users sign in regularly.
To Reproduce
-
Enable an OAuth provider (e.g. Google) on a project.
-
Sign in with that provider as a new user. This creates the identity.
-
Wait a few minutes, sign out, and sign in again with the same provider.
-
Run:
select provider, created_at, updated_at, last_sign_in_at from auth.identities where user_id = '<user id>';
-
updated_atshows the second sign-in, butlast_sign_in_atstill equals the first one (the identity's creation time).
Project-wide check:
select provider,
count(*) as identities,
count(*) filter (where last_sign_in_at > created_at + interval '1 minute') as last_sign_in_moved,
count(*) filter (where updated_at > created_at + interval '1 minute') as updated_at_moved
from auth.identities
group by provider;last_sign_in_moved is 0 for every provider, while updated_at_moved is not.
Expected behavior
A sign-in through an identity sets that identity's last_sign_in_at to the time of the sign-in, so the identity with the newest last_sign_in_at is the one used most recently. The likely fix is to assign identity.LastSignInAt (e.g. now := time.Now(); identity.LastSignInAt = &now) before the UpdateOnly call above.
Screenshots
n/a
System information
- OS: n/a (server-side, hosted Supabase project on supabase.com)
- Browser (if applies): any
- Version of supabase-js: 2.95.3 (with @supabase/ssr 0.8.0)
- Version of Node.js: 24.x
Additional context
- Password sign-ins (
ResourceOwnerPasswordGrantininternal/api/token.go) do not update any identity'slast_sign_in_ateither, so theemailidentity's value also stays at its creation time. If the field is meant to reflect the last sign-in per identity, that path needs the same update. - How this surfaced: we picked the identity with the newest
last_sign_in_atto work out which provider a user had just signed in with, to show a "last used" hint on the sign-in page. Because the value never moves, that always returned the most recently linked identity instead. We now pass the provider through the OAuth redirect URL instead. - Related: Should the
admincreate user handler of Auth create identities based on the provider under the hood? #1577 mentions in passing thatlast_sign_in_atdid not change after a sign-in, but that issue is about the admin create-user endpoint and was closed as a question. - Code references are from
masterat the time of writing.
Activity