apps/api: add type BaseAuthController
This commit is contained in:
parent
4971605821
commit
69665a4831
@ -25,10 +25,11 @@ import { cookieOptions } from 'src/config/cookie';
|
||||
import { env } from 'src/config/env';
|
||||
import { Credentials } from 'src/dto/credentials';
|
||||
import { Account } from 'src/schemas/account.schema';
|
||||
import type { BaseAuthController } from 'src/types/auth-controller';
|
||||
|
||||
@Controller('account')
|
||||
@ApiTags('account')
|
||||
export class AccountController {
|
||||
export class AccountController implements BaseAuthController {
|
||||
constructor(private readonly accountService: AccountService) {}
|
||||
|
||||
private clearCookies(req, reply) {
|
||||
@ -102,7 +103,11 @@ export class AccountController {
|
||||
}
|
||||
|
||||
@Post('/login')
|
||||
async login(@Body() credentials: Credentials, @Res() reply: FastifyReply) {
|
||||
async login(
|
||||
@Body() credentials: Credentials,
|
||||
@Req() _req: FastifyRequest,
|
||||
@Res() reply: FastifyReply
|
||||
) {
|
||||
try {
|
||||
const token = await this.accountService.login(credentials);
|
||||
|
||||
|
||||
@ -19,11 +19,12 @@ import { ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { cookieOptions } from 'src/config/cookie';
|
||||
import { env } from 'src/config/env';
|
||||
import type { BaseAuthController } from 'src/types/auth-controller';
|
||||
import { User } from 'src/utils/ldap';
|
||||
|
||||
@Controller('ldap')
|
||||
@ApiTags('ldap')
|
||||
export class LdapController {
|
||||
export class LdapController implements BaseAuthController {
|
||||
cookieOptions: CookieSerializeOptions;
|
||||
constructor(private readonly ldapService: LdapService) {}
|
||||
|
||||
@ -42,19 +43,11 @@ export class LdapController {
|
||||
status: HttpStatus.OK,
|
||||
})
|
||||
async login(
|
||||
@Req() req: FastifyRequest,
|
||||
@Body() credentials: Credentials,
|
||||
@Req() _req: FastifyRequest,
|
||||
@Res() reply: FastifyReply
|
||||
) {
|
||||
const twoFactor = req.headers['tfa'] === 'telegram';
|
||||
|
||||
try {
|
||||
if (twoFactor) {
|
||||
const user = await this.ldapService.getUser(credentials.login);
|
||||
|
||||
return reply.status(200).send(user);
|
||||
}
|
||||
|
||||
const token = await this.ldapService.login(credentials);
|
||||
|
||||
return reply.setCookie(env.COOKIE_TOKEN_NAME, token, cookieOptions).status(200).send();
|
||||
|
||||
8
apps/api/src/types/auth-controller.ts
Normal file
8
apps/api/src/types/auth-controller.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import type { Credentials } from 'src/dto/credentials';
|
||||
|
||||
export type BaseAuthController = {
|
||||
getUser: (req: FastifyRequest, reply: FastifyReply) => Promise<never>;
|
||||
login: (credentials: Credentials, req: FastifyRequest, reply: FastifyReply) => Promise<never>;
|
||||
logout: (req: FastifyRequest, reply: FastifyReply) => Promise<never>;
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user