56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
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 };
|
||
}
|
||
}
|
||
}
|