Nestjs Auth logo

Nestjs Auth

Organization
nestjs
nestjs-auth

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.

Overview

Publishernestjs
Repositoryagent-skills
Skill namenestjs-auth
Stars
176
Forks
5
Bundled files
2
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 2 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by nestjs on GitHub. Read the source before you install it.

Installation

Install the Nestjs Auth AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/nestjs/agent-skills/tree/master/plugins/nestjs/skills/nestjs-auth
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/nestjs/agent-skills.git /tmp/agent-skills
mkdir -p .claude/skills
cp -r /tmp/agent-skills/plugins/nestjs/skills/nestjs-auth .claude/skills/nestjs-auth
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Nestjs Auth in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Nestjs Auth on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Nestjs Auth is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Authentication and authorization in NestJS

Use the first-party packages rather than assembling auth from @nestjs/jwt, Passport and bcrypt:

bash
npm i @nestjs/authentication @nestjs/authorization
  • @nestjs/authentication (docs: https://docs.nestjs.com/security/authentication.md): a global guard that puts every route behind sign-in, @Public() to open one, @Authenticate({ optional: true }), @CurrentUser(), PasswordHasher (scrypt), sessions, TokenService (JWT access + rotating refresh tokens), ApiKeyProvider, TOTP, magic links, OIDC. No third-party dependencies.
  • @nestjs/authorization (docs: https://docs.nestjs.com/security/authorization.md): policies as providers, @Can(Policy, 'ability') on routes, AuthorizationService.authorize() in services. Denies a route that declares no check.

Rules that matter

  • Check passwords with PasswordHasher.verify(password, found?.passwordHash), and only then decide. With no user it checks a dummy hash, so an unknown email takes as long as a wrong password. Never if (!user) throw before the hash check: the faster answer tells anyone which emails are registered. Hash with passwordHasher.hash(); inject PasswordHasher (the module provides it).
  • Sign-in is @Post() + @HttpCode(200): it creates nothing, so not 201.
  • accessToken.key must be at least 32 bytes for HS256 (openssl rand -base64 32); a shorter one stops the app at startup. Never hash, pad or derive a longer key from a short secret: tokens are then signed with a different key, and anything that verifies them with the configured secret rejects them.
  • When the task fixes the secret or the token format (a given JWT_SECRET, a default value, tokens other services verify), keep it exactly. If it's under 32 bytes, sign and verify with @nestjs/jwt using that secret, and plug verification into @nestjs/authentication as your own AuthenticationProvider, so the global guard, @Public() and @CurrentUser() still work. Say in your summary that the secret is too short for production. Code: references/authentication.md.
  • Refresh tokens rotate and detect theft. TokenService.refresh() spends each refresh token; a reused one is a 401 and revokes its whole family, so the client must sign in again. Access tokens stay valid until they expire: keep ttl short.
  • Services read the user from AuthenticationContext (requireUser()), a singleton backed by AsyncLocalStorage: no request-scoped providers, and run({ user }, fn) for workers and tests.
  • Load the user on every request in the provider's validate()/authenticate(), and read roles from it, not from token claims: a role change or a deleted user then takes effect at once.
  • Global guards run in import order: import AuthenticationModule before AuthorizationModule (the app refuses to start otherwise). A custom guard that sets request.user goes in as APP_GUARD before AuthorizationModule.
  • @nestjs/authorization denies every route that declares nothing: once AuthorizationModule is imported, a signed-in user gets 403 on any handler without @Can(), @Can.Anyone() or @Public() (the startup log lists them). Adding it to an existing app, put @Can.Anyone() on routes any signed-in user may call (e.g. GET /me, the user's own resources) and re-test them.
  • 401 vs 403: no or bad credentials is 401 (the guard's), signed in but not allowed is 403 (@Can() or ForbiddenException).
  • Streams authenticate once. An open SSE or WebSocket connection keeps its user after the token, API key or session is revoked, or the user is deleted. It is easy to miss. End the stream when its credential is revoked: takeUntil() on a revocation signal, shown in nestjs-feature's references/http.md.
  • Response bodies. The guard's 401 is an HttpException ({ statusCode, message, errorCode } plus a WWW-Authenticate header): a global exception filter can reshape it like any other error.
  • Cost. At its default cost a hash takes ~0.3 s and 128 MiB, on the libuv threadpool. Tests override it: .overrideProvider(PasswordHasher).useValue(new PasswordHasher({ logN: 10 })). Throttle sign-in (nestjs-production).
  • In-memory stores. Sessions and refresh tokens live in memory until you register a store, and with NODE_ENV=production the module refuses to start that way. A bearer-only API can turn refresh tokens off: refreshToken: false.

Pick the setup

The app needsUse
Bearer JWTs it issues itself, secret up to youaccessToken options + JwtBearerProvider + TokenService.issue()
A secret or token format fixed by the task@nestjs/jwt + your own AuthenticationProvider
Browser sessionsSessionCookieProvider + SignInService.signIn()
Keys for scripts and partnersApiKeyProvider (keys prefix_<id>_<secret>, stored as SHA-256) or your own provider for another header; manage keys only from a session (@Authenticate({ providers: [SessionAuth] })), so a leaked key can't mint more
Roles or ownership checks@nestjs/authorization policies

Code for each row, checked against @nestjs/authentication 0.1.6 and @nestjs/authorization 0.1.4: references/authentication.md, references/authorization.md.

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Nestjs Auth AI skill do?

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.

Why use Nestjs Auth on TypingMind?

Because you install it once and use it with any model. Nestjs Auth is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Nestjs Auth in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/nestjs/agent-skills/tree/master/plugins/nestjs/skills/nestjs-auth. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Nestjs Auth?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Nestjs Auth?

As many as you like. As long as a model supports skills, you can use Nestjs Auth with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Nestjs Auth AI skill free?

Yes. It is published on GitHub by nestjs under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇