tsk-34: Update last login of user (#37)

Closes #34

Reviewed-on: phoenix/textsender-auth#37
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-12-31 23:13:02 +00:00
committed by phoenix
parent d34cad033d
commit de171b790d
10 changed files with 101 additions and 12 deletions
+3 -1
View File
@@ -3,7 +3,9 @@ package endpoint
// Endpoint for registering a user
const Register = "/api/v1/register"
const Login = "/api/v1/login"
const UpdatePassword = "/api/v1/user/password/update"
const CreateServiceUser = "/api/v1/service/register"
const LoginServiceUser = "/api/v1/service/login"
const TokenRefresh = "/api/v1/token/refresh"
const UpdatePassword = "/api/v1/user/password/update"
+12 -3
View File
@@ -3,6 +3,7 @@ package handler
import (
"log"
"net/http"
"time"
"git.kundeng.us/phoenix/textsender-models/tx0/token"
"git.kundeng.us/phoenix/textsender-models/tx0/user"
@@ -75,6 +76,7 @@ func (l *LoginHandler) Login(w http.ResponseWriter, r *http.Request) {
resp.Message = err.Error()
} else {
if hashing.CheckPasswordHash(req.Password, user.Password) {
lastLogin := time.Now()
var tokGen utility.TokenGenerator
secretKey := config.GetSecretKey()
tokGen.SetSecretKey(secretKey)
@@ -83,9 +85,16 @@ func (l *LoginHandler) Login(w http.ResponseWriter, r *http.Request) {
statusCode = http.StatusInternalServerError
resp.Message = "Error generating token"
} else {
statusCode = http.StatusOK
resp.Data = append(resp.Data, *myToken)
resp.Message = "Successful"
log.Println("Updating user's last login")
if rowsAffected, err := l.UserStore.UpdateLastLogin(ctx, user.Id, lastLogin); err != nil {
statusCode = http.StatusInternalServerError
resp.Message = err.Error()
} else {
log.Println("Rows updated:", rowsAffected)
statusCode = http.StatusOK
resp.Data = append(resp.Data, *myToken)
resp.Message = "Successful"
}
}
} else {
statusCode = http.StatusNotFound
+13 -3
View File
@@ -1,7 +1,9 @@
package handler
import (
"log"
"net/http"
"time"
"git.kundeng.us/phoenix/textsender-models/tx0/token"
"git.kundeng.us/phoenix/textsender-models/tx0/user"
@@ -153,6 +155,7 @@ func (s *ServiceHandler) Login(w http.ResponseWriter, r *http.Request) {
statusCode = http.StatusInternalServerError
resp.Message = "Not valid"
} else {
lastLogin := time.Now()
var tokGen utility.TokenGenerator
tokGen.SetHourOffset(8)
secretKey := config.GetSecretKey()
@@ -162,9 +165,16 @@ func (s *ServiceHandler) Login(w http.ResponseWriter, r *http.Request) {
statusCode = http.StatusInternalServerError
resp.Message = err.Error()
} else {
statusCode = http.StatusOK
resp.Data = append(resp.Data, myToken)
resp.Message = "Successful"
log.Println("Updating service user's last login")
if rowsAffected, err := s.ServiceStore.UpdateLastLogin(ctx, serviceUser.Id, lastLogin); err != nil {
statusCode = http.StatusInternalServerError
resp.Message = err.Error()
} else {
log.Println("Rows updated:", rowsAffected)
statusCode = http.StatusOK
resp.Data = append(resp.Data, myToken)
resp.Message = "Successful"
}
}
}
}
+22
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"sync"
"time"
"git.kundeng.us/phoenix/textsender-models/tx0/user"
"github.com/google/uuid"
@@ -101,3 +102,24 @@ func (m *MockServiceUserStore) GetWithId(ctx context.Context, id uuid.UUID) (*us
return nil, nil
}
func (m *MockServiceUserStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.Error != nil {
return 0, m.Error
}
serviceUser, exists := m.ServiceUsers[id]
if !exists {
return 0, errors.New("Service user not found")
}
serviceUser.LastLogin = &lastLogin
m.ServiceUsers[id] = serviceUser
m.ServiceUsersByUsername[serviceUser.Username] = serviceUser
return 1, nil
}
+21
View File
@@ -136,3 +136,24 @@ func (m *MockUserStore) UpdatePassword(ctx context.Context, id uuid.UUID, passwo
return 1, nil
}
func (m *MockUserStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.Error != nil {
return 0, m.Error
}
user, exists := m.Users[id]
if !exists {
return 0, errors.New("User not found")
}
user.LastLogin = &lastLogin
m.Users[id] = user
m.UsersByUsername[user.Username] = user
return 1, nil
}
+12
View File
@@ -3,6 +3,7 @@ package store
import (
"context"
"fmt"
"time"
"git.kundeng.us/phoenix/textsender-models/tx0/user"
"github.com/google/uuid"
@@ -10,11 +11,13 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
)
// TODO: Rename this to ServiceUserStore
type ServiceStore interface {
CheckWithUsername(ctx context.Context, username string) (bool, error)
GetWithUsername(ctx context.Context, username string) (*user.ServiceUser, error)
GetWithId(ctx context.Context, id uuid.UUID) (*user.ServiceUser, error)
Create(ctx context.Context, serviceUser *user.ServiceUser) error
UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error)
}
type PGServiceStore struct {
@@ -81,3 +84,12 @@ func (s *PGServiceStore) Create(ctx context.Context, serviceUser *user.ServiceUs
&serviceUser.Id, &serviceUser.Created,
)
}
func (s *PGServiceStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error) {
query := `UPDATE service_users SET last_login = $1 WHERE id = $2`
if affected, err := s.db.Exec(ctx, query, lastLogin, id); err != nil {
return 0, err
} else {
return affected.RowsAffected(), nil
}
}
+11
View File
@@ -3,6 +3,7 @@ package store
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
@@ -18,6 +19,7 @@ type UserStore interface {
GetAllUsers(ctx context.Context) ([]*user.User, error)
UserExists(ctx context.Context, username string) (bool, error)
UpdatePassword(ctx context.Context, id uuid.UUID, password string) (int64, error)
UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error)
}
type PGUserStore struct {
@@ -123,3 +125,12 @@ func (s *PGUserStore) UpdatePassword(ctx context.Context, id uuid.UUID, password
return affected.RowsAffected(), nil
}
}
func (s *PGUserStore) UpdateLastLogin(ctx context.Context, id uuid.UUID, lastLogin time.Time) (int64, error) {
query := `UPDATE users SET last_login = $1 WHERE id = $2`
if affected, err := s.db.Exec(ctx, query, lastLogin, id); err != nil {
return 0, err
} else {
return affected.RowsAffected(), nil
}
}