Skip to content

Commit 60a2201

Browse files
committed
add note about cookie authentication to docs
1 parent 17e7aca commit 60a2201

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

docs/docs/advanced/authentication.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ layering the foundation for more advanced authentication. See below for more det
1414

1515
## Dart Frog Auth
1616

17-
The authentication methods provided in `dart_frog_auth` are based on `Authorization` specification,
18-
as defined in [`General HTTP`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). Here you will find support
19-
for `Basic` and `Bearer` authentications, which are common authentication methods used by many developers.
17+
The authentication methods provided in `dart_frog_auth` use different HTTP headers depending on the method. Basic and Bearer authentication use the `Authorization` header, as defined in [`General HTTP`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication), while Cookie-based authentication uses the `Cookie` header, as defined in [`HTTP Cookies`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies). The package provides support for Basic, Bearer, and Cookie-based authentications, which are common authentication methods used by many developers.
2018

2119
## Basic Authentication
2220

@@ -153,6 +151,50 @@ Response onRequest(RequestContext context) {
153151

154152
In the case of `null` being returned (unauthenticated), the middleware will automatically send an unauthorized `401` in the response.
155153

154+
### Cookie-based Authentication
155+
156+
To implement cookie-based authentication, you can use the `cookieAuthentication` middleware:
157+
158+
```dart
159+
// routes/admin/_middleware.dart
160+
import 'package:dart_frog/dart_frog.dart';
161+
import 'package:dart_frog_auth/dart_frog_auth.dart';
162+
import 'package:blog/user.dart';
163+
164+
Handler middleware(Handler handler) {
165+
final userRepository = ...;
166+
return handler
167+
.use(requestLogger())
168+
.use(
169+
cookieAuthentication<User>(
170+
authenticator: (context, cookies) {
171+
final userRepository = context.read<UserRepository>();
172+
return userRepository.fetchFromAccessCookies(cookies);
173+
}
174+
),
175+
);
176+
}
177+
```
178+
179+
The `authenticator` parameter must be a function that receives two positional argument the
180+
context and the cookies set in the cookie header and returns a user if any is found
181+
for that token.
182+
183+
Just like in the basic and bearer methods, if a user is returned, it will be set in the request
184+
context and can be read on request handlers, for example:
185+
186+
```dart
187+
import 'package:dart_frog/dart_frog.dart';
188+
import 'package:blog/user.dart';
189+
190+
Response onRequest(RequestContext context) {
191+
final user = context.read<User>();
192+
return Response.json(body: {'user': user.id});
193+
}
194+
```
195+
196+
In the case of `null` being returned (unauthenticated), the middleware will automatically send an unauthorized `401` in the response.
197+
156198
### Filtering Routes
157199

158200
In many instances, developers will want to apply authentication to some routes, while not to others.

0 commit comments

Comments
 (0)