Nestjs Authのロゴ

Nestjs Auth

組織
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.

概要

公開者nestjs
リポジトリagent-skills
スキル名nestjs-auth
スター
176
フォーク
5
同梱ファイル
2
ライセンスMIT
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 2個の同梱ファイル

    モデルが作業中に読み取れるスクリプト、テンプレート、参考資料です。ファイルは読み取り専用で、実行されることはありません。

  • オープンソース

    nestjsがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキル「Nestjs Auth」をTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/nestjs/agent-skills/tree/master/plugins/nestjs/skills/nestjs-auth
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

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
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでNestjs Authを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあるとすぐに、モデルが自動的にNestjs Authを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Nestjs Authは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、スキルを使わない間は指示の読み込みにトークンを消費しません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

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.

同梱ファイル

スキルが読み込まれている間、モデルは必要に応じてこれらを読み取ります。読み取り可能なファイルとして提供され、実行されることはありません。

よくある質問

AIスキル「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.

TypingMindでNestjs Authを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Nestjs Authはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにNestjs Authをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/nestjs/agent-skills/tree/master/plugins/nestjs/skills/nestjs-authを貼り付けてください。TypingMindがSKILL.mdを読み込み、関連ファイルをまとめて、チャットごとに有効にできるスキルとしてインストールします。

Nestjs Authを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストの内容がこのスキルの用途に合ったときに、このスキルを読み込めます。

Nestjs Authで使えるAIモデルの数に制限はありますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもNestjs Authを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Nestjs Auth」は無料ですか?

はい。nestjsがMITライセンスのもとでGitHubに公開しています。ご利用のAIプロバイダーに支払うのは、使用したトークンの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇