@firas-dahmani/common-auth
v1.0.0
Published
Reusable JWT + roles guards and decorators for Rebody microservices
Readme
2.1 Install yarn add @rebody/common-auth
make sure these exist at the service root (same versions everywhere)
yarn add @nestjs/jwt jsonwebtoken
2.2 Add env vars (HS256 shared secret mode)
Use the same secret your auth-ms uses to sign access tokens:
JWT_ACCESS_SECRET=... JWT_ISSUER=rebody-auth JWT_AUDIENCE=rebody
2.3 Register module once (AppModule) import { Module } from '@nestjs/common'; import { CommonAuthModule } from '@rebody/common-auth';
@Module({ imports: [ CommonAuthModule.register({ jwt: { secret: process.env.JWT_ACCESS_SECRET!, issuer: process.env.JWT_ISSUER, audience: process.env.JWT_AUDIENCE, algorithms: ['HS256'], }, globalGuard: true, }), ], }) export class AppModule {}
2.4 Use in controllers import { Controller, Get } from '@nestjs/common'; import { AuthNotRequired, CurrentAuth, Roles } from '@rebody/common-auth';
@Controller('products') export class ProductsController { @Get('public') @AuthNotRequired() listPublic() { return { ok: true }; }
@Get('me') me(@CurrentAuth() auth: any) { return { sub: auth.sub, role: auth.role }; }
@Get('admin') @Roles('ADMIN') adminOnly() { return { ok: true }; } }
That’s it. No Prisma dependency. No duplicated Role enum. Role is just a string from JWT.
