@ooneex/auth component handles authentication: it verifies bearer tokens, resolves the authenticated user, and enforces role-based route protection. It exposes an IAuth interface and a @decorator.auth() decorator so you can register any strategy with the container — a local JWT, an OAuth provider, or a third-party identity service.
Why this component
- Token verification built in. Extract a bearer token, verify it, and resolve the current user without wiring the pipeline yourself.
- Role-aware routing. Middleware reads each route’s
rolesand only enforces authentication on protected routes — guest routes pass through. - Pluggable strategies. Implement
IAuthto back authentication with any provider while call sites stay unchanged. - Container-managed. Register with a decorator and resolve from the container — no manual wiring.
- Provider-agnostic call sites. Controllers read
context.userand never depend on the underlying auth provider.
How it works
Authentication runs as middleware in the request pipeline. For each request it:- Extracts the token from the
Authorization: Bearer <token>header (or abearerTokenquery). - Checks the route’s
roles— if the route is guest-only (no roles orROLE_GUEST), it skips verification. - Otherwise verifies the token and resolves the user via
getCurrentUser(token). - Maps the provider’s user onto the framework
IUserand sets it oncontext.user.
context.user — they never import the auth provider directly.
Decorator and usage
@decorator.auth()
Registers an auth class with the container. It accepts an optional scope (defaults to singleton). A class only needs to implement getCurrentUser() to satisfy IAuth.
context.user. You can also resolve the strategy from the container directly:
Protecting routes
Declare the roles a route requires in itsRoute decorator. The middleware enforces authentication only when roles are present; an empty list (or ROLE_GUEST) leaves the route open.
Exceptions
The component throwsAuthException for authentication failures. It carries a machine-readable key, a human-readable message, and a data object, so callers can branch on the key.
| Key | When |
|---|---|
MISSING_BEARER_TOKEN | A protected route is requested without a token. |
INVALID_TOKEN | Token verification fails or resolves no user. |
Best practices
- Drive access from route roles. Declare
roleson routes and let the middleware enforce them — don’t re-check tokens inside controllers. - Read
context.user, not the provider. Keep controllers provider-agnostic so the auth strategy can change without touching handlers. - Branch on
AuthException.key. Return401forMISSING_BEARER_TOKEN/INVALID_TOKEN; keep keys stable and put detail indata. - Keep verification secrets in the environment. Load any signing key or provider secret from
.env; never hard-code it. - Fail fast on misconfiguration. Validate required secrets when the strategy is constructed so problems surface at startup, not at request time.
- Implement
IAuthto swap strategies. Back a custom provider by implementinggetCurrentUser()and registering it with@decorator.auth().