fix(user): assign snowflake IDs on registration

The HTTP register path left ID at 0, so SQLite/GORM filled a
serial primary key. CreateUser now generates a snowflake ID when
none is set, matching admin create and OAuth signup.
This commit is contained in:
ryan
2026-09-02 18:41:37 +08:00
parent 05606dfb56
commit 353a5f9f75
2 changed files with 7 additions and 1 deletions
@@ -166,6 +166,8 @@ func TestUserLoginHTTPHandler(t *testing.T) {
IsActive: true,
}
require.NoError(t, user.CreateUser(context.Background(), plainUser))
require.NotZero(t, plainUser.ID)
require.Greater(t, plainUser.ID, uint64(10000), "user id must be snowflake, not sqlite autoincrement")
reqBodyPlain := `{"username":"plain_admin","password":"12345678"}`
reqPlain, _ := http.NewRequest(http.MethodPost, "/api/v1/user/login", bytes.NewBufferString(reqBodyPlain))
+5 -1
View File
@@ -6,6 +6,7 @@ package user
import (
"Wavelet/core"
"Wavelet/core/contracts"
"Wavelet/pkg/idgen"
"Wavelet/pkg/util"
"context"
"errors"
@@ -82,8 +83,11 @@ func GetUserByEmail(ctx context.Context, email string) (*User, error) {
return &u, nil
}
// CreateUser 创建用户
// CreateUser 创建用户。ID 为空时用雪花算法分配,避免 SQLite/GORM 把 0 当成自增主键。
func CreateUser(ctx context.Context, u *User) error {
if u != nil && u.ID == 0 {
u.ID = idgen.NextUint64ID()
}
return getDB(ctx).Create(u).Error
}