flipped-aurora/gin-vue-admin · error

用户不存在

Error message

用户不存在

What it means

CreateApiToken issues a custom-lifetime JWT for automation, but first verifies the target user exists and Preloads their Authorities. If the query on sys_users by apiToken.UserID returns any error (record not found or DB error), it returns "用户不存在" without issuing a token.

Source

Thrown at server/service/system/sys_api_token.go:19

package system

import (
	"context"
	"errors"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	sysReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/golang-jwt/jwt/v5"
	"time"
)

type ApiTokenService struct{}

func (apiVersion *ApiTokenService) CreateApiToken(ctx context.Context, apiToken system.SysApiToken, days int) (string, error) {
	var user system.SysUser
	if err := global.GVA_DB.WithContext(ctx).Preload("Authorities").Where("id = ?", apiToken.UserID).First(&user).Error; err != nil {
		return "", errors.New("用户不存在")
	}

	hasAuth := false
	for _, auth := range user.Authorities {
		if auth.AuthorityId == apiToken.AuthorityID {
			hasAuth = true
			break
		}
	}
	if !hasAuth && user.AuthorityId != apiToken.AuthorityID {
		return "", errors.New("用户不具备该角色权限")
	}

	j := &utils.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一不同的部分是过期时间

	expireTime := time.Duration(days) * 24 * time.Hour
	if days == -1 {
		expireTime = 100 * 365 * 24 * time.Hour

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Verify the user ID exists (SELECT id FROM sys_users WHERE id = ?) and use a valid one.
  2. Re-create the user account if it was deleted, then request the token.
  3. Confirm you are pointing at the intended database (config mismatch between environments).

Example fix

// before
svc.CreateApiToken(ctx, system.SysApiToken{UserID: 9999, AuthorityID: 888}, 30)

// after: resolve the user by username first
var user system.SysUser
if err := global.GVA_DB.WithContext(ctx).Where("username = ?", "ci-bot").First(&user).Error; err != nil {
    return err
}
svc.CreateApiToken(ctx, system.SysApiToken{UserID: user.ID, AuthorityID: 888}, 30)
Defensive patterns

Strategy: validation

Validate before calling

const user = await getUserById({ id: form.userId })
if (!user?.data?.user?.ID) throw new Error('目标用户不存在,无法签发 API token')

Try / catch

try {
  const token = await createApiToken(form)
} catch (e) {
  if (String(e?.msg).includes('用户不存在')) {
    ElMessage.error('用户 ID 无效:请确认该账号存在且未在当前环境被删除')
  } else { throw e }
}

Prevention

When it happens

Trigger: Calling the API-token creation endpoint with a UserID that does not exist in sys_users (deleted user, wrong ID, or an ID from a different database environment).

Common situations: Creating a CI token after the user account was deleted; copying a user ID from a staging dump into production; a hard-coded UserID in a script left over from a rebuilt database.

Understand the failure class

Background: "User not found", "Invalid user", and "does not exist": what missing-user lookup errors mean across Rocket.Chat, LiteLLM, Phabricator, rustfs, and pnpm — this error's family across 10 libraries.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/630110980b25472d. Report an issue: GitHub.