dartapi_auth
dartapi_auth
is a lightweight authentication and authorization package designed for the DartAPI ecosystem. It provides JWT-based authentication utilities, middleware for request protection, and token lifecycle management.
It is fully compatible with projects generated using the DartAPI CLI, and integrates seamlessly with ApiRoute<ApiInput, ApiOutput>
.
โจ Features
- ๐ JWT Access & Refresh Token generation
- ๐งพ Token verification with expiration, type, and issuer checks
- ๐ก๏ธ Plug-and-play authentication middleware for protected routes
- ๐ง Helpers to extract tokens from headers or cookies
- โ
Works perfectly with
dartapi_core
anddartapi
๐ฆ Installation
dependencies:
dartapi_auth: ^0.0.4
๐ Usage
๐ Setup JwtService
final jwtService = JwtService(
accessTokenSecret: 'my-secret',
refreshTokenSecret: 'my-refresh-secret',
issuer: 'dartapi',
audience: 'api-clients',
);
๐งช Generate Tokens
final accessToken = jwtService.generateAccessToken(claims: {
'sub': 'user-123',
'username': 'akash',
});
final refreshToken = jwtService.generateRefreshToken(accessToken: accessToken);
๐ Verify Tokens
final accessPayload = jwtService.verifyAccessToken(accessToken);
final refreshPayload = jwtService.verifyRefreshToken(refreshToken);
๐ Use Middleware to Protect Routes
Import middleware
import 'package:dartapi_auth/dartapi_auth.dart';
Apply per-route:
ApiRoute<void, List<UserDTO>>(
method: ApiMethod.get,
path: '/users',
typedHandler: getUsers,
middlewares: [authMiddleware(jwtService)],
);
๐ Example Use in dartapi CLI Project
bin/main.dart
final jwtService = JwtService(...);
final app = DartAPI();
app.addControllers([
UserController(jwtService),
AuthController(jwtService),
]);
UserController
ApiRoute<void, List<UserDTO>>(
method: ApiMethod.get,
path: '/users',
typedHandler: getAllUsers,
middlewares: [authMiddleware(jwtService)],
);
๐ Exports
JwtService
authMiddleware()
utils.dart
๐ License
BSD 3-Clause License ยฉ 2025 Akash G Krishnan
LICENSE
๐ Related
- dartapi - CLI to generate projects using this
- dartapi_core - Type-safe API routing & controller logic
- dartapi_db - DB abstraction layer