Skip to main content

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

Authentication package for dox framework with JWT and session drivers.

dox_auth #

Login for Dox apps. It checks an email and a password. It gives back a JWT token for an API, or keeps the login in the session for web pages.

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.x). 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.

For web pages, use SessionAuthDriver() in the guard. It needs SessionMiddleware:

bool ok = await Auth().attemptSession(req, credentials); // false when wrong
Auth().logout(req);

Route.get('/dashboard', <dynamic>[AuthMiddleware(redirectTo: '/login'), pageController.dashboard]);

Features #

  • JWT driver (JwtAuthDriver) with a secret, algorithm, expiry and issuer.
  • Session driver (SessionAuthDriver) for web pages. Login and logout give the session a new id.
  • 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 user is not logged in. With redirectTo, it sends the user to that page instead.
  • More than one guard, like web and admin, each with its own driver and user model.
  • The user is loaded from the database on each request with dox_orm.

Things to know: a token expires after 1 hour by default. Change it with expiresIn. There is no server list of tokens, so you can not cancel one token. A session login ends with Auth().logout(req). With the default cookie session driver, all session data sits in the cookie. So an old copy of the cookie still works after logout. If logout must really end old cookies, use the cache session driver, like SessionConfig(driver: 'cache', store: 'redis').

Documentation #

License #

MIT. See LICENSE.

1
likes
0
points
2.05k
downloads

Publisher

verified publisherdartondox.dev

Weekly Downloads

Authentication package for dox framework with JWT and session drivers.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

bcrypt, dart_jsonwebtoken, dox_annotation, dox_core, dox_orm

More

Packages that depend on dox_auth