web-service/apps/freeblox-web-service/src/services/auth/auth.controller.ts

87 lines
2.5 KiB
TypeScript

import {
Body,
ClassSerializerInterceptor,
Controller,
Get,
Inject,
Ip,
Post,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import {
ApiBearerAuth,
ApiOkResponse,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { LoginDto } from './dtos/login.dto';
import { User } from '../../decorators/user.decorator';
import { UserInfo } from '@freeblox/shared';
import { lastValueFrom } from 'rxjs';
import { AuthGuard } from '../../guards/auth.guard';
import { UserDto } from './dtos/user.dto';
import { ChallengeResponseDto } from './dtos/challenge-response.dto';
import { LoginByRefreshTokenDto } from './dtos/login-refresh-token.dto';
import { ChallengeRequestDto } from './dtos/challenge-request.dto';
import { Throttle } from '@nestjs/throttler';
@Controller({
version: '1',
path: 'auth',
})
@ApiBearerAuth()
@ApiTags('Auth')
@UseInterceptors(ClassSerializerInterceptor)
export class AuthController {
constructor(@Inject('auth') private auth: ClientProxy) {}
@Post('login')
@Throttle(3, 60)
@ApiOperation({ summary: 'Login by username or email and password' })
@ApiOkResponse({ type: ChallengeResponseDto })
async login(@Body() body: LoginDto, @Ip() ip: string) {
return this.auth.send('auth.login', { body, ip });
}
@Post('challenge')
@Throttle(3, 60)
@ApiOperation({ summary: 'Login by challenge' })
@ApiOkResponse({ type: ChallengeResponseDto })
async challenge(@Body() body: ChallengeRequestDto, @Ip() ip: string) {
return this.auth.send('auth.loginByChallenge', {
challenge: body.challenge,
secret: body.secret,
body: body.body,
ip,
});
}
@Post('refresh')
@Throttle(3, 60)
@ApiOperation({ summary: 'Login by refresh token' })
@ApiOkResponse({ type: ChallengeResponseDto })
async refresh(@Body() body: LoginByRefreshTokenDto, @Ip() ip: string) {
return this.auth.send('auth.loginByRefreshToken', {
token: body.token,
ip,
});
}
@Get('me')
@ApiOperation({ summary: 'Current user information' })
@ApiOkResponse({ type: UserDto })
@UseGuards(AuthGuard)
async myInfo(@User() user: UserInfo): Promise<UserDto> {
return lastValueFrom(this.auth.send('auth.getUserById', { id: user.sub }));
}
@Get('bans')
@ApiOperation({ summary: 'Current user ban history' })
@UseGuards(AuthGuard)
async banInfo(@User() user: UserInfo) {
return lastValueFrom(this.auth.send('auth.getUserBans', { user }));
}
}