Update UpdateUserLastLogin method to require loginCtx
- Modified the UpdateUserLastLogin method to validate that loginCtx is not nil, returning an error if it is. - Updated the corresponding test to reflect this change, ensuring that an error is asserted when loginCtx is nil, improving error handling and robustness of user login tracking.
This commit is contained in:
parent
0a353e6e41
commit
ba296c3b48
2 changed files with 20 additions and 16 deletions
|
|
@ -468,12 +468,16 @@ func (u *DefaultUser) DeleteUser(ctx context.Context, userID string) error {
|
||||||
|
|
||||||
// UpdateUserLastLogin updates the user's last login timestamp and context
|
// UpdateUserLastLogin updates the user's last login timestamp and context
|
||||||
func (u *DefaultUser) UpdateUserLastLogin(ctx context.Context, userID string, loginCtx *types.LoginContext) error {
|
func (u *DefaultUser) UpdateUserLastLogin(ctx context.Context, userID string, loginCtx *types.LoginContext) error {
|
||||||
|
// Validate loginCtx is required
|
||||||
|
if loginCtx == nil {
|
||||||
|
return fmt.Errorf("loginCtx is required")
|
||||||
|
}
|
||||||
|
|
||||||
updateData := maps.MapStrAny{
|
updateData := maps.MapStrAny{
|
||||||
"last_login_at": time.Now(),
|
"last_login_at": time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add login context fields if provided
|
// Add login context fields
|
||||||
if loginCtx != nil {
|
|
||||||
if loginCtx.IP != "" {
|
if loginCtx.IP != "" {
|
||||||
updateData["last_login_ip"] = loginCtx.IP
|
updateData["last_login_ip"] = loginCtx.IP
|
||||||
}
|
}
|
||||||
|
|
@ -486,7 +490,6 @@ func (u *DefaultUser) UpdateUserLastLogin(ctx context.Context, userID string, lo
|
||||||
if loginCtx.Platform != "" {
|
if loginCtx.Platform != "" {
|
||||||
updateData["last_login_platform"] = loginCtx.Platform
|
updateData["last_login_platform"] = loginCtx.Platform
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return u.UpdateUser(ctx, userID, updateData)
|
return u.UpdateUser(ctx, userID, updateData)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -221,9 +221,10 @@ func TestUserBasicOperations(t *testing.T) {
|
||||||
assert.Equal(t, "desktop", user["last_login_device"])
|
assert.Equal(t, "desktop", user["last_login_device"])
|
||||||
assert.Equal(t, "web", user["last_login_platform"])
|
assert.Equal(t, "web", user["last_login_platform"])
|
||||||
|
|
||||||
// Test with nil loginCtx (should only update timestamp)
|
// Test with nil loginCtx (should return error)
|
||||||
err = testProvider.UpdateUserLastLogin(ctx, testUserID, nil)
|
err = testProvider.UpdateUserLastLogin(ctx, testUserID, nil)
|
||||||
assert.NoError(t, err)
|
assert.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "loginCtx is required")
|
||||||
|
|
||||||
// Test with partial loginCtx (only IP)
|
// Test with partial loginCtx (only IP)
|
||||||
partialCtx := &types.LoginContext{
|
partialCtx := &types.LoginContext{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue