a073af76fe
- Implemented ConfigController for handling config-related requests. - Created ConfigService for business logic related to configurations. - Added CreateConfigDto for validating configuration data. feat: add good categories module with controller and service - Implemented GoodCategoriesController for managing good categories. - Created GoodCategoriesService for business logic related to good categories. - Added CreateGoodCategoryDto for validating good category data. feat: add goods module with controller and service - Implemented GoodsController for managing goods. - Created GoodsService for business logic related to goods. - Added CreateGoodDto for validating good data. feat: add profile module with controller and service - Implemented ProfileController for managing user profiles. - Created ProfileService for business logic related to profiles. - Added CreateProfileDto for profile data structure. feat: add sales invoice payments module with controller and service - Implemented SalesInvoicePaymentsController for managing invoice payments. - Created SalesInvoicePaymentsService for business logic related to payments. - Added CreateSalesInvoicePaymentDto for validating payment data. feat: add service categories module with controller and service - Implemented ServiceCategoriesController for managing service categories. - Created ServiceCategoriesService for business logic related to service categories. - Added CreateServiceCategoryDto for validating service category data. feat: add services module with controller and service - Implemented ServicesController for managing services. - Created ServicesService for business logic related to services. - Added CreateServiceDto for validating service data. feat: add trigger logs module with controller and service - Implemented TriggerLogsController for managing trigger logs. - Created TriggerLogsService for business logic related to trigger logs. - Added CreateTriggerLogDto for validating trigger log data. feat: add uploaders module for handling file uploads - Implemented UploadersController for managing file uploads. - Created ImageUploaderService for image upload logic. - Created UploaderService for file handling and storage.
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import {
|
|
CanActivate,
|
|
ExecutionContext,
|
|
Injectable,
|
|
UnauthorizedException,
|
|
} from '@nestjs/common'
|
|
import { Reflector } from '@nestjs/core'
|
|
import { JwtService } from '@nestjs/jwt'
|
|
import { IS_PUBLIC_KEY } from '../../common/decorators/public.decorator'
|
|
import { IWithJWTPayloadRequest } from '../../common/models/token-model'
|
|
|
|
@Injectable()
|
|
export class JwtAuthGuard implements CanActivate {
|
|
constructor(
|
|
private jwt: JwtService,
|
|
private reflector: Reflector,
|
|
) {}
|
|
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
context.getHandler(),
|
|
context.getClass(),
|
|
])
|
|
if (isPublic) return true
|
|
|
|
const req = context.switchToHttp().getRequest<IWithJWTPayloadRequest>()
|
|
|
|
// Log headers to inspect whether Authorization is present
|
|
|
|
// Try token from cookie first, then from Authorization header (Bearer)
|
|
let token: string | undefined = req.cookies?.accessToken
|
|
if (!token) {
|
|
const authHeader = (req.headers.authorization || req.headers.Authorization) as
|
|
| string
|
|
| undefined
|
|
if (authHeader && authHeader.startsWith('Bearer ')) {
|
|
token = authHeader.slice(7)
|
|
}
|
|
}
|
|
|
|
if (!token) throw new UnauthorizedException('Missing access token')
|
|
|
|
try {
|
|
const payload = this.jwt.verify(token, {
|
|
secret: process.env.JWT_SECRET || 'secret',
|
|
})
|
|
|
|
console.log(payload)
|
|
|
|
if (payload.type !== 'POS')
|
|
throw new UnauthorizedException('Invalid or expired token')
|
|
|
|
// Set the typed dataPayload to the request
|
|
req.dataPayload = payload
|
|
|
|
return true
|
|
} catch (err) {
|
|
console.log(err)
|
|
throw new UnauthorizedException('Invalid or expired token')
|
|
}
|
|
}
|
|
}
|