Merge pull request #1192 from trheyi/main
Update UpdateUserLastLogin method to require loginCtx
This commit is contained in:
commit
b3a365b53b
2 changed files with 20 additions and 16 deletions
|
|
@ -468,24 +468,27 @@ func (u *DefaultUser) DeleteUser(ctx context.Context, userID string) error {
|
|||
|
||||
// UpdateUserLastLogin updates the user's last login timestamp and context
|
||||
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{
|
||||
"last_login_at": time.Now(),
|
||||
}
|
||||
|
||||
// Add login context fields if provided
|
||||
if loginCtx != nil {
|
||||
if loginCtx.IP != "" {
|
||||
updateData["last_login_ip"] = loginCtx.IP
|
||||
}
|
||||
if loginCtx.UserAgent != "" {
|
||||
updateData["last_login_user_agent"] = loginCtx.UserAgent
|
||||
}
|
||||
if loginCtx.Device != "" {
|
||||
updateData["last_login_device"] = loginCtx.Device
|
||||
}
|
||||
if loginCtx.Platform != "" {
|
||||
updateData["last_login_platform"] = loginCtx.Platform
|
||||
}
|
||||
// Add login context fields
|
||||
if loginCtx.IP != "" {
|
||||
updateData["last_login_ip"] = loginCtx.IP
|
||||
}
|
||||
if loginCtx.UserAgent != "" {
|
||||
updateData["last_login_user_agent"] = loginCtx.UserAgent
|
||||
}
|
||||
if loginCtx.Device != "" {
|
||||
updateData["last_login_device"] = loginCtx.Device
|
||||
}
|
||||
if loginCtx.Platform != "" {
|
||||
updateData["last_login_platform"] = loginCtx.Platform
|
||||
}
|
||||
|
||||
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, "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)
|
||||
assert.NoError(t, err)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "loginCtx is required")
|
||||
|
||||
// Test with partial loginCtx (only IP)
|
||||
partialCtx := &types.LoginContext{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue