cool node.js 后端接口实现账号密码登录和注册

1.实现H5 的账号密码 登录和注册功能

2.登录和注册代码

controller

复制代码
@Post('/h5Login', { summary: 'H5登录' })
  async LoginByH5(@Body() body) {
    const res: any = await this.businessLoginService.H5Login(body);
    return this.ok(res);
  }

  @Post('/h5Register', { summary: 'H5注册' })
  async RegisterByH5(@Body() body) {
    const res: any = await this.businessLoginService.H5Register(body);
    return this.ok(res);
  }

service

复制代码
 // H5 登录

  async H5Login(login) {
    if (!login.password || !login.phone) {
      throw new CoolCommException('参数不能为空~');
    }
    const user = await this.businessStudentEntity.findOneBy({
      phone: login?.phone,
      password: md5(login?.password),
    });
    if (!user) {
      // if (user.password !== md5(login.password)) {
      //   // 校验用户状态及密码
      //   throw new CoolCommException('账户或密码不正确~');
      // }
      // if (!user.phone) {
      //   throw new CoolCommException('账户不存在~');
      // }
      throw new CoolCommException('账户或密码不正确~');
    } else {
      // 生成token
      const { expire, refreshExpire } = this.coolConfig.jwt.token;
      const result = {
        expire,
        token: await this.generateToken(user, expire),
        refreshExpire,
        refreshToken: await this.generateToken(user, refreshExpire, true),
      };

      // 将用户相关信息保存到缓存
      await this.cacheManager.set(`business:token:${user.id}`, result.token, {
        ttl: expire,
      });
      await this.cacheManager.set(
        `business:token:refresh:${user.id}`,
        result.token,
        { ttl: refreshExpire }
      );

      return { token: result.token, user };
    }
  }

  //H5注册
  async H5Register(register) {
    if (
      !register.phone ||
      !register.password ||
      !register.confirmPassword ||
      !register.yzmCode ||
      !register.registerCode
    ) {
      throw new CoolCommException('参数不能为空~');
    }
    const phoneRegex = /^1[3-9]\d{9}$/; // 手机号码的正则表达式

    if (!phoneRegex.test(register.phone)) {
      throw new CoolCommException('手机号码格式不正确~');
    }
    if (register.password !== register.confirmPassword) {
      throw new CoolCommException('两次密码不一致');
    }
    const inviteCode = await this.businessUserEntity.findOneBy({
      inviteCode: register.registerCode,
    });
    if (!inviteCode) {
      throw new CoolCommException('导师不存在~');
    }
    const user = await this.businessStudentEntity.findOneBy({
      phone: register?.phone,
    });
    if (user) {
      throw new CoolCommException('账户已存在~');
    }

    await this.businessStudentEntity.save({
      phone: register.phone,
      password: md5(register.password),
      membershipLevel: 0,
      balance: 0,
      userId: inviteCode.id,
      nickName: `蹲票用户${register.phone}`,
    });
    return 1;
  }
相关推荐
胡萝卜术7 小时前
力扣5. 最长回文子串
前端·javascript·面试
我星期八休息7 小时前
网络编程—应用层HTTP协议
linux·运维·开发语言·前端·网络·网络协议·http
不好听6138 小时前
前端路由完全指南(下篇):懒加载、History 栈与工程化实践
前端·react.js
触底反弹8 小时前
🔥 保姆级教程|SSE + BFF + 跨域三件套,从零实现 ChatGPT 流式输出(附完整代码)
人工智能·node.js·vite
触底反弹9 小时前
🔥 从零搭建 RAG 知识库:爬虫→分词→向量化→检索,一步都不能错
javascript·人工智能·面试
圣光SG10 小时前
Java Web入门基础知识笔记
java·前端·笔记
cyforkk11 小时前
Vercel 绑定自定义域名极简配置指南
服务器·前端·网络
এ慕ོ冬℘゜12 小时前
JavaScript 数组核心方法实战|新增元素 + 数组转字符串 零基础详解
开发语言·javascript·ecmascript
IT_陈寒12 小时前
React useEffect依赖数组中埋的坑,这次终于让我逮到了
前端·人工智能·后端