monorepo-microservice-rbac/apps/services/aorta/gateway/src/auth/auth.controller.ts
2023-10-10 16:48:34 +08:00

56 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Body, Controller, Inject, Post, Req, Res } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { UserLoginDto } from './auth.dto';
import { firstValueFrom } from 'rxjs';
import { Response, Request } from 'express';
@Controller('auth')
export class AuthController {
constructor(@Inject('Client') private readonly client: ClientProxy) {}
/**
* 业务系统登录接口
* 1.对于登录的dto对象由于业务的不同有可能存在dto对象的不同因此校验逻辑一定是放在业务的登录接口
* 2.除了对参数进行校验,此外还需要对用户的身份进行校验:角色(是否属于这个系统)、可用(isEnabled)
* 3.签发token
*/
@Post('signIn')
async auth(
@Req() request: Request,
@Body() userLoginDto: UserLoginDto,
@Res({ passthrough: true }) res: Response,
) {
const { username, password, phoneNumber } = userLoginDto;
// TODO: isEnabled、phoneNumber用于后续验证
const { isLegal, data, msg } = await firstValueFrom(
this.client.send('cert.user.account', { username, password }),
);
await firstValueFrom(
this.client.send('logger.user.signIn', {
platform: 'dmp',
username,
finger: request.headers['x-finger'] as string,
finger2: request.headers['x-finger2'] as string,
isLegal,
}),
);
if (isLegal) {
// 签发token
const { token } = await firstValueFrom(
this.client.send('cert.token.create', { username }),
);
const { tokenKeyInCookie, expires } = await firstValueFrom(
this.client.send('cert.token.config', []),
);
res.cookie(tokenKeyInCookie, token, {
maxAge: expires * 1000,
httpOnly: true,
});
const { password, ...rest } = data;
return { code: 'ok', data: rest, msg: '登陆成功' };
} else {
return { code: 'fail', msg };
}
}
}