12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const moment = require('moment');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const jwt = require('jsonwebtoken');
- const uuid = require('uuid');
- class LoginService extends CrudService {
- constructor(ctx) {
- super(ctx, 'login');
- this.model = this.ctx.model.User;
- }
- async login({ login_id, password }) {
- assert(login_id, '请输入登录用户名');
- assert(password, '请输入密码');
- const user = await this.checkUser(login_id, password);
- const jwt_user = await this.createJwt(user);
- const state = uuid();
- const key = `free:auth:state:${state}`;
- await this.app.redis.set(key, jwt_user, 'EX', 60 * 60 * 24);
- return { key };
- }
- async checkUser(login_id, password) {
- const user = await this.model.findOne({ login_id }, '+password');
- if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST, '用户不存在');
- if (user.password.secret !== password) {
- throw new BusinessError(ErrorCode.BAD_PASSWORD, '用户密码错误');
- }
- return JSON.parse(JSON.stringify(user));
- }
- // 创建登录Token
- async createJwt({ id, login_id, ...info }) {
- info = _.omit(info, [ '_v', 'meta' ]);
- const { secret, expiresIn = '1d' } = this.config.jwt;
- const subject = login_id;
- const res = { id, login_id, ...info };
- const token = await jwt.sign(res, secret, { expiresIn, subject });
- return token;
- }
- // 取得redis内token信息
- async token({ key }) {
- assert(key, 'key不能为空');
- const token = await this.app.redis.get(key);
- if (!token) {
- throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
- }
- // 转换成用户信息,给过去
- let res = jwt.decode(token);
- res = _.pick(res, [ 'login_id', '_tenant', 'id', 'name', 'role', 'status' ]);
- return res;
- }
- // 删除操作
- async destroy({ key }) {
- const res = await this.app.redis.del(key);
- return res;
- }
- }
- module.exports = LoginService;
|