47 lines
1.5 KiB
Go
47 lines
1.5 KiB
Go
|
|
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
|
|||
|
|
}
|