40 lines
999 B
TypeScript
40 lines
999 B
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Query,
|
|
Session,
|
|
HttpException,
|
|
HttpStatus,
|
|
} from '@nestjs/common';
|
|
import { UsersService } from './users.service';
|
|
import { LoginDto } from './dto/login.dto';
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
|
|
@ApiTags('auth')
|
|
@Controller()
|
|
export class UsersController {
|
|
constructor(private readonly usersService: UsersService) {}
|
|
|
|
@Get('/login')
|
|
async login(
|
|
@Query('code') code: string,
|
|
@Session() session: Record<string, any>,
|
|
): Promise<LoginDto> {
|
|
await this.usersService.createDiscordToken({ code }, session);
|
|
const user = await this.usersService.discordLogin(session);
|
|
return user;
|
|
}
|
|
|
|
@Get('/whoami')
|
|
async remove(@Session() session: Record<string, any>): Promise<LoginDto> {
|
|
if (!session.tokenData) {
|
|
throw new HttpException(
|
|
"Session doesn't contains auth information",
|
|
HttpStatus.UNAUTHORIZED,
|
|
);
|
|
}
|
|
const user = await this.usersService.discordLogin(session);
|
|
return user;
|
|
}
|
|
}
|