Skip to content

akashgk/dartapi_auth

Repository files navigation

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


🔗 Related

About

DartAPI Auth is a lightweight and extensible authentication package for Dart backend applications.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages