forked from hangshuo652/aurak
0a9588abb7
- Add pagination support to findAll (page, limit query params) - Add findByTemplateId method to service - Add GET /by-template/:templateId endpoint to controller - Service already includes CRUD for QuestionBank and QuestionBankItem
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { lastValueFrom, Observable } from 'rxjs';
|
|
import { IS_PUBLIC_KEY } from './public.decorator';
|
|
import { tenantStore } from '../tenant/tenant.store';
|
|
|
|
@Injectable()
|
|
export class JwtAuthGuard extends AuthGuard('jwt') implements CanActivate {
|
|
constructor(private reflector: Reflector) {
|
|
super();
|
|
}
|
|
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
context.getHandler(),
|
|
context.getClass(),
|
|
]);
|
|
if (isPublic) {
|
|
return true;
|
|
}
|
|
|
|
const result = await super.canActivate(context);
|
|
let canActivate = false;
|
|
|
|
if (result instanceof Observable) {
|
|
canActivate = await lastValueFrom(result);
|
|
} else {
|
|
canActivate = result;
|
|
}
|
|
|
|
if (canActivate) {
|
|
const request = context.switchToHttp().getRequest();
|
|
const user = request.user;
|
|
if (user) {
|
|
const store = tenantStore.getStore();
|
|
if (store) {
|
|
store.tenantId = user.tenantId;
|
|
store.userId = user.id;
|
|
}
|
|
}
|
|
}
|
|
return canActivate;
|
|
}
|
|
}
|