Repository navigation
Description
Is your feature request related to a problem? Please describe.
AuthKey is used to create an authentication key used to authorize users' calls to endpoints. The class contains a user ID field that is thought to be linked to the user information to which the credentials belong.
This schema makes Serverpod core models depend on models introduced in an optional Serverpod module. It also enforces a certain pattern for connecting auth credentials with the user's information.
With the introduction of object relations in the Serverpod ORM and the possibility of adding relations to model tables in modules, there is now better support for handling relations. Unfortunately, these can't be used since relations are not allowed to modify module tables.
With the current solution, an AuthKey entry also needs to be added for each authentication token created by the user, duplicating the scopes granted to the user.
Describe the solution you'd like
Two new model tables could be implemented with different purposes. The first would be to define a single user (referred to as AuthUser) in the eyes of the authentication and the second would be to record each authentication key (referred to as AuthToken) generated for the user. Serverpod's authentication should then use these in place of the current AuthKey.
AuthUser
The auth user model is the representation of the user in the authentication system. It only contains an ID and Scopes for the user.
It could look something like this:
class: AuthUser
table: serverpod_auth_user
fields:
### The scopes this user is granted.
scopes: List<String>AuthToken
The auth token is used to record successful authentications and is used to validate the auth user.
It could look something like this:
class: AuthToken
table: serverpod_auth_tokens
fields:
### The user to whom this token belongs.
authUser: AuthUser?, relation
### The hashed version of the key.
keyHash: String
### The key that is used to authenticate the user.
key: String?, !persist
### The method of signing in this key was generated through. This can be email
### or different social logins.
method: String
indexes:
serverpod_auth_token_authUserId_idx:
fields: authUserIdDependency towards AuthUser
With this structure, it is possible to create database relations towards the auth user like so:
class: MyUser
table: my_user
fields:
authUser: AuthUser?, relation
name: String
email: StringThis enables the use of referential actions, meaning that any authentication information could be removed if the user object is removed.
It also creates a single source of truth for what scopes are granted an auth user.
Describe alternatives you've considered
An additional junction table could also be implemented to provide the same type of support. That is, a table connecting a user's information with AuthKey entries.
But this would introduce redundant complexity and duplicate unnecessary information.
Additional context
The implementation of this change will need to be reflected in the authentication module user model along with how sign in users.
Both entries will need to be supported over a period of time to allow users to authenticate both using the old way and the new way.
Entries could slowly be migrated when encountered to allow a safe removal of the old table and field at a later state.
Activity
[-]Replace AuthKey with a single authentication user that can be referred in relation.[/-][+]Introduce AuthUser with a single authentication user that can be referred in relation.[/+]SandPod commentedon Jun 16, 2025
This feature was solved by: #3445