Logo Nestjs Auth

Nestjs Auth

Tổ chức
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.

Tổng quan

Nhà phát hànhnestjs
Kho mãagent-skills
Tên kỹ năngnestjs-auth
Sao
176
Bản fork
5
Tệp đi kèm
2
Giấy phépMIT
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 2 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được nestjs công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Nestjs Auth trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/nestjs/agent-skills/tree/master/plugins/nestjs/skills/nestjs-auth
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân 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
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Nestjs Auth trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Nestjs Auth ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Nestjs Auth với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

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.

Tệp đi kèm

Khi kỹ năng đã được tải, mô hình có thể đọc các tệp này nếu cần. Các tệp này chỉ dùng để đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Nestjs Auth làm được gì?

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.

Vì sao nên dùng Nestjs Auth trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Nestjs Auth là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Nestjs Auth trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/nestjs/agent-skills/tree/master/plugins/nestjs/skills/nestjs-auth. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm rồi cài đặt cả gói dưới dạng một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Nestjs Auth?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Nestjs Auth với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Nestjs Auth với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Nestjs Auth có miễn phí không?

Có. Kỹ năng này được nestjs phát hành trên GitHub theo giấy phép MIT. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇