cool Node 框架后端接口 登录接口涉及登录以及权限的判断逻辑

1.实现前端登录功能 但是同时可能还得判断当前的账号有没有权限登录 这样的逻辑 代码 不能让代码特别冗余 也不能有太多查询表操作 浪费资源

2.实现

①contrller

复制代码
@Post('/h5Login', { summary: 'H5登录' })
  async LoginByH5(@Body() body) {
    const res: any = await this.businessLoginService.H5Login(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) {
      throw new CoolCommException('账户或密码不正确~');
    }
    if (user.isEnabled == 0) {
      throw new CoolCommException('账户没有权限~');
    }
    const { expire, refreshExpire } = this.coolConfig.jwt.token;
    const result = {
      expire,
      token: await this.generateTokenClient(user, expire),
      refreshExpire,
      refreshToken: await this.generateTokenClient(user, refreshExpire, true),
    };
    await this.cacheManager.set(
      `business:client:token:${user.id}`,
      result.token,
      { ttl: expire }
    );
    await this.cacheManager.set(
      `business:client:token:refresh:${user.id}`,
      result.refreshToken,
      { ttl: refreshExpire }
    );
    return result;
  }

3.说明

拿我这个代码来说,我这个时node 的操作 跟java中的接口写法差不多 但是代码和格式有根本的区别 这样的service 判断比较简洁 只需要查询一次数据表 就可以

相关推荐
粥里有勺糖1 小时前
安利一下最近用的桌面“Agent” | T3 Code
前端·github·ai编程
xy34531 小时前
Axure 9.0 动态面板基本操作
前端·ui·html·axure·原型·产品设计
不停喝水1 小时前
【前端转全栈java速通课】 项目实战④7-11节 操作数据库-登录-注册-修改密码-注销-mubatis-plus简化crud-
java·前端·数据库
逍遥5966070101 小时前
网络设备交换机路由器 AI 配置助手
node.js·硬件工程
leisoo80971 小时前
融资融券数据怎么查两融指标含义与杠杆观察方法 IG50免费开源股票数据API接口
开发语言·jvm·数据库·python·json
Aric_Jones2 小时前
pgsql内置publication/subscription功能,实现数据同步
数据库
有想法的py工程师2 小时前
PostgreSQL 字符集双重转义击穿 磁盘 I/O
数据库·postgresql
一水鉴天2 小时前
统一对象拓扑(UOT)设计全链定稿:从三件事分离到三继承 之1 20261010(豆包“查看聊天内容”继续)
前端·css
头发还在的女程序员2 小时前
【无标题】
网络·数据库·短剧小程序·短剧系统·海外短剧·短剧后台