Loading...
Loading...
Sign-in, sessions, bearer tokens, API keys and permissions in a NestJS application with the first-party @nestjs/authentication and @nestjs/authorization packages - timing-safe password checks, a global guard with @Public(), policies with @Can(), and what to do when a task fixes the JWT secret or token format. Use when adding or changing authentication or authorization in a NestJS application.
npx skill4agent add nestjs/agent-skills nestjs-auth@nestjs/jwtnpm i @nestjs/authentication @nestjs/authorization@nestjs/authentication@Public()@Authenticate({ optional: true })@CurrentUser()PasswordHasherTokenServiceApiKeyProvider@nestjs/authorization@Can(Policy, 'ability')AuthorizationService.authorize()PasswordHasher.verify(password, found?.passwordHash)if (!user) throwpasswordHasher.hash()PasswordHasher@Post()@HttpCode(200)accessToken.keyopenssl rand -base64 32JWT_SECRET@nestjs/jwt@nestjs/authenticationAuthenticationProvider@Public()@CurrentUser()TokenService.refresh()ttlAuthenticationContextrequireUser()AsyncLocalStoragerun({ user }, fn)validate()authenticate()AuthenticationModuleAuthorizationModulerequest.userAPP_GUARDAuthorizationModule@nestjs/authorizationAuthorizationModule@Can()@Can.Anyone()@Public()@Can.Anyone()GET /me@Can()ForbiddenExceptiontakeUntil()nestjs-featurereferences/http.mdHttpException{ statusCode, message, errorCode }WWW-Authenticate.overrideProvider(PasswordHasher).useValue(new PasswordHasher({ logN: 10 }))nestjs-productionNODE_ENV=productionrefreshToken: false| The app needs | Use |
|---|---|
| Bearer JWTs it issues itself, secret up to you | |
| A secret or token format fixed by the task | |
| Browser sessions | |
| Keys for scripts and partners | |
| Roles or ownership checks | |
@nestjs/authentication@nestjs/authorization