agentleFS
manish1803manish1803/nextjs-fullstack-skills/skills/02-fullstack-backend/auth-authorization/SKILL.md

auth-authorization

Skill2 starsChanged 4 months agoMITView on GitHub

Implement authentication (login, session/JWT, OAuth, magic links) and authorization (roles, permissions, route protection) in full-stack React/Next.js apps. Use whenever the user asks about login, sign-up, sessions, JWT, OAuth providers, protecting routes, role-based access control, or "who can see/do what" in the app.

2 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If JWTs are required (e.g.

    line 13

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Authentication & Authorization Skill

Authentication: prefer a managed/battle-tested library over hand-rolled

Default recommendation for Next.js: Auth.js (NextAuth), Clerk, or Better Auth — hand-rolling password hashing, session handling, and OAuth flows is a security liability unless there's a specific reason to. If the user insists on custom auth, still use vetted primitives (bcrypt/argon2 for hashing, jose for JWT) and never invent your own crypto.

Session strategy

  • Cookie-based sessions (httpOnly, secure, sameSite=lax/strict) are the default for web apps — not localStorage-stored JWTs, which are vulnerable to XSS token theft.
  • If JWTs are required (e.g. for a separate API consumed by mobile), keep them short-lived (~15 min) with a refresh token rotated and stored httpOnly, not in localStorage/sessionStorage.
  • Validate the session on the server on every protected request — never trust a client-supplied "isLoggedIn" flag.

Route protection (Next.js App Router)

// middleware.ts
export function middleware(req: NextRequest) {
  const session = getSessionFromCookie(req);
  if (!session && isProtectedRoute(req.nextUrl.pathname)) {
    return NextResponse.redirect(new URL('/login', req.url));
  }
}
export const config = { matcher: ['/dashboard/:path*', '/settings/:path*'] };

Also re-check auth inside the Server Component/Server Action itself (defense in depth) — middleware alone isn't a substitute for checking session before returning sensitive data.

Authorization (RBAC/permissions)

  • Model as role (coarse: admin/member/viewer) + optional fine-grained permissions array for anything beyond simple role tiers.
  • Enforce authorization at the data-access layer (inside the service function that queries the DB), not only in the UI — hiding a button is not access control.
async function getOrgSettings(userId: string, orgId: string) {
  const membership = await getMembership(userId, orgId);
  if (!membership || membership.role !== 'admin') throw new ForbiddenError();
  return db.orgSettings.findUnique({ where: { orgId } });
}
  • For multi-tenant apps, scope every query by orgId/tenantId derived from the session — never trust a tenant ID passed from the client without cross-checking membership.

OAuth integration checklist

  • Redirect URIs registered exactly (including protocol/trailing slash) per environment (dev/staging/prod)
  • State parameter validated to prevent CSRF on the OAuth callback
  • Minimum necessary scopes requested
  • Provider-returned email verified/trusted only if the provider confirms email verification

Anti-patterns to flag

  • JWTs or session tokens stored in localStorage
  • Authorization checks only in the frontend (hiding UI ≠ securing data)
  • Password hashing with plain SHA-256/MD5 instead of bcrypt/argon2
  • Missing tenant/org scoping on a query in a multi-tenant app (cross-tenant data leak)

Example prompt this skill should trigger on

"Add login with email/password and Google OAuth, and make sure only admins can access the billing settings page."

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.