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 分钟前
electron-vite 安装better-sqlite3
javascript·数据库·electron
大明者省7 分钟前
Ubuntu22.04 宝塔面板与 XFCE 远程桌面端口兼容性分析
运维·服务器·数据库·笔记
@菜菜_达10 分钟前
jquery.inputmask插件介绍
前端·javascript·jquery
QuZhengRong10 分钟前
【Luck-Report】缓存
java·前端·后端·vue·excel
jiayong2315 分钟前
前端面试题库 - 浏览器与网络篇
前端·网络·面试
Csvn19 分钟前
小程序开发:微信小程序与 uni-app 实战指南
前端
摸鱼小李上线了25 分钟前
vue项目页面添加水印实现方法
前端·javascript·vue.js
砍材农夫31 分钟前
物联网 基于netty构建mqtt协议规范(主题通配符订阅)
java·前端·javascript·物联网·netty
liudanzhengxi32 分钟前
巧用ULN2003A轻松扩展单片机IO口
数据库·mongodb
彩票管理中心秘书长35 分钟前
智能体状态指示:何时思考、何时调用工具、何时出错
前端·后端·程序员