WIp - fix login into central
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
UnauthorizedException,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import { IsEmail, IsString, MinLength, IsOptional } from 'class-validator';
|
||||
import { AuthService } from './auth.service';
|
||||
@@ -40,17 +41,36 @@ class RegisterDto {
|
||||
export class AuthController {
|
||||
constructor(private authService: AuthService) {}
|
||||
|
||||
private isCentralSubdomain(subdomain: string): boolean {
|
||||
const centralSubdomains = (process.env.CENTRAL_SUBDOMAINS || 'central,admin').split(',');
|
||||
return centralSubdomains.includes(subdomain);
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@Post('login')
|
||||
async login(@TenantId() tenantId: string, @Body() loginDto: LoginDto) {
|
||||
if (!tenantId) {
|
||||
throw new UnauthorizedException('Tenant ID is required');
|
||||
async login(
|
||||
@TenantId() tenantId: string,
|
||||
@Body() loginDto: LoginDto,
|
||||
@Req() req: any,
|
||||
) {
|
||||
const subdomain = req.raw?.subdomain;
|
||||
|
||||
console.log('subdomain:' + subdomain);
|
||||
|
||||
console.log('CENTRAL_SUBDOMAINS:', process.env.CENTRAL_SUBDOMAINS);
|
||||
|
||||
// If it's a central subdomain, tenantId is not required
|
||||
if (!subdomain || !this.isCentralSubdomain(subdomain)) {
|
||||
if (!tenantId) {
|
||||
throw new UnauthorizedException('Tenant ID is required');
|
||||
}
|
||||
}
|
||||
|
||||
const user = await this.authService.validateUser(
|
||||
tenantId,
|
||||
loginDto.email,
|
||||
loginDto.password,
|
||||
subdomain,
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
@@ -64,9 +84,15 @@ export class AuthController {
|
||||
async register(
|
||||
@TenantId() tenantId: string,
|
||||
@Body() registerDto: RegisterDto,
|
||||
@Req() req: any,
|
||||
) {
|
||||
if (!tenantId) {
|
||||
throw new UnauthorizedException('Tenant ID is required');
|
||||
const subdomain = req.raw?.subdomain;
|
||||
|
||||
// If it's a central subdomain, tenantId is not required
|
||||
if (!subdomain || !this.isCentralSubdomain(subdomain)) {
|
||||
if (!tenantId) {
|
||||
throw new UnauthorizedException('Tenant ID is required');
|
||||
}
|
||||
}
|
||||
|
||||
const user = await this.authService.register(
|
||||
@@ -75,6 +101,7 @@ export class AuthController {
|
||||
registerDto.password,
|
||||
registerDto.firstName,
|
||||
registerDto.lastName,
|
||||
subdomain,
|
||||
);
|
||||
|
||||
return user;
|
||||
|
||||
Reference in New Issue
Block a user