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

相关推荐
PythonFun1 分钟前
WPS单元格中如何分别设置中英字体为不同字号
数据库·mysql·wps
GISer_Jing4 分钟前
大语言模型Agent入门指南
前端·数据库·人工智能
多仔ヾ11 分钟前
Vue.js 前端开发实战之 10-网络请求和 UI 组件库
vue.js
Mikhail_G12 分钟前
Mysql数据库操作指南——数据库(零基础篇)
大数据·数据库·sql·mysql·数据分析
运筹vivo@15 分钟前
BUUCTF: [极客大挑战 2019]Upload
前端·web安全·php·ctf
qq_124987075318 分钟前
基于Spring Boot的长春美食推荐管理系统的设计与实现(源码+论文+部署+安装)
java·前端·spring boot·后端·毕业设计·美食·计算机毕业设计
运筹vivo@19 分钟前
攻防世界: easyupload
前端·web安全·php·ctf
沉淅尘23 分钟前
Context Engineering: 优化大语言模型性能的关键策略与艺术
数据库·人工智能·语言模型
重生之绝世牛码30 分钟前
Linux软件安装 —— Elasticsearch集群安装(带密码验证)
大数据·linux·运维·数据库·elasticsearch·软件安装
敲敲千反田36 分钟前
redis哨兵和缓存
数据库·redis·缓存