Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit c8a5a6c

Browse files
authored
Firebase Auth integration for HTTP functions. (#3)
* Test Firebase Auth. * Update path inside git. * Successful auth request. * Refactor FirebaseAuthValidator. * Test with updated auth validator. * Update init. * Use env variable for project id for local builds. * Update Http generation strategy. * Update auth callback. * Update method name in validator callback. * Show IdToken in Dartblaze exports. * Update exports on Dartblaze. * Update http strategy and dartblaze exports. * Update IdToken param validity. * Update gitignore. * Fix example. * Dartblaze 0.0.2. * Prepare dartblaze_builder for new version publishing. * Update example and use latest packages. * Update Http functions docs.
1 parent 55b9126 commit c8a5a6c

File tree

23 files changed

+445
-860
lines changed

23 files changed

+445
-860
lines changed

docs/docs/functions/http-functions.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ Future<Response> updateTodoLogger(Todo todo, RequestLogger logger) async {
7878
}
7979
```
8080

81+
## Authentication
82+
83+
All `@Http` functions have Firebase Auth integration enabled by default. This means that the function expects `Authorization: Bearer <token>` header when being called from client.
84+
85+
Token is Firebase id token, which you can get on the client by calling `await FirebaseAuth.instance.currentUser.getIdToken()`.
86+
87+
You can disable auth for a function in the header: `@Http(auth: false)`.
88+
89+
90+
8191
## :warning: Current Limitations
8292

8393
1. **No Body Validation**: The annotation does not currently support automatic request body validation. Validation must be implemented manually within the handler.

docs/docs/testing.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Todo with _$Todo {
3737
factory Todo.fromJson(Map<String, dynamic> json) => _$TodoFromJson(json);
3838
}
3939
40-
@Http()
40+
@Http(auth:false)
4141
Future<Response> updateTodo(Todo todo) async {
4242
firestore.collection('todos').doc(todo.id).update(todo.toJson());
4343
return Response.ok('Todo updated: ${todo.id}');
@@ -59,4 +59,10 @@ curl -X POST http://localhost:8080/todos \
5959
As a result, you should see this in your terminal:
6060
```bash
6161
Todo updated: 123
62-
```
62+
```
63+
64+
::: tip
65+
All `@Http` functions have Firebase Auth integration enabled by default. This means that the function expects `Authorization: Bearer <token>` header when being called from client.
66+
67+
Check [authorization](./functions/http-functions.md#authentication) section of Http function to see how to get the token.
68+
:::

example/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# https://dart.dev/guides/libraries/private-files
22
# Created by `dart pub`
33
.dart_tool/
4+
pubspec.lock
45

56
# logs
67
firebase-debug.log
@@ -14,6 +15,7 @@ service-account.json
1415
# Generated files
1516
/lib/**/*.freezed.dart
1617
/lib/**/*.g.dart
18+
bin/server.dart
1719

1820
# Deployment files
1921
functions.json
@@ -22,3 +24,7 @@ functions.json
2224
.firebaserc
2325
firebase.json
2426
project.json
27+
28+
# IDE files
29+
.idea/
30+
.vscode/

example/bin/server.dart

Lines changed: 0 additions & 91 deletions
This file was deleted.

example/lib/functions/http.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ Future<Response> updateTodo(Todo todo) async {
99
return Response.ok('Todo updated: ${todo.id}');
1010
}
1111

12+
@Http(auth: false)
13+
Future<Response> updateTodoNoAuth(Todo todo) async {
14+
firestore.collection('todos').doc(todo.id).update(todo.toJson());
15+
return Response.ok('Todo updated: ${todo.id}');
16+
}
17+
1218
@Http()
13-
Future<Response> updateTodoRequest(Request request) async {
19+
Future<Response> updateTodoRequest(
20+
Request request, {
21+
required IdToken authToken,
22+
}) async {
1423
final todo = await request.body.as(Todo.fromJson);
1524
firestore.collection('todos').doc(todo.id).update(todo.toJson());
1625
return Response.ok('Todo updated: ${todo.id}');

0 commit comments

Comments
 (0)