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

相关推荐
千寻技术帮2 小时前
10361_基于Springboot的哈尔滨旅游管理系统
数据库·spring boot·mysql·毕业设计·旅游
TG:@yunlaoda360 云老大3 小时前
华为云国际站代理商GES的图引擎服务有哪些优势?
服务器·数据库·华为云
2501_946230988 小时前
Cordova&OpenHarmony通知中心实现
android·javascript
南山安8 小时前
JavaScript 函数柯里化:从入门到实战,一文搞定(面试可用)
javascript·面试·函数式编程
谢尔登8 小时前
Monorepo 架构
前端·arcgis·架构
啃火龙果的兔子8 小时前
JavaScript 中的 Symbol 特性详解
开发语言·javascript·ecmascript
栀秋6669 小时前
你会先找行还是直接拍平?两种二分策略你Pick哪个?
前端·javascript·算法
Coder_Boy_9 小时前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
漂流瓶jz9 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·css
xhxxx9 小时前
传统工具调用太痛苦?LangChain 一键打通 LLM 与真实世界
前端·langchain·llm