user-access

Read the authenticated user via useAuth and ApiHandlerContext.userId.

Updated Mar 28, 2025
One-click install
npx skills add https://github.com/gileck/app-template-ai --skill user-access
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: user-access
Source: https://github.com/gileck/app-template-ai/tree/main/.ai/skills/template/user-access
Command: npx skills add https://github.com/gileck/app-template-ai --skill user-access

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Accessing the current authenticated user in both client UI code and server API handlers is essential for personalizing experiences and enforcing user-based access control.

Core Features & Use Cases

  • Client-side: Retrieve the user via useAuth() and access user.id for UI decisions.
  • Server-side: Read context.userId from ApiHandlerContext to authorize requests and fetch user-specific data.
  • Use Case: Display personalized greetings and enforce route/data restrictions based on user identity.

Quick Start

  1. On the client, import { useAuth } from 'path/to/AuthContext' and read the user object.
  2. In API handlers, access const userId = context.userId and guard routes when userId is undefined.

Frequently Asked Questions about user-access

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I get the authenticated user ID in both client components and server API handlers?

Use the useAuth() hook in client components to access user.id, and read context.userId from ApiHandlerContext in server API handlers to retrieve the authenticated user ID for authorization and personalization.

What is the best way to conditionally render UI based on JWT authentication status?

Call useAuth() in your client component to access the user object for conditional rendering; it returns user data when authenticated and undefined when unauthenticated, enabling personalized UI based on JWT authentication status.

How do I protect server API routes when the user is unauthenticated?

Access context.userId from ApiHandlerContext in your server API handler and guard the route by returning an error or redirecting when userId is undefined, ensuring only authenticated users can reach protected data endpoints.

Can I use JWT to authorize requests and fetch user-specific data on the server side?

Yes, JWT-derived userId is available in ApiHandlerContext on the server, allowing you to authorize requests, enforce user-based access control, and fetch user-specific data securely in your API handlers.

Why does useAuth return undefined for unauthenticated users in client-side code?

useAuth returns undefined for unauthenticated users because no valid JWT session exists, allowing client code to distinguish between logged-in and logged-out states for conditional rendering and personalized UI decisions.

Do I need a separate authentication library to access user data in client-server applications?

No separate library is needed if you have a client-side useAuth hook exposing user.id and a server-side ApiHandlerContext deriving userId from JWT, as these cover reading authenticated user identity for both UI personalization and protected data access.