Version: @gately/nodejs@2.4.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, options?)
Verifies a Gately session token and returns its claims. Verification happens on Gately’s servers (POST /api/v1/auth/verify): the signature, expiry, and project are all checked, so a token is only accepted if Gately issued it to a member of the project that owns apiKey. Results are cached in memory for up to 5 minutes (never past the token’s expiry), so most requests don’t make a network call.
Versions before 2.4.0 decoded the token locally without checking its signature, and rejected real session tokens. Upgrade to 2.4.0 or later.
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