Skip to content

Commit cbc4bb7

Browse files
docs: Add missing docs on how to use UserProfile callbacks (#432)
1 parent debdac6 commit cbc4bb7

File tree

5 files changed

+145
-5
lines changed

5 files changed

+145
-5
lines changed

docs/06-concepts/11-authentication/03-working-with-users.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ await AuthServices.instance.userProfiles.changeFullName(session, authUserId, 'my
107107

108108
For the full list of operations, see the [UserProfiles](https://pub.dev/documentation/serverpod_auth_core_server/latest/serverpod_auth_core_server/UserProfiles-class.html) class documentation.
109109

110+
### User profile callbacks
111+
112+
You can react when a user profile is created or updated by using the `UserProfileConfig` callbacks. Configure them when initializing auth services on the `pod` object via the `userProfileConfig` parameter.
113+
114+
:::warning
115+
All callbacks receive a `transaction` parameter that should be used on all database operations performed inside the callback. Failing to pass the transaction might lead to entries not being found or changes not being rolled back together.
116+
:::
117+
118+
| Callback | When | Purpose |
119+
| -------- | ---- | ------- |
120+
| `onBeforeUserProfileCreated` | Before a profile is created | Receive and return `UserProfileData` to modify defaults (user name, full name, email). |
121+
| `onAfterUserProfileCreated` | After a profile is created | Run logic that depends on the new profile (e.g. related data, default image). |
122+
| `onBeforeUserProfileUpdated` | Before a profile is updated (name, full name, or picture) | Receive and return `UserProfileData` to enforce rules or transform values. |
123+
| `onAfterUserProfileUpdated` | After a profile is updated | Sync related data or trigger side effects. |
124+
125+
Example using `onAfterUserProfileCreated`:
126+
127+
```dart
128+
pod.initializeAuthServices(
129+
...
130+
userProfileConfig: UserProfileConfig(
131+
onAfterUserProfileCreated: (session, userProfile, {required transaction}) async {
132+
// Do something with the new profile (e.g. create related data, set default image)
133+
},
134+
),
135+
);
136+
```
137+
110138
### Accessing user profiles from the app
111139

112140
To access the user profile from your Flutter app, you can use the `userProfileInfo` endpoint that is included in the authentication module:
@@ -175,7 +203,7 @@ class UserProfileEditEndpoint extends UserProfileEditBaseEndpoint {
175203

176204
### Setting a default user image
177205

178-
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, it is possible to set a default user image using the `UserProfileConfig` object.
206+
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, you can set a default user image using the `onAfterUserProfileCreated` callback in `UserProfileConfig` (see [User profile callbacks](#user-profile-callbacks) for the full set of callbacks).
179207

180208
```dart
181209
pod.initializeAuthServices(

versioned_docs/version-3.0.0/06-concepts/11-authentication/03-working-with-users.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ await AuthServices.instance.userProfiles.changeFullName(session, authUserId, 'my
107107

108108
For the full list of operations, see the [UserProfiles](https://pub.dev/documentation/serverpod_auth_core_server/latest/serverpod_auth_core_server/UserProfiles-class.html) class documentation.
109109

110+
### User profile callbacks
111+
112+
You can react when a user profile is created or updated by using the `UserProfileConfig` callbacks. Configure them when initializing auth services on the `pod` object via the `userProfileConfig` parameter.
113+
114+
:::warning
115+
All callbacks receive a `transaction` parameter that should be used on all database operations performed inside the callback. Failing to pass the transaction might lead to entries not being found or changes not being rolled back together.
116+
:::
117+
118+
| Callback | When | Purpose |
119+
| -------- | ---- | ------- |
120+
| `onBeforeUserProfileCreated` | Before a profile is created | Receive and return `UserProfileData` to modify defaults (user name, full name, email). |
121+
| `onAfterUserProfileCreated` | After a profile is created | Run logic that depends on the new profile (e.g. related data, default image). |
122+
| `onBeforeUserProfileUpdated` | Before a profile is updated (name, full name, or picture) | Receive and return `UserProfileData` to enforce rules or transform values. |
123+
| `onAfterUserProfileUpdated` | After a profile is updated | Sync related data or trigger side effects. |
124+
125+
Example using `onAfterUserProfileCreated`:
126+
127+
```dart
128+
pod.initializeAuthServices(
129+
...
130+
userProfileConfig: UserProfileConfig(
131+
onAfterUserProfileCreated: (session, userProfile, {required transaction}) async {
132+
// Do something with the new profile (e.g. create related data, set default image)
133+
},
134+
),
135+
);
136+
```
137+
110138
### Accessing user profiles from the app
111139

112140
To access the user profile from your Flutter app, you can use the `userProfileInfo` endpoint that is included in the authentication module:
@@ -175,7 +203,7 @@ class UserProfileEditEndpoint extends UserProfileEditBaseEndpoint {
175203

176204
### Setting a default user image
177205

178-
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, it is possible to set a default user image using the `UserProfileConfig` object.
206+
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, you can set a default user image using the `onAfterUserProfileCreated` callback in `UserProfileConfig` (see [User profile callbacks](#user-profile-callbacks) for the full set of callbacks).
179207

180208
```dart
181209
pod.initializeAuthServices(

versioned_docs/version-3.1.0/06-concepts/11-authentication/03-working-with-users.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ await AuthServices.instance.userProfiles.changeFullName(session, authUserId, 'my
107107

108108
For the full list of operations, see the [UserProfiles](https://pub.dev/documentation/serverpod_auth_core_server/latest/serverpod_auth_core_server/UserProfiles-class.html) class documentation.
109109

110+
### User profile callbacks
111+
112+
You can react when a user profile is created or updated by using the `UserProfileConfig` callbacks. Configure them when initializing auth services on the `pod` object via the `userProfileConfig` parameter.
113+
114+
:::warning
115+
All callbacks receive a `transaction` parameter that should be used on all database operations performed inside the callback. Failing to pass the transaction might lead to entries not being found or changes not being rolled back together.
116+
:::
117+
118+
| Callback | When | Purpose |
119+
| -------- | ---- | ------- |
120+
| `onBeforeUserProfileCreated` | Before a profile is created | Receive and return `UserProfileData` to modify defaults (user name, full name, email). |
121+
| `onAfterUserProfileCreated` | After a profile is created | Run logic that depends on the new profile (e.g. related data, default image). |
122+
| `onBeforeUserProfileUpdated` | Before a profile is updated (name, full name, or picture) | Receive and return `UserProfileData` to enforce rules or transform values. |
123+
| `onAfterUserProfileUpdated` | After a profile is updated | Sync related data or trigger side effects. |
124+
125+
Example using `onAfterUserProfileCreated`:
126+
127+
```dart
128+
pod.initializeAuthServices(
129+
...
130+
userProfileConfig: UserProfileConfig(
131+
onAfterUserProfileCreated: (session, userProfile, {required transaction}) async {
132+
// Do something with the new profile (e.g. create related data, set default image)
133+
},
134+
),
135+
);
136+
```
137+
110138
### Accessing user profiles from the app
111139

112140
To access the user profile from your Flutter app, you can use the `userProfileInfo` endpoint that is included in the authentication module:
@@ -175,7 +203,7 @@ class UserProfileEditEndpoint extends UserProfileEditBaseEndpoint {
175203

176204
### Setting a default user image
177205

178-
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, it is possible to set a default user image using the `UserProfileConfig` object.
206+
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, you can set a default user image using the `onAfterUserProfileCreated` callback in `UserProfileConfig` (see [User profile callbacks](#user-profile-callbacks) for the full set of callbacks).
179207

180208
```dart
181209
pod.initializeAuthServices(

versioned_docs/version-3.2.0/06-concepts/11-authentication/03-working-with-users.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ await AuthServices.instance.userProfiles.changeFullName(session, authUserId, 'my
107107

108108
For the full list of operations, see the [UserProfiles](https://pub.dev/documentation/serverpod_auth_core_server/latest/serverpod_auth_core_server/UserProfiles-class.html) class documentation.
109109

110+
### User profile callbacks
111+
112+
You can react when a user profile is created or updated by using the `UserProfileConfig` callbacks. Configure them when initializing auth services on the `pod` object via the `userProfileConfig` parameter.
113+
114+
:::warning
115+
All callbacks receive a `transaction` parameter that should be used on all database operations performed inside the callback. Failing to pass the transaction might lead to entries not being found or changes not being rolled back together.
116+
:::
117+
118+
| Callback | When | Purpose |
119+
| -------- | ---- | ------- |
120+
| `onBeforeUserProfileCreated` | Before a profile is created | Receive and return `UserProfileData` to modify defaults (user name, full name, email). |
121+
| `onAfterUserProfileCreated` | After a profile is created | Run logic that depends on the new profile (e.g. related data, default image). |
122+
| `onBeforeUserProfileUpdated` | Before a profile is updated (name, full name, or picture) | Receive and return `UserProfileData` to enforce rules or transform values. |
123+
| `onAfterUserProfileUpdated` | After a profile is updated | Sync related data or trigger side effects. |
124+
125+
Example using `onAfterUserProfileCreated`:
126+
127+
```dart
128+
pod.initializeAuthServices(
129+
...
130+
userProfileConfig: UserProfileConfig(
131+
onAfterUserProfileCreated: (session, userProfile, {required transaction}) async {
132+
// Do something with the new profile (e.g. create related data, set default image)
133+
},
134+
),
135+
);
136+
```
137+
110138
### Accessing user profiles from the app
111139

112140
To access the user profile from your Flutter app, you can use the `userProfileInfo` endpoint that is included in the authentication module:
@@ -175,7 +203,7 @@ class UserProfileEditEndpoint extends UserProfileEditBaseEndpoint {
175203

176204
### Setting a default user image
177205

178-
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, it is possible to set a default user image using the `UserProfileConfig` object.
206+
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, you can set a default user image using the `onAfterUserProfileCreated` callback in `UserProfileConfig` (see [User profile callbacks](#user-profile-callbacks) for the full set of callbacks).
179207

180208
```dart
181209
pod.initializeAuthServices(

versioned_docs/version-3.3.0/06-concepts/11-authentication/03-working-with-users.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ await AuthServices.instance.userProfiles.changeFullName(session, authUserId, 'my
107107

108108
For the full list of operations, see the [UserProfiles](https://pub.dev/documentation/serverpod_auth_core_server/latest/serverpod_auth_core_server/UserProfiles-class.html) class documentation.
109109

110+
### User profile callbacks
111+
112+
You can react when a user profile is created or updated by using the `UserProfileConfig` callbacks. Configure them when initializing auth services on the `pod` object via the `userProfileConfig` parameter.
113+
114+
:::warning
115+
All callbacks receive a `transaction` parameter that should be used on all database operations performed inside the callback. Failing to pass the transaction might lead to entries not being found or changes not being rolled back together.
116+
:::
117+
118+
| Callback | When | Purpose |
119+
| -------- | ---- | ------- |
120+
| `onBeforeUserProfileCreated` | Before a profile is created | Receive and return `UserProfileData` to modify defaults (user name, full name, email). |
121+
| `onAfterUserProfileCreated` | After a profile is created | Run logic that depends on the new profile (e.g. related data, default image). |
122+
| `onBeforeUserProfileUpdated` | Before a profile is updated (name, full name, or picture) | Receive and return `UserProfileData` to enforce rules or transform values. |
123+
| `onAfterUserProfileUpdated` | After a profile is updated | Sync related data or trigger side effects. |
124+
125+
Example using `onAfterUserProfileCreated`:
126+
127+
```dart
128+
pod.initializeAuthServices(
129+
...
130+
userProfileConfig: UserProfileConfig(
131+
onAfterUserProfileCreated: (session, userProfile, {required transaction}) async {
132+
// Do something with the new profile (e.g. create related data, set default image)
133+
},
134+
),
135+
);
136+
```
137+
110138
### Accessing user profiles from the app
111139

112140
To access the user profile from your Flutter app, you can use the `userProfileInfo` endpoint that is included in the authentication module:
@@ -175,7 +203,7 @@ class UserProfileEditEndpoint extends UserProfileEditBaseEndpoint {
175203

176204
### Setting a default user image
177205

178-
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, it is possible to set a default user image using the `UserProfileConfig` object.
206+
When logging in from some providers, the user image is automatically fetched and set as the user's profile picture - such as with Google Sign In. However, when an image is not found or the provider does not expose the picture, you can set a default user image using the `onAfterUserProfileCreated` callback in `UserProfileConfig` (see [User profile callbacks](#user-profile-callbacks) for the full set of callbacks).
179207

180208
```dart
181209
pod.initializeAuthServices(

0 commit comments

Comments
 (0)