Skip to content

Commit 2e6829c

Browse files
docs: Document how to use the new unauthenticatedClientCall annotation (serverpod#324)
1 parent 97d206e commit 2e6829c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

docs/06-concepts/11-authentication/02-basics.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,55 @@ class MyEndpoint extends Endpoint {
3838
}
3939
```
4040

41+
## Explicitly allowing unauthenticated access
42+
43+
In some cases, you may want to explicitly allow certain endpoints or methods to be accessed without authentication. Serverpod provides the `@unauthenticatedClientCall` annotation for this purpose.
44+
45+
When an endpoint or method is annotated with `@unauthenticatedClientCall`:
46+
- No authentication will be added to the header on the client when calling it.
47+
- The server will receive calls as if there is no user signed in.
48+
49+
:::info
50+
Under the hood, the `@unauthenticatedClientCall` annotation makes the client omit authentication headers for calls to the annotated endpoint or method. On the server side, it ensures that the session is treated as unauthenticated for those calls, regardless of any existing authentication state.
51+
:::
52+
53+
You can use this annotation in two ways:
54+
55+
1. On the entire endpoint class to make all methods unauthenticated:
56+
```dart
57+
@unauthenticatedClientCall
58+
class UnauthenticatedEndpoint extends Endpoint {
59+
Future<bool> someMethod(Session session) async {
60+
return session.isUserSignedIn; // Will always return false
61+
}
62+
63+
Stream<bool> someStream(Session session) async* {
64+
yield await session.isUserSignedIn; // Will always return false
65+
}
66+
}
67+
```
68+
69+
2. On specific methods to make only those methods unauthenticated:
70+
```dart
71+
class PartiallyUnauthenticatedEndpoint extends Endpoint {
72+
@unauthenticatedClientCall
73+
Future<bool> publicMethod(Session session) async {
74+
return session.isUserSignedIn; // Will always return false
75+
}
76+
77+
Future<bool> authenticatedMethod(Session session) async {
78+
return session.isUserSignedIn;
79+
}
80+
}
81+
```
82+
83+
This is particularly useful for endpoints that must not receive authentication, such as JWT refresh endpoints.
84+
85+
:::warning
86+
Using `@unauthenticatedClientCall` on an endpoint or method that also has `requireLogin` set to true will lead to a conflict. Since the client will suppress sending authentication information, but the server will expect it, calls to such endpoints or methods will always fail with an authentication error.
87+
:::
88+
89+
4190
## Authorization on endpoints
4291

4392
Serverpod also supports scopes for restricting access. One or more scopes can be associated with a user. For instance, this can be used to give admin access to a specific user. To restrict access for an endpoint, override the `requiredScopes` property. Note that setting `requiredScopes` implicitly sets `requireLogin` to true.

0 commit comments

Comments
 (0)