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 判断比较简洁 只需要查询一次数据表 就可以

相关推荐
captain3763 小时前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
jktantan3 小时前
Vue keep-alive 切标签后滚动位置回顶?因为 onDeactivated 里读到的 scrollTop 是 0
前端·javascript·vue.js
总要学点什么3 小时前
第89篇:三维特效-螺旋圆效果——让螺旋臂卷起一圈圈外扩的涟漪
开发语言·javascript·ecmascript·webgl·cesium·空间分析·1024程序员节
只睡四小时4 小时前
MCP 弃用 SSE?Node 分帧漏 81.7% 事件
node.js·webassembly·实时通信·mcp·streamable http
小此方4 小时前
Re:Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
xszivvvv4 小时前
Mariadb数据库frm文件异常,如何恢复数据
数据库·mariadb
2601_962885724 小时前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
excel4 小时前
encodeURI 和 encodeURIComponent 有什么区别?
前端
smile_life_4 小时前
MySQL 设置 防止账户被暴力破解
数据库·mysql
前端精髓4 小时前
调用openrouter的免费API模型
java·前端·javascript