Skip to content

Commit 9a519be

Browse files
docs: Misc small improvements for the authentication docs (#391)
1 parent 826ef62 commit 9a519be

File tree

32 files changed

+298
-84
lines changed

32 files changed

+298
-84
lines changed

docs/06-concepts/11-authentication/01-setup.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ class SignInPage extends StatelessWidget {
268268
body: SignInWidget(
269269
client: client,
270270
onAuthenticated: () {
271-
// Navigate to home screen
272-
Navigator.of(context).pushReplacement(
273-
MaterialPageRoute(builder: (_) => HomePage()),
274-
);
271+
// Do something when the user is authenticated.
272+
//
273+
// NOTE: You should not navigate to the home screen here, otherwise
274+
// the user will have to sign in again every time they open the app.
275275
},
276276
onError: (error) {
277277
// Handle errors
@@ -289,4 +289,8 @@ This widget is a convenient way to use identity providers out-of-the-box, but yo
289289

290290
#### Updating the UI based on authentication state
291291

292-
Instead of navigating to the home screen using the `onAuthenticated` callback, you can listen to authentication state changes and update the UI accordingly. See the [Client-side authentication](./basics#monitor-authentication-changes) section for more details.
292+
To update the UI based on authentication state, you must listen to authentication state changes using the `authInfoListenable` getter. See the [Client-side authentication](./basics#monitor-authentication-changes) section for more details.
293+
294+
:::warning
295+
Do not navigate to the home screen using the `onAuthenticated` callback. This will cause the user to have to sign in again every time they open the app.
296+
:::

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,34 @@ await AuthServices.instance.authUsers.delete(session, userIdUuidValue);
2020

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

23+
## Blocking users
24+
25+
You can block users to prevent them from signing in to your application. When a blocked user attempts to authenticate, an `AuthUserBlockedException` will be thrown, and the authentication will fail.
26+
27+
### Blocking or unblocking a user
28+
29+
To block/unblock a user, use the `update` method of the `AuthUsers` class:
30+
31+
```dart
32+
await AuthServices.instance.authUsers.update(
33+
session,
34+
authUserId: authUserId,
35+
blocked: true, // or false to unblock
36+
);
37+
```
38+
39+
Users can also be created with the blocked status set from the start:
40+
```dart
41+
await AuthServices.instance.authUsers.create(
42+
session,
43+
blocked: true,
44+
);
45+
```
46+
47+
:::note
48+
When a user is blocked, they will not be able to sign in until they are unblocked. However, blocking a user does not automatically revoke their existing sessions. Be sure to revoke existing sessions for a complete block operation. See [Revoking tokens](./token-managers/managing-tokens#revoking-tokens) for more details.
49+
:::
50+
2351
## User profiles
2452

2553
By default, all authenticated users have a `UserProfile` object that contains information about the signed-in user. To access the `UserProfile` object, you can use the `userProfile` extension on the `AuthenticationInfo` object.
@@ -83,6 +111,17 @@ indexes:
83111
unique: true
84112
```
85113
114+
:::note
115+
Note that the `AuthUser` model is declared in the `serverpod_auth_core` module, which is automatically included in your project as a dependency of the `serverpod_auth_idp` module. If you are not ignoring the generated files in your `analysis_options.yaml`, you might need to explicitly add the `serverpod_auth_core` module to your project to prevent `depend_on_referenced_packages` lint errors. The general recommendation, however, is to ignore linting on generated files:
116+
117+
```yaml
118+
# analysis_options.yaml
119+
analyzer:
120+
exclude:
121+
- lib/src/generated/**
122+
```
123+
:::
124+
86125
:::tip
87126
When referencing module classes in your model files, you can use a nickname for the module instead of the full module name. See the [modules documentation](../modules) for more information.
88127
:::

docs/06-concepts/11-authentication/04-providers/01-email/01-setup.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ import 'package:serverpod_auth_idp_flutter/serverpod_auth_idp_flutter.dart';
102102
EmailSignInWidget(
103103
client: client,
104104
onAuthenticated: () {
105-
// Navigate to home screen or update UI
106-
Navigator.of(context).pushReplacement(
107-
MaterialPageRoute(builder: (_) => HomePage()),
108-
);
105+
// Do something when the user is authenticated.
106+
//
107+
// NOTE: You should not navigate to the home screen here, otherwise
108+
// the user will have to sign in again every time they open the app.
109109
},
110110
onError: (error) {
111111
// Handle errors

docs/06-concepts/11-authentication/04-providers/01-email/03-customizing-the-ui.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ EmailSignInWidget(
5858
// Open privacy policy
5959
},
6060
onAuthenticated: () {
61-
// Handle successful authentication
61+
// Do something when the user is authenticated.
62+
//
63+
// NOTE: You should not navigate to the home screen here, otherwise
64+
// the user will have to sign in again every time they open the app.
6265
},
6366
onError: (error) {
6467
// Handle errors
@@ -115,7 +118,10 @@ final controller = EmailAuthController(
115118
client: client,
116119
startScreen: EmailFlowScreen.login,
117120
onAuthenticated: () {
118-
// Handle successful authentication
121+
// Do something when the user is authenticated.
122+
//
123+
// NOTE: You should not navigate to the home screen here, otherwise
124+
// the user will have to sign in again every time they open the app.
119125
},
120126
onError: (error) {
121127
// Handle errors

docs/06-concepts/11-authentication/04-providers/02-google/01-setup.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ import 'package:serverpod_auth_idp_flutter/serverpod_auth_idp_flutter.dart';
226226
GoogleSignInWidget(
227227
client: client,
228228
onAuthenticated: () {
229-
// Navigate to home screen or update UI
230-
Navigator.of(context).pushReplacement(
231-
MaterialPageRoute(builder: (_) => HomePage()),
232-
);
229+
// Do something when the user is authenticated.
230+
//
231+
// NOTE: You should not navigate to the home screen here, otherwise
232+
// the user will have to sign in again every time they open the app.
233233
},
234234
onError: (error) {
235235
// Handle errors

docs/06-concepts/11-authentication/04-providers/02-google/03-customizing-the-ui.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ GoogleSignInWidget(
4646
attemptLightweightSignIn: true,
4747
4848
onAuthenticated: () {
49-
// Handle successful authentication
49+
// Do something when the user is authenticated.
50+
//
51+
// NOTE: You should not navigate to the home screen here, otherwise
52+
// the user will have to sign in again every time they open the app.
5053
},
5154
onError: (error) {
5255
// Handle errors
@@ -64,7 +67,10 @@ import 'package:serverpod_auth_idp_flutter/serverpod_auth_idp_flutter.dart';
6467
final controller = GoogleAuthController(
6568
client: client,
6669
onAuthenticated: () {
67-
// Handle successful authentication
70+
// Do something when the user is authenticated.
71+
//
72+
// NOTE: You should not navigate to the home screen here, otherwise
73+
// the user will have to sign in again every time they open the app.
6874
},
6975
onError: (error) {
7076
// Handle errors

docs/06-concepts/11-authentication/04-providers/03-apple/01-setup.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ import 'package:serverpod_auth_idp_flutter/serverpod_auth_idp_flutter.dart';
188188
AppleSignInWidget(
189189
client: client,
190190
onAuthenticated: () {
191-
// Navigate to home screen or update UI
192-
Navigator.of(context).pushReplacement(
193-
MaterialPageRoute(builder: (_) => HomePage()),
194-
);
191+
// Do something when the user is authenticated.
192+
//
193+
// NOTE: You should not navigate to the home screen here, otherwise
194+
// the user will have to sign in again every time they open the app.
195195
},
196196
onError: (error) {
197197
// Handle errors

docs/06-concepts/11-authentication/04-providers/03-apple/03-customizing-the-ui.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ AppleSignInWidget(
4242
],
4343
4444
onAuthenticated: () {
45-
// Handle successful authentication
45+
// Do something when the user is authenticated.
46+
//
47+
// NOTE: You should not navigate to the home screen here, otherwise
48+
// the user will have to sign in again every time they open the app.
4649
},
4750
onError: (error) {
4851
// Handle errors
@@ -60,7 +63,10 @@ import 'package:serverpod_auth_idp_flutter/serverpod_auth_idp_flutter.dart';
6063
final controller = AppleAuthController(
6164
client: client,
6265
onAuthenticated: () {
63-
// Handle successful authentication
66+
// Do something when the user is authenticated.
67+
//
68+
// NOTE: You should not navigate to the home screen here, otherwise
69+
// the user will have to sign in again every time they open the app.
6470
},
6571
onError: (error) {
6672
// Handle errors

docs/06-concepts/11-authentication/06-ui-components.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class SignInPage extends StatelessWidget {
3030
body: SignInWidget(
3131
client: client,
3232
onAuthenticated: () {
33-
// Navigate to home screen
34-
Navigator.of(context).pushReplacement(
35-
MaterialPageRoute(builder: (_) => HomePage()),
36-
);
33+
// Do something when the user is authenticated.
34+
//
35+
// NOTE: You should not navigate to the home screen here, otherwise
36+
// the user will have to sign in again every time they open the app.
3737
},
3838
onError: (error) {
3939
// Handle errors
@@ -62,7 +62,10 @@ SignInWidget(
6262
disableGoogleSignInWidget: false,
6363
disableAppleSignInWidget: true, // Disable Apple sign-in
6464
onAuthenticated: () {
65-
// Handle authentication
65+
// Do something when the user is authenticated.
66+
//
67+
// NOTE: You should not navigate to the home screen here, otherwise
68+
// the user will have to sign in again every time they open the app.
6669
},
6770
)
6871
```

docs/08-upgrading/01-upgrade-to-three.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ class MyEndpoint extends Endpoint {
241241

242242
Serverpod 3.0 includes several changes to the authentication system that improve type safety and performance.
243243

244+
:::note
245+
Besides minor interface changes described in this section, the legacy authentication system is still supported and can be used alongside the new version. This allows to safely upgrade your project to Serverpod 3.0 while using the legacy authentication and gradually migrate to the new one.
246+
:::
247+
244248
### Custom authentication handlers
245249

246250
In the new authentication system, the default authentication header has changed from `Basic` to `Bearer` - which is now officially supported. This introduced a change for custom `AuthenticationHandler` implementations: the `token` parameter will now receive the token unwrapped from the `Bearer` prefix - just as it does for `Basic` tokens.
@@ -304,6 +308,10 @@ Client(host)..authenticationKeyManager = myManager;
304308
Client(host)..authKeyProvider = myProvider;
305309
```
306310

311+
:::note
312+
To keep backwards compatibility with the old authentication system, the `FlutterAuthenticationKeyManager` from the legacy package now implements the `AuthKeyProvider` interface, so you can safely pass it to the client as the `authKeyProvider`.
313+
:::
314+
307315
## Enum serialization
308316

309317
The default enum serialization strategy has changed from `byIndex` to `byName`. This change improves robustness when reordering or adding enum values, as serialized data remains valid even if the enum definition changes. With `byName`, the string representation is stored instead of the numeric index, making your data more resilient and easier to debug.

0 commit comments

Comments
 (0)