dartapi_auth 0.0.4 copy "dartapi_auth: ^0.0.4" to clipboard
dartapi_auth: ^0.0.4 copied to clipboard

DartAPI Auth is a lightweight and extensible authentication package for Dart backend applications. It provides JWT-based authentication with Auth Middleware.

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


1
likes
160
points
163
downloads

Publisher

verified publisherakashgk.com

Weekly Downloads

DartAPI Auth is a lightweight and extensible authentication package for Dart backend applications. It provides JWT-based authentication with Auth Middleware.

Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

dart_jsonwebtoken, shelf

More

Packages that depend on dartapi_auth