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 and dartapi

๐Ÿ“ฆ 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


Libraries

dartapi_auth