mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	fix: fix default token not created in some cases (#1510)
* 修复git、微信等用户注册不会创建默认令牌问题 修复git、微信等用户注册不会创建默认令牌问题 * 修复git、微信等用户注册不会创建默认令牌问题 删除普通用户注册代码 * fix: do not block if error happened --------- Co-authored-by: JustSong <songquanpeng@foxmail.com>
This commit is contained in:
		@@ -6,8 +6,6 @@ import (
 | 
			
		||||
	"github.com/songquanpeng/one-api/common"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/config"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/ctxkey"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/helper"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/logger"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/random"
 | 
			
		||||
	"github.com/songquanpeng/one-api/model"
 | 
			
		||||
	"net/http"
 | 
			
		||||
@@ -111,7 +109,6 @@ func Logout(c *gin.Context) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Register(c *gin.Context) {
 | 
			
		||||
	ctx := c.Request.Context()
 | 
			
		||||
	if !config.RegisterEnabled {
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{
 | 
			
		||||
			"message": "管理员关闭了新用户注册",
 | 
			
		||||
@@ -176,28 +173,7 @@ func Register(c *gin.Context) {
 | 
			
		||||
		})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	go func() {
 | 
			
		||||
		err := user.ValidateAndFill()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			logger.Errorf(ctx, "user.ValidateAndFill failed: %w", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		cleanToken := model.Token{
 | 
			
		||||
			UserId:         user.Id,
 | 
			
		||||
			Name:           "default",
 | 
			
		||||
			Key:            random.GenerateKey(),
 | 
			
		||||
			CreatedTime:    helper.GetTimestamp(),
 | 
			
		||||
			AccessedTime:   helper.GetTimestamp(),
 | 
			
		||||
			ExpiredTime:    -1,
 | 
			
		||||
			RemainQuota:    -1,
 | 
			
		||||
			UnlimitedQuota: true,
 | 
			
		||||
		}
 | 
			
		||||
		err = cleanToken.Insert()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			logger.Errorf(ctx, "cleanToken.Insert failed: %w", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	c.JSON(http.StatusOK, gin.H{
 | 
			
		||||
		"success": true,
 | 
			
		||||
		"message": "",
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"github.com/songquanpeng/one-api/common"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/blacklist"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/config"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/helper"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/logger"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/random"
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
@@ -140,6 +141,22 @@ func (user *User) Insert(inviterId int) error {
 | 
			
		||||
			RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("邀请用户赠送 %s", common.LogQuota(config.QuotaForInviter)))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// create default token
 | 
			
		||||
	cleanToken := Token{
 | 
			
		||||
		UserId:         user.Id,
 | 
			
		||||
		Name:           "default",
 | 
			
		||||
		Key:            random.GenerateKey(),
 | 
			
		||||
		CreatedTime:    helper.GetTimestamp(),
 | 
			
		||||
		AccessedTime:   helper.GetTimestamp(),
 | 
			
		||||
		ExpiredTime:    -1,
 | 
			
		||||
		RemainQuota:    -1,
 | 
			
		||||
		UnlimitedQuota: true,
 | 
			
		||||
	}
 | 
			
		||||
	result.Error = cleanToken.Insert()
 | 
			
		||||
	if result.Error != nil {
 | 
			
		||||
		// do not block
 | 
			
		||||
		logger.SysError(fmt.Sprintf("create default token for user %d failed: %s", user.Id, result.Error.Error()))
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user