Version: @gately/nodejs@2.2.0
Features: Token verification, Express middleware, Fastify plugin, token caching, TypeScript support
Features: Token verification, Express middleware, Fastify plugin, token caching, TypeScript support
Installation
Quick Start
Express
Fastify
Manual Token Verification
Core API
verifyToken(token, apiKey)
Verifies and decodes a Gately JWT token.validateToken(token, apiKey)
Alias forverifyToken.
getAuthorizationHeader(authHeader)
Extracts the JWT from anAuthorization header.
Express Integration
Global Middleware
Per-Route: requireAuth
Per-Route: optionalAuth
Attaches user toreq.auth if token is present, but doesn’t block if missing:
Per-Route: withAuth
Attach a custom callback after verification:protect
Shorthand route-level protection:Fastify Integration
Plugin (Global)
Route-Level Protection
Request Object
Middleware attaches anauth object to every authenticated request:
Utility Functions
isTokenExpired
getTokenExpiry
Token Cache
Tokens are cached in memory to avoid re-decoding on every request:Error Handling
Role-Based Access
TypeScript
Environment Variables
Troubleshooting
Token verification fails
Token verification fails
- Confirm the token was issued by Gately and hasn’t expired
- Ensure
apiKeymatches the project the token was issued for - Check the
Authorizationheader format:Bearer <token>
Middleware not protecting routes
Middleware not protecting routes
Middleware must be registered before route handlers:
req.auth is undefined
req.auth is undefined
Ensure the middleware ran successfully. Check that:
- A valid
Authorization: Bearer <token>header is sent - The route is not in
excludePaths optionalis not set totrue(which silently skips bad tokens)
Next Steps
React SDK
Client-side authentication
Next.js SDK
Full-stack Next.js auth