Members
# constant User
User model exposing persistence helpers for platform accounts.
# constant UserSchema
Mongoose schema describing application users.
# constant asyncHandler
Wraps an async function to catch any rejections and forward them to the next middleware. This allows you to use async/await in route handlers without manually wrapping them in try-catch blocks.
Example
```typescript
// Instead of:
app.get('/users', async (req, res, next) => {
try {
const users = await userService.getUsers();
res.json(users);
} catch (error) {
next(error);
}
});
// You can do:
app.get('/users', asyncHandler(async (req, res) => {
const users = await userService.getUsers();
res.json(users);
}));
```
# constant authInterceptor
gRPC Server Interceptor for Authentication
This interceptor is called for all RPC methods and handles authentication by extracting JWT tokens from metadata and attaching user info to the call.
Based on gRPC proposal L112: Node.js Server Interceptors
# constant envSchema
Environment variables schema and validation for K12 Auth Service.
Defines the expected shape of environment variables.
Uses zod for schema definition and runtime validation.
All variables are required unless a default value is provided.
Use env instead of accessing process.env directly.
# constant redisUtils
Convenience wrappers around common Redis commands.
# constant requireAdmin
Middleware ensuring the requester is authenticated as an admin.
# constant requireAuth
Middleware ensuring the requester is authenticated.
Methods
# authenticateToken(req, res, next) → {Promise.<void>}
Middleware verifying bearer tokens and populating req.user.
Parameters:
| Name | Type | Description |
|---|---|---|
req |
IAuthRequest
|
Express request containing auth headers. |
res |
Response
|
Express response instance. |
next |
NextFunction
|
Express next handler. |
Resolves when the request is forwarded.
Promise.<void>
# closeRedisConnection() → {Promise.<void>}
Gracefully close the Redis connection if it exists.
Resolves when the client quits.
Promise.<void>
# connectToRedis()
Establish connection to Redis for caching and pub/sub use cases.
# createServiceError()
Create a ServiceError with required metadata field
# getAuthenticatedUser()
Get authenticated user from call Returns user if authenticated, throws ServiceError if not Use this at the beginning of any protected RPC handler
Example
const user = getAuthenticatedUser(call);
// user is guaranteed to exist or error is thrown
# getRedisStatus() → {object}
Snapshot of the current Redis client status.
Connection metadata.
object
# initializeVault() → {Promise.<void>}
Initialize Vault connectivity and load secrets into memory.
Resolves when secrets are loaded.
Promise.<void>
# optionalAuthenticateToken(req, _res, next) → {Promise.<void>}
Optional authentication middleware
Attempts to authenticate the user if an access token is provided in the Authorization header, but never fails the request. This is useful for endpoints that should work both with and without authentication (e.g., logout endpoint that should clear cache even with expired tokens).
Parameters:
| Name | Type | Description |
|---|---|---|
req |
IAuthRequest
|
Express request containing auth headers. |
_res |
Response
|
Express response instance. |
next |
NextFunction
|
Express next handler. |
Resolves when the request is forwarded.
Promise.<void>
# requireAdmin()
Admin-only checker Convenience function to check if user is admin
# requireRole()
Role-based authorization checker Use this in your RPC handlers to check if user has required role
# async seedAdminUser()
Seed script to create a root admin user
# setupRedisEventListeners(client)
Wire Redis connection lifecycle event logging.
Parameters:
| Name | Type | Description |
|---|---|---|
client |
Redis
|
Active Redis client instance. |
# validateRequest(schema) → {function}
Middleware factory for request validation using Zod schemas.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
schema |
object
|
Schema map to validate against. |
|
body |
ZodSchema.<TBody>
|
<optional> |
Optional body schema. |
query |
ZodSchema.<TQuery>
|
<optional> |
Optional query schema. |
params |
ZodSchema.<TParams>
|
<optional> |
Optional params schema. |
Express middleware.
function