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

相关推荐
yqcoder2 分钟前
CSS 外边距重叠(Margin Collapsing):现象、原理与完美解决方案
前端·css
快乐非自愿33 分钟前
Redis--SDS字符串与集合的底层实现原理
数据库·redis·缓存
这儿有一堆花1 小时前
住宅代理(Residential Proxy)技术指南
开发语言·数据库·php
山楂树の1 小时前
图像标注大坑:img图片 + Canvas 叠加标注,同步放大后标注位置偏移、对不齐?详解修复方案及亚像素处理原理
前端·css·学习·canva可画
本山德彪1 小时前
我做了一个拼豆图纸生成器,把照片秒变图纸
前端
茉莉玫瑰花茶1 小时前
LangChain 核心组件 [ 2 ]
java·数据库·langchain
DTrader1 小时前
用TS无法实盘量化? - 实盘均线策略
前端·api
进击的夸父1 小时前
vfojs:Vue 超集架构,外壳React灵魂Vue
前端
编程老船长1 小时前
解决不同项目需要不同 Node.js 版本的问题
前端·vue.js
Wect1 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·算法·typescript