You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/06-concepts/11-authentication/02-basics.md
+49Lines changed: 49 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,55 @@ class MyEndpoint extends Endpoint {
38
38
}
39
39
```
40
40
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 {
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
+
41
90
## Authorization on endpoints
42
91
43
92
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