dox_auth 2.0.0 dox_auth: ^2.0.0 copied to clipboard
Authentication package for dox framework with jsonwebtoken(jwt) driver.
- Create auth config
import 'package:dox_auth/dox_auth.dart';
import 'package:dox_core/dox_core.dart';
import 'package:dox/models/user/user.model.dart';
class AuthConfig extends AuthConfigInterface {
@override
String get defaultGuard => 'web';
@override
Map<String, Guard> get guards => <String, Guard>{
'web': Guard(
driver: JwtDriver(
secret: SecretKey(Env.get('APP_KEY')),
),
provider: Provider(
model: () => User(),
),
),
};
}
- Modify
bin/server.dart
to add auth config
Dox dox = Dox();
await dox.initialize(config);
dox.setAuthConfig(AuthConfig());
- Attempt Login
Map<String, dynamic> credentials = req.only(<String>['email', 'password']);
Auth auth = Auth();
String? token = await auth.attempt(credentials);
User? user = auth.user<User>();
- Register
doxAuthMiddleware
in route
Route.get('/auth/user', <dynamic>[doxAuthMiddleware, authController.user]);
- Verify Logged In or Fetch User information
Future<dynamic> fetchUser(DoxRequest req) async {
Auth? auth = req.auth<Auth>();
if (auth?.isLoggedIn() == true) {
return auth?.user();
}
throw UnAuthorizedException();
}