Template
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Query,
|
|
Session,
|
|
HttpException,
|
|
HttpStatus,
|
|
Redirect,
|
|
} from '@nestjs/common';
|
|
import { UsersService } from './users.service';
|
|
import { UserResponseDTO } from './dto/userresponse.dto';
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { SessionData } from 'express-session';
|
|
|
|
const configService = new ConfigService();
|
|
|
|
@ApiTags('auth')
|
|
@Controller()
|
|
export class UsersController {
|
|
constructor(private readonly usersService: UsersService) {}
|
|
|
|
@Get('/login')
|
|
@Redirect()
|
|
async login(@Query('code') code: string, @Session() session: SessionData) {
|
|
await this.usersService.createDiscordToken({ code }, session);
|
|
await this.usersService.discordLogin(session);
|
|
return { url: configService.get<string>('CLIENT_ENDPOINT') };
|
|
}
|
|
|
|
@Get('/logout')
|
|
@Redirect()
|
|
async logout(@Session() session: SessionData) {
|
|
await this.usersService.discordLogout(session);
|
|
return { url: configService.get<string>('CLIENT_ENDPOINT') };
|
|
}
|
|
|
|
@Get('/whoami')
|
|
async remove(@Session() session: SessionData): Promise<UserResponseDTO> {
|
|
if (!session.tokenData) {
|
|
throw new HttpException(
|
|
"Session doesn't contains auth information",
|
|
HttpStatus.UNAUTHORIZED,
|
|
);
|
|
}
|
|
await this.usersService.discordLogin(session);
|
|
return new UserResponseDTO(session.user);
|
|
}
|
|
}
|