Implementing SMS panel in Super Admin
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
||||
|
||||
export const RemoteTenant = createParamDecorator(
|
||||
(_data: unknown, ctx: ExecutionContext) => {
|
||||
const req = ctx.switchToHttp().getRequest();
|
||||
return req.remoteTenant;
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class RemoteTokenGuard implements CanActivate {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
const header = String(req.headers?.authorization || '');
|
||||
const match = /^Bearer\s+(.+)$/i.exec(header.trim());
|
||||
const token = match?.[1]?.trim();
|
||||
|
||||
if (!token || token.length < 16) {
|
||||
throw new UnauthorizedException('توکن نسخه نامعتبر است');
|
||||
}
|
||||
|
||||
const tenant = await this.prisma.tenant.findFirst({
|
||||
where: {
|
||||
remoteApiToken: token,
|
||||
isActive: true,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
nameFa: true,
|
||||
domain: true,
|
||||
product: true,
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!tenant) {
|
||||
throw new UnauthorizedException('توکن نسخه نامعتبر است');
|
||||
}
|
||||
|
||||
req.remoteTenant = tenant;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user