Skip to content

Commit 0f1db3a

Browse files
authored
feat(auth2): Add hook for when user is migrated in serverpod_auth_migration (serverpod#3728)
1 parent 994d046 commit 0f1db3a

File tree

5 files changed

+239
-27
lines changed

5 files changed

+239
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
export 'src/business/auth_migration_config.dart';
12
export 'src/business/auth_migration_email.dart';
2-
export 'src/business/auth_migration_email_config.dart';
33
export 'src/generated/endpoints.dart';
44
export 'src/generated/protocol.dart';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:serverpod/serverpod.dart';
2+
3+
/// Configuration options for the account migration.
4+
class AuthMigrationConfig {
5+
/// Whether to import the `serverpod_auth` `UserInfo` into a
6+
/// `serverpod_auth_profile` `UserProfile`.
7+
///
8+
/// Defaults to `true`.
9+
final bool importProfile;
10+
11+
/// Callback to be invoked if a user has been migrated.
12+
///
13+
/// If one does any further migratations for this user in the database, one
14+
/// should ensure to use the passed `transaction`, such that a failure rolls
15+
/// back the entire, partial write.
16+
final UserMigrationHook? userMigrationHook;
17+
18+
/// Create a new email account migration configuration.
19+
AuthMigrationConfig({
20+
this.importProfile = true,
21+
this.userMigrationHook,
22+
});
23+
}
24+
25+
/// Callback to be invoked when a `serverpod_auth` `UserInfo` has been migrated
26+
/// to a `serverpod_auth_user` `AuthUser`.
27+
///
28+
/// This is called only once, even if multiple authentications are migrated for
29+
/// the user successively.
30+
typedef UserMigrationHook = Future<void> Function(
31+
Session session, {
32+
required int oldUserId,
33+
required UuidValue newAuthUserId,
34+
Transaction? transaction,
35+
});

modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/auth_migration_email.dart

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'package:serverpod_auth_user_server/serverpod_auth_user_server.dart'
1313
/// `serverpod_auth_email_account`.
1414
abstract final class AuthMigrationEmail {
1515
/// The current configuration for the email authentication migration.
16-
static AuthMigrationEmailConfig config = AuthMigrationEmailConfig();
16+
static AuthMigrationConfig config = AuthMigrationConfig();
1717

1818
/// Attempts to migrate the user and their email authentication to the new
1919
/// auth module.
@@ -92,7 +92,7 @@ abstract final class AuthMigrationEmail {
9292
session.db,
9393
transaction,
9494
(final transaction) async {
95-
final migratedUser = await _migrateUserIfNeeded(
95+
final (migratedUser, didCreate) = await _migrateUserIfNeeded(
9696
session,
9797
userInfo,
9898
transaction: transaction,
@@ -105,6 +105,15 @@ abstract final class AuthMigrationEmail {
105105
password: password,
106106
transaction: transaction,
107107
);
108+
109+
if (didCreate) {
110+
await config.userMigrationHook?.call(
111+
session,
112+
oldUserId: migratedUser.oldUserId,
113+
newAuthUserId: migratedUser.newAuthUserId,
114+
transaction: transaction,
115+
);
116+
}
108117
},
109118
);
110119
}
@@ -160,7 +169,7 @@ abstract final class AuthMigrationEmail {
160169
session.db,
161170
transaction,
162171
(final transaction) async {
163-
final migratedUser = await _migrateUserIfNeeded(
172+
final (migratedUser, didCreate) = await _migrateUserIfNeeded(
164173
session,
165174
userInfo,
166175
transaction: transaction,
@@ -173,11 +182,21 @@ abstract final class AuthMigrationEmail {
173182
password: null,
174183
transaction: transaction,
175184
);
185+
186+
if (didCreate) {
187+
await config.userMigrationHook?.call(
188+
session,
189+
oldUserId: migratedUser.oldUserId,
190+
newAuthUserId: migratedUser.newAuthUserId,
191+
transaction: transaction,
192+
);
193+
}
176194
},
177195
);
178196
}
179197

180-
static Future<MigratedUser> _migrateUserIfNeeded(
198+
static Future<(MigratedUser migratedUser, bool didCreate)>
199+
_migrateUserIfNeeded(
181200
final Session session,
182201
final legacy_auth.UserInfo userInfo, {
183202
required final Transaction transaction,
@@ -189,7 +208,7 @@ abstract final class AuthMigrationEmail {
189208
);
190209

191210
if (migratedUser != null) {
192-
return migratedUser;
211+
return (migratedUser, false);
193212
}
194213

195214
final authUser = await new_auth_user.AuthUsers.create(
@@ -208,10 +227,13 @@ abstract final class AuthMigrationEmail {
208227
);
209228
}
210229

211-
return MigratedUser.db.insertRow(
212-
session,
213-
MigratedUser(oldUserId: userInfo.id!, newAuthUserId: authUser.id),
214-
transaction: transaction,
230+
return (
231+
await MigratedUser.db.insertRow(
232+
session,
233+
MigratedUser(oldUserId: userInfo.id!, newAuthUserId: authUser.id),
234+
transaction: transaction,
235+
),
236+
true,
215237
);
216238
}
217239

modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/auth_migration_email_config.dart

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)