/
main.dart
153 lines (137 loc) 路 4.44 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'package:firebase_app_check/firebase_app_check.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'firebase_options.dart';
const kWebRecaptchaSiteKey = '6Lemcn0dAAAAABLkf6aiiHvpGD6x-zF3nOSDU2M8';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
// Activate app check after initialization, but before
// usage of any Firebase services.
await FirebaseAppCheck.instance
// Your personal reCaptcha public key goes here:
.activate(
webRecaptchaSiteKey: kWebRecaptchaSiteKey,
androidDebugProvider: true,
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final String title = 'Firebase App Check';
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase App Check',
home: FirebaseAppCheckExample(title: title),
);
}
}
class FirebaseAppCheckExample extends StatefulWidget {
FirebaseAppCheckExample({
Key? key,
required this.title,
}) : super(key: key);
final String title;
@override
_FirebaseAppCheck createState() => _FirebaseAppCheck();
}
class _FirebaseAppCheck extends State<FirebaseAppCheckExample> {
final appCheck = FirebaseAppCheck.instance;
String _message = '';
String _eventToken = 'not yet';
@override
void initState() {
appCheck.onTokenChange.listen(setEventToken);
super.initState();
}
void setMessage(String message) {
setState(() {
_message = message;
});
}
void setEventToken(String? token) {
setState(() {
_eventToken = token ?? 'not yet';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: [
ElevatedButton(
onPressed: () async {
// Use this button to check whether the request was validated on the Firebase console
// Gets first document in collection
final result = await FirebaseFirestore.instance
.collection('flutter-tests')
.limit(1)
.get();
if (result.docs.isNotEmpty) {
setMessage('Document found');
} else {
setMessage(
'Document not found, please add a document to the collection',
);
}
},
child: const Text('Test App Check validates requests'),
),
ElevatedButton(
onPressed: () async {
if (kIsWeb) {
print(
'Pass in your "webRecaptchaSiteKey" key found on you Firebase Console to activate if using on the web platform.',
);
}
await appCheck.activate(
webRecaptchaSiteKey: kWebRecaptchaSiteKey,
androidDebugProvider: true,
);
setMessage('activated!!');
},
child: const Text('activate()'),
),
ElevatedButton(
onPressed: () async {
// Token will be passed to `onTokenChange()` event handler
await appCheck.getToken(true);
},
child: const Text('getToken()'),
),
ElevatedButton(
onPressed: () async {
await appCheck.setTokenAutoRefreshEnabled(true);
setMessage('successfully set auto token refresh!!');
},
child: const Text('setTokenAutoRefreshEnabled()'),
),
const SizedBox(height: 20),
Text(
_message, //#007bff
style: const TextStyle(
color: Color.fromRGBO(47, 79, 79, 1),
fontSize: 16,
),
),
const SizedBox(height: 20),
Text(
'Token received from tokenChanges() API: $_eventToken', //#007bff
style: const TextStyle(
color: Color.fromRGBO(128, 0, 128, 1),
fontSize: 16,
),
),
],
),
),
);
}
}