refactor(nestjs): hook up auth endpoints

This commit is contained in:
Ahmed Bouhuolia
2025-05-08 18:10:02 +02:00
parent 401b3dc111
commit f78d6efe27
26 changed files with 304 additions and 111 deletions

View File

@@ -10,11 +10,15 @@ import { Reflector } from '@nestjs/core';
import { IS_PUBLIC_ROUTE } from '../Auth/Auth.constants';
export const IS_IGNORE_TENANT_INITIALIZED = 'IS_IGNORE_TENANT_INITIALIZED';
export const IgnoreTenantInitializedRoute = () => SetMetadata(IS_IGNORE_TENANT_INITIALIZED, true);
export const IgnoreTenantInitializedRoute = () =>
SetMetadata(IS_IGNORE_TENANT_INITIALIZED, true);
@Injectable()
export class EnsureTenantIsInitializedGuard implements CanActivate {
constructor(private readonly tenancyContext: TenancyContext, private reflector: Reflector) {}
constructor(
private readonly tenancyContext: TenancyContext,
private reflector: Reflector,
) {}
/**
* Validate the tenant of the current request is initialized..
@@ -22,10 +26,11 @@ export class EnsureTenantIsInitializedGuard implements CanActivate {
* @returns {Promise<boolean>}
*/
async canActivate(context: ExecutionContext): Promise<boolean> {
const isIgnoreEnsureTenantInitialized = this.reflector.getAllAndOverride<boolean>(
IS_IGNORE_TENANT_INITIALIZED,
[context.getHandler(), context.getClass()],
);
const isIgnoreEnsureTenantInitialized =
this.reflector.getAllAndOverride<boolean>(IS_IGNORE_TENANT_INITIALIZED, [
context.getHandler(),
context.getClass(),
]);
const isPublic = this.reflector.getAllAndOverride<boolean>(
IS_PUBLIC_ROUTE,
[context.getHandler(), context.getClass()],

View File

@@ -11,11 +11,15 @@ import { Reflector } from '@nestjs/core';
import { IS_PUBLIC_ROUTE } from '../Auth/Auth.constants';
export const IS_IGNORE_TENANT_SEEDED = 'IS_IGNORE_TENANT_SEEDED';
export const IgnoreTenantSeededRoute = () => SetMetadata(IS_IGNORE_TENANT_SEEDED, true);
export const IgnoreTenantSeededRoute = () =>
SetMetadata(IS_IGNORE_TENANT_SEEDED, true);
@Injectable()
export class EnsureTenantIsSeededGuard implements CanActivate {
constructor(private readonly tenancyContext: TenancyContext, private reflector: Reflector) {}
constructor(
private readonly tenancyContext: TenancyContext,
private reflector: Reflector,
) {}
/**
* Validate the tenant of the current request is seeded.
@@ -27,15 +31,16 @@ export class EnsureTenantIsSeededGuard implements CanActivate {
IS_PUBLIC_ROUTE,
[context.getHandler(), context.getClass()],
);
const isIgnoreEnsureTenantSeeded = this.reflector.getAllAndOverride<boolean>(
IS_IGNORE_TENANT_SEEDED,
[context.getHandler(), context.getClass()],
);
const isIgnoreEnsureTenantSeeded =
this.reflector.getAllAndOverride<boolean>(IS_IGNORE_TENANT_SEEDED, [
context.getHandler(),
context.getClass(),
]);
if (isPublic || isIgnoreEnsureTenantSeeded) {
return true;
}
const tenant = await this.tenancyContext.getTenant();
if (!tenant.seededAt) {
throw new UnauthorizedException({
message: 'Tenant database is not seeded with initial data yet.',

View File

@@ -49,6 +49,6 @@ export class TenancyContext {
// Get the user from the request headers.
const userId = this.cls.get('userId');
return this.systemUserModel.query().findOne({ id: userId });
return this.systemUserModel.query().findById(userId);
}
}

View File

@@ -49,7 +49,7 @@ export class TenantUser extends TenantBaseModel {
* Relationship mapping.
*/
static get relationMappings() {
const Role = require('models/Role');
const { Role } = require('../../../Roles/models/Role.model');
return {
/**
@@ -57,7 +57,7 @@ export class TenantUser extends TenantBaseModel {
*/
role: {
relation: Model.BelongsToOneRelation,
modelClass: Role.default,
modelClass: Role,
join: {
from: 'users.roleId',
to: 'roles.id',