From af73e78aa3cd2ac68f5b87b8cb9f7e9fc3b350ef Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sun, 14 Jun 2026 16:54:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9A=E4=B9=89=20UserRepository=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=8F=8A=20User=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/store/user.go | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 backend/internal/store/user.go diff --git a/backend/internal/store/user.go b/backend/internal/store/user.go new file mode 100644 index 0000000..40dcb78 --- /dev/null +++ b/backend/internal/store/user.go @@ -0,0 +1,46 @@ +package store + +import ( + "context" + "errors" + "time" +) + +var ( + ErrUserNotFound = errors.New("user not found") + ErrUsernameTaken = errors.New("username already taken") + ErrRefreshTokenNotFound = errors.New("refresh token not found") +) + +// UserRepository 用户持久化接口。 +type UserRepository interface { + // Create 创建用户,返回生成的 ID。 + Create(ctx context.Context, username, passwordHash string) (string, error) + + // FindByUsername 按用户名查找,不存在返回 ErrUserNotFound。 + FindByUsername(ctx context.Context, username string) (*User, error) + + // FindByID 按 ID 查找,不存在返回 ErrUserNotFound。 + FindByID(ctx context.Context, id string) (*User, error) + + // SaveRefreshToken 保存 refresh token hash。 + SaveRefreshToken(ctx context.Context, userID, tokenHash string, expiresAt time.Time) error + + // FindRefreshToken 按 token hash 查找,返回 user_id。不存在返回 ErrRefreshTokenNotFound。 + FindRefreshToken(ctx context.Context, tokenHash string) (string, error) + + // DeleteRefreshToken 按 token hash 删除。 + DeleteRefreshToken(ctx context.Context, tokenHash string) error + + // DeleteUserRefreshTokens 删除用户的所有 refresh token(登出所有设备)。 + DeleteUserRefreshTokens(ctx context.Context, userID string) error +} + +// User 用户数据模型(store 层)。 +type User struct { + ID string + Username string + PasswordHash string + CreatedAt time.Time + UpdatedAt time.Time +}