Skip to content

Commit

Permalink
Restructure auth configuration (#76)
Browse files Browse the repository at this point in the history
* restructure auth configuration

* update migration config
  • Loading branch information
necessarylion authored Dec 7, 2023
1 parent bb18bed commit 565bf64
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CREATE TABLE IF NOT EXISTS users (
id serial PRIMARY KEY,
name VARCHAR ( 255 ) NOT NULL,
email VARCHAR ( 255 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL,
password VARCHAR ( 255 ) NOT NULL,
deleted_at TIMESTAMP,
created_at TIMESTAMP,
Expand Down
20 changes: 0 additions & 20 deletions packages/dox-app/lib/config/auth_config.dart

This file was deleted.

17 changes: 15 additions & 2 deletions packages/dox-app/lib/services/auth_service.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import 'package:dox_app/config/auth_config.dart';
import 'package:dox_app/models/user/user.model.dart';
import 'package:dox_auth/dox_auth.dart';
import 'package:dox_core/dox_core.dart';

class AuthService implements DoxService {
@override
void setup() {
Auth.initialize(AuthConfig());
Auth.initialize(AuthConfig(
/// default auth guard
defaultGuard: 'web',

/// list of auth guards
guards: <String, AuthGuard>{
'web': AuthGuard(
driver: JwtAuthDriver(secret: SecretKey(Env.get('APP_KEY'))),
provider: AuthProvider(
model: () => User(),
),
),
},
));
}
}
2 changes: 1 addition & 1 deletion packages/dox-auth/lib/src/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Auth implements IAuth {

AuthDriver get _driver => AuthEngine().driver;

static void initialize(IAuthConfig authConfig) {
static void initialize(AuthConfig authConfig) {
AuthEngine().init(authConfig);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/dox-auth/lib/src/auth_engine.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:dox_auth/src/interfaces.dart';

class AuthEngine {
late IAuthConfig config;
late AuthConfig config;

/// get auth guard
AuthGuard get guard => config.guards[config.defaultGuard]!;
Expand All @@ -17,7 +17,7 @@ class AuthEngine {
factory AuthEngine() => _singleton;
AuthEngine._internal();

void init(IAuthConfig authConfig) {
void init(AuthConfig authConfig) {
config = authConfig;
}
}
10 changes: 7 additions & 3 deletions packages/dox-auth/lib/src/interfaces.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ class AuthProvider {
});
}

abstract class IAuthConfig {
String get defaultGuard => 'web';
class AuthConfig {
final String defaultGuard;
final Map<String, AuthGuard> guards;

Map<String, AuthGuard> get guards => <String, AuthGuard>{};
AuthConfig({
required this.defaultGuard,
this.guards = const <String, AuthGuard>{},
});
}

abstract class AuthDriver {
Expand Down
2 changes: 1 addition & 1 deletion packages/dox-migration/lib/src/dox_migration_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Migration {
/// main function to run migration
/// - will create migration table if not exist
/// - will run migrations from db/migration folder
Future<void> migrate({PgEndpoint? endPoint}) async {
Future<void> migrate([PgEndpoint? endPoint]) async {
endPoint = getEndpoint(endPoint);
pool = PgPool(endPoint);
await createMigrationTableIfNotExist(pool);
Expand Down

0 comments on commit 565bf64

Please sign in to comment.