Skip to main content

dox_auth 3.0.0-alpha.5 copy "dox_auth: ^3.0.0-alpha.5" to clipboard
dox_auth: ^3.0.0-alpha.5 copied to clipboard

Authentication package for dox framework with jsonwebtoken(jwt) driver.

dox_auth #

Login for Dox apps. It checks an email and a password and gives back a JWT token.

pub package license: MIT

Part of Dox, a web backend framework for Dart, like Laravel. Source code: github.com/dartondox/dox.

Install #

dart pub add dox_auth

Dox 3.x is in alpha (3.0.0-alpha.4). The API can still change before the stable release.

A new app made with dox create already has it.

Use it #

Set it up in a service:

import 'package:dox_auth/dox_auth.dart';
import 'package:dox_core/dox_core.dart';

class AuthService implements DoxService {
  @override
  void setup() {
    Auth.initialize(AuthConfig(
      defaultGuard: 'web',
      guards: <String, AuthGuard>{
        'web': AuthGuard(
          driver: JwtAuthDriver(
            secret: SecretKey(Env.get('APP_KEY')),
            expiresIn: const Duration(days: 7),
          ),
          provider: AuthProvider(model: () => User()),
        ),
      },
    ));
  }
}

Log in, and protect a route:

String? token = await Auth().attempt(<String, dynamic>{
  'email': 'ada@example.com',
  'password': 'secret',
}); // null when the email or password is wrong

Route.get('/auth/user', <dynamic>[AuthMiddleware(), authController.user]);

After AuthMiddleware, req.auth?.user<User>() is the logged in user.

Features #

  • JWT driver (JwtAuthDriver) with a secret, algorithm, expiry and issuer.
  • Auth().attempt(credentials) finds the user and checks the bcrypt hash.
  • Auth().login(user) gives a token with no password check.
  • AuthMiddleware answers 401 when the token is missing or not valid.
  • The user is loaded from the database on each request with dox_orm.

Things to know: set expiresIn, or a token never expires. There is no server list of tokens, so you can not cancel one token. Only the default guard is used.

Documentation #

License #

MIT. See LICENSE.

1
likes
0
points
2.05k
downloads

Publisher

verified publisherdartondox.dev

Weekly Downloads

Authentication package for dox framework with jsonwebtoken(jwt) driver.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

bcrypt, dart_jsonwebtoken, dox_annotation, dox_orm

More

Packages that depend on dox_auth