شعار 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 يُحمّله النموذج عند الحاجة، فلا يستهلك توكنات إلا عندما يَرِد طلب ذو صلة فعلًا.

  • تعمل مع أي نموذج لغوي كبير

    مهارات الذكاء الاصطناعي مكتوبة بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا تعمل مع GPT أو Claude أو Gemini أو Grok أو نموذج محلي.

  • الملفات المرفقة: 2

    نصوص برمجية وقوالب ومراجع يمكن للنموذج قراءتها أثناء عمله. الملفات للقراءة فقط ولا تُنفّذ أبدًا.

  • مفتوحة المصدر

    نشرها nestjs على GitHub. اطّلع على المصدر قبل تثبيتها.

التثبيت

ثبّت مهارة الذكاء الاصطناعي Nestjs Auth في TypingMind لاستخدامها مع أي نموذج لغوي كبير، أو أضفها إلى وكيل ذكاء اصطناعي آخر يقرأ SKILL.md.

1

التثبيت في TypingMind

يُثبّت TypingMind المهارة مباشرةً من مجلدها على GitHub — فيقرأ SKILL.md، ويجمع ملفات الموارد في حزمة، ويحفظ النتيجة محليًا.

  1. افتح التطبيق وانتقل إلى «الإضافات» ثم «المهارات».
  2. اختر «تثبيت من GitHub».
  3. الصق رابط مجلد المهارة الموضّح أدناه وأكّد التثبيت.
    https://github.com/nestjs/agent-skills/tree/master/plugins/nestjs/skills/nestjs-auth
  4. فعّل المهارة في كل محادثة تريد إتاحتها فيها.
الإضافات ← المهارات ← إضافة مهارة ← من رابط GitHub، ثم الصق رابط المجلد واضغط على «متابعة».
2

التثبيت في وكيل ذكاء اصطناعي آخر

يمكن لأي وكيل ذكاء اصطناعي يقرأ تنسيق Agent Skills استخدام هذه المهارة — انسخ المجلد إلى مجلد المهارات الخاص بذلك الوكيل.

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

فعّل Nestjs Auth في أي محادثة على TypingMind وسيتولى النموذج الباقي. تتضمن مطالبة النظام اسمها ووصفها، وبمجرد ورود طلب ذي صلة، يُحمّل النموذج التعليمات الكاملة تلقائيًا — فلا تحتاج إلى استدعائها يدويًا، ولا تستهلك أي توكنات حتى تُستخدم فعليًا.

يُحمّل النموذج مهارة Nestjs Auth تلقائيًا بمجرد ورود طلب ذي صلة بها.

تعمل مع أي نموذج ذكاء اصطناعي

مهارات الذكاء الاصطناعي هي تعليمات بصيغة Markdown وليست شيفرة خاصة بمزوّد معيّن، لذا لا ترتبط Nestjs Auth بالنموذج الذي كُتبت له. ثبّتها مرة واحدة في TypingMind واستخدمها مع GPT-5 أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو نموذج محلي تشغّله بنفسك — كل ذلك باستخدام مفاتيح API الخاصة بك.

  • تُحمَّل عند الحاجة فقط

    تتضمن مطالبة النظام الاسم والوصف فقط. وتُجلب التعليمات عند أول طلب ذي صلة، لذا لا تستهلك تعليمات المهارة أي توكنات ما دامت المهارة غير مستخدمة.

  • بدّل النماذج أثناء المحادثة

    لأن المهارة تعليمات وليست شيفرة، فإن تغيير النموذج لا يعطّلها — فالنموذج التالي يقرأ ملف 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.

الملفات المرفقة

يقرأ النموذج هذه الملفات عند الحاجة ما دامت المهارة محمّلة. وهي متاحة للقراءة ولا تُنفّذ أبدًا.

الأسئلة الشائعة

ماذا تفعل مهارة الذكاء الاصطناعي 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 Auth على TypingMind؟

لأنك تثبّتها مرة واحدة وتستخدمها مع أي نموذج. مهارة Nestjs Auth عبارة عن نص Markdown وليست شيفرة خاصة بمزود بعينه، لذا تعمل المهارة نفسها مع GPT-5 أو Claude أو Gemini أو Grok أو نموذج محلي، ويمكنك تبديل النموذج أثناء المحادثة دون تعطّل المهارة. يعمل TypingMind بمفاتيح API الخاصة بك، فتدفع للمزودين مباشرةً بدلًا من اشتراك لكل مقعد، وتبقى مهاراتك ومحادثاتك في مساحة التخزين الخاصة بك.

كيف أثبّت Nestjs Auth في TypingMind؟

في TypingMind، افتح «الإضافات»، ثم «المهارات»، ثم اختر «التثبيت من GitHub» والصق https://github.com/nestjs/agent-skills/tree/master/plugins/nestjs/skills/nestjs-auth. يقرأ TypingMind ملف SKILL.md الخاص بالمهارة، ويجمع ملفاتها ويثبّتها كمهارة يمكنك تفعيلها لكل محادثة على حدة.

ما نماذج الذكاء الاصطناعي التي يمكنها استخدام Nestjs Auth؟

أي نموذج تربطه في TypingMind. مهارات الذكاء الاصطناعي عبارة عن تعليمات Markdown وليست شيفرة خاصة بمزود بعينه، لذا يمكن لكل من GPT وClaude وGemini وGrok والنماذج المحلية تحميل هذه المهارة عندما يتوافق الطلب معها.

كم نموذج ذكاء اصطناعي يمكنني استخدامه مع Nestjs Auth؟

أي عدد تريده. ما دام النموذج يدعم المهارات، يمكنك استخدام Nestjs Auth معه، سواء كان GPT أو Claude أو Gemini أو Grok أو DeepSeek أو Mistral أو Llama أو غيرها، وكل ذلك على TypingMind باستخدام مفاتيح API الخاصة بك.

هل مهارة الذكاء الاصطناعي Nestjs Auth مجانية؟

نعم. نشرها nestjs على GitHub بموجب ترخيص MIT. لا تدفع إلا لمزود الذكاء الاصطناعي الخاص بك مقابل التوكنات التي تستخدمها.

ما هي مهارات الذكاء الاصطناعي؟

مهارة الذكاء الاصطناعي حزمة تعليمات قابلة لإعادة الاستخدام تعلّم نموذج الذكاء الاصطناعي كيفية تنفيذ مهمة محددة. وتتبع تنسيق Agent Skills المفتوح: ملف SKILL.md يتضمن اسمًا ووصفًا، إلى جانب أي نصوص برمجية أو قوالب أو ملفات مرجعية قد يحتاج إليها النموذج. لا يقرأ النموذج التعليمات إلا عندما يتوافق طلبك مع المهارة، لذا لا تترتب على المهارة المثبتة أي تكلفة حتى تُستخدم.

كيف تختلف مهارات الذكاء الاصطناعي عن الإضافات أو موصلات MCP؟

تمنح الإضافة أو موصل MCP النموذج أدوات جديدة يمكنه استدعاؤها، وهي شيفرة تُنفَّذ في مكان ما وتُرجع نتيجة. أما مهارة الذكاء الاصطناعي فتزوّد النموذج بالمعرفة ومنهجية العمل: كيفية التعامل مع مهمة، والخطوات التي ينبغي اتباعها، وما ينبغي أن تكون عليه المخرجات الجيدة. المهارات عبارة عن نصوص Markdown، لذا لا تحتاج إلى خادم أو مفتاح API أو بيئة تشغيل، وتعمل مع أي نموذج.

عرض الكل

أنشئ مساحة عملك للذكاء الاصطناعي الآن

اشترك في نشرتنا الإخبارية لتصلك أخبار الميزات الجديدة والهدايا القادمة 👇