You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js添加测试管理员账号,解决登录‘未找到注册管理员’错误

问题描述

作为React和Node.js新手,在管理员登录时收到错误提示No registered admin found,已知是系统中无管理员数据导致。现需在Node.js代码中添加测试用管理员邮箱和密码,相关代码及错误信息如下:

登录逻辑代码

import { hash } from "../../../core/crypto/hash.js";
import { webToken } from "../../../core/crypto/web_token.js";
import { errors } from "../../../core/classes/errors.js";

import { adminsService } from "../../admins/index.js";

export const adminLogin = async (creds) => {
  try {
    const admin = await adminsService.getWithCredentials(
      creds.email.toLowerCase()
    );

    if (!admin) {
      throw new errors.Authentication("No registered admin found", "Auth");
    }

    if (!(await hash.verify(creds.password, admin.password))) {
      throw new errors.Authentication("Password doesn't match.", "Auth");
    }

    const token = webToken.generate({ uid: admin.id, role: "admin" }, "10h");

    return { name: admin.name, email: admin.email, token };
  } catch (error) {
    throw error;
  }
};

错误信息

authentication No registered admin found
Authentication {
   type: 'authentication',
   message: 'No registered admin found',
   from: 'Auth'
}

解决方案

要添加测试用管理员账号,需通过adminsService创建管理员数据,具体步骤如下:

  1. 确认管理员服务的创建方法
    查看../../admins/index.js中的adminsService,找到用于创建管理员的方法(通常名为create、add或createAdmin),该方法需接收包含name、email、password的对象参数。

  2. 编写初始化测试管理员的代码
    可在项目启动脚本(如server.js)中添加初始化逻辑,或单独写一个脚本执行一次:

import { hash } from "./core/crypto/hash.js";
import { adminsService } from "./services/admins/index.js";

// 初始化测试管理员
const initTestAdmin = async () => {
  // 先哈希密码,避免明文存储
  const hashedPassword = await hash.hash("test1234");
  const testAdmin = {
    name: "测试管理员",
    email: "admin@test.com",
    password: hashedPassword
  };

  // 检查是否已存在该管理员,避免重复创建
  const existingAdmin = await adminsService.getWithCredentials(testAdmin.email.toLowerCase());
  if (!existingAdmin) {
    await adminsService.createAdmin(testAdmin); // 替换为实际的创建方法名
    console.log("测试管理员已创建");
  } else {
    console.log("测试管理员已存在");
  }
};

// 服务器启动时执行初始化
initTestAdmin().catch(err => console.error("初始化测试管理员失败:", err));
  1. 使用测试账号登录
    现在可以用邮箱admin@test.com和密码test1234进行登录,即可通过验证。

  2. 注意事项

  • 生产环境务必删除测试账号,替换为正式管理员账号。
  • 密码必须经过哈希处理后存储,禁止明文保存,若你的hash模块哈希方法名为其他(如generate),请对应修改代码。

内容的提问来源于stack exchange,提问作者Misba Ayat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 21:21:25