BLOG-94 Create user in DB when first login through OIDC #96
Loading…
x
Reference in New Issue
Block a user
No description provided.
Delete Branch "BLOG-94_create_user_when_first_login"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
This PR introduces the functionality to persist user information in the database. When a user logs in via OIDC for the first time, a new user record is created. Subsequent logins will retrieve the existing user data from the database.
This change ensures that users have a persistent identity within our system, identified by their unique combination of OIDC issuer and subject ID.
Key Changes
User Persistence Logic:
ExchangeAuthCodeUseCase
, after successfully exchanging the authorization code, the logic now checks if the user exists in our database using theirissuer
andsource_id
.AuthError::UserNotFound
), a new record is created in theuser
table.User
entity returned by the use case now contains the internal databaseid
.Database Integration in Auth Feature:
UserDbService
trait and itssqlx
-based implementation,UserDbServiceImpl
, to handle database operations for users.AuthRepository
is extended to include methods for querying (get_user_by_source_id
) and saving (save_user
) users, delegating the calls to the newUserDbService
.server/src/container.rs
has been updated to provide theUserDbServiceImpl
to theAuthRepositoryImpl
.Domain and Data Model Updates:
User
domain entity now includesid
(the database primary key) andissuer
(from OIDC claims) to uniquely identify a user across different identity providers.UserResponseDto
now exposes the internalid
instead of thesource_id
.Session Management:
user_id
(i32
) instead of the entire user object. This is more efficient and secure.constants.rs
file for better maintainability.Database Changes
user
table.id
,issuer
,source_id
,displayed_name
, andemail
.UNIQUE
index has been created on(source_id, issuer)
to guarantee that each user from a specific identity provider is stored only once.Refactoring
image
feature to changeid: Option<i32>
toid: i32
for consistency with the newUser
entity model.Package Changes
No response
Screenshots
No response
Reference
Resolves #94
Checklist
/improve
PR Code Suggestions ✨
Improve error handling for user retrieval
The current error handling for
get_user_by_source_id
only checks forAuthError::UserNotFound
. If a different database error occurs, it is not propagated,leading to a silent failure and potentially returning an unsaved user. Use a
match
statement to explicitly handle all
Result
variants, propagating unexpected errors.backend/feature/auth/src/application/use_case/exchange_auth_code_use_case.rs [49-62]
Suggestion importance[1-10]: 9
__
Why: The original code only handled
AuthError::UserNotFound
, potentially leading to silent failures ifget_user_by_source_id
returned other errors. The suggestedmatch
statement correctly propagates all errors and ensures theid
is updated for existing users, significantly improving robustness.Addressed in
ef7db7424a