-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathauth_example.dart
More file actions
197 lines (175 loc) · 6.06 KB
/
auth_example.dart
File metadata and controls
197 lines (175 loc) · 6.06 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2024, the dart_firebase_admin project authors. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.
import 'package:dart_firebase_admin/auth.dart';
import 'package:dart_firebase_admin/dart_firebase_admin.dart';
Future<void> authExample(FirebaseApp admin) async {
print('\n### Auth Example ###\n');
final auth = admin.auth();
UserRecord? user;
try {
print('> Check if user with email exists: test@example.com\n');
user = await auth.getUserByEmail('test@example.com');
print('> User found by email\n');
} on FirebaseAuthAdminException catch (e) {
if (e.errorCode == AuthClientErrorCode.userNotFound) {
print('> User not found, creating new user\n');
user = await auth.createUser(
CreateRequest(email: 'test@example.com', password: 'Test@12345'),
);
} else {
print('> Auth error: ${e.errorCode} - ${e.message}');
}
} catch (e, stackTrace) {
print('> Unexpected error: $e');
print('Stack trace: $stackTrace');
}
if (user != null) {
print('Fetched user email: ${user.email}');
}
}
Future<void> projectConfigExample(FirebaseApp admin) async {
print('\n### Project Config Example ###\n');
final projectConfigManager = admin.auth().projectConfigManager;
try {
// Get current project configuration
print('> Fetching current project configuration...\n');
final config = await projectConfigManager.getProjectConfig();
// Display current configuration
print('Current project configuration:');
if (config.emailPrivacyConfig != null) {
print(
' - Email Privacy: ${config.emailPrivacyConfig!.enableImprovedEmailPrivacy}',
);
}
if (config.passwordPolicyConfig != null) {
print(
' - Password Policy: ${config.passwordPolicyConfig!.enforcementState}',
);
}
if (config.smsRegionConfig != null) {
print(' - SMS Region Config: enabled');
}
if (config.mobileLinksConfig != null) {
print(' - Mobile Links: ${config.mobileLinksConfig!.domain?.value}');
}
print('');
// Example: Update email privacy configuration
print('> Updating email privacy configuration...\n');
final updatedConfig = await projectConfigManager.updateProjectConfig(
UpdateProjectConfigRequest(
emailPrivacyConfig: EmailPrivacyConfig(
enableImprovedEmailPrivacy: true,
),
),
);
print('Configuration updated successfully!');
if (updatedConfig.emailPrivacyConfig != null) {
print(
' - Improved Email Privacy: ${updatedConfig.emailPrivacyConfig!.enableImprovedEmailPrivacy}',
);
}
} on FirebaseAuthAdminException catch (e) {
print('> Auth error: ${e.code} - ${e.message}');
} catch (e) {
print('> Error managing project config: $e');
}
}
/// Tenant management example.
///
/// Steps to enable Identity Platform:
///
/// 1. Go to Google Cloud Console (not Firebase Console):
/// - Visit: https://console.cloud.google.com/
/// - Select your project
///
/// 2. Enable Identity Platform API:
/// - In the search bar, search for "Identity Platform"
/// - Click on "Identity Platform"
/// - Click "Enable API" if not already enabled
///
/// 3. Upgrade to Identity Platform:
/// - Once in Identity Platform, look for an "Upgrade" or "Get Started" button
/// - Follow the prompts to upgrade from Firebase Auth to Identity Platform
///
/// 4. Enable Multi-tenancy:
/// - After upgrading, go to Settings
/// - Look for "Multi-tenancy" option
/// - Enable it
Future<void> tenantExample(FirebaseApp admin) async {
print('\n### Tenant Example ###\n');
final tenantManager = admin.auth().tenantManager;
String? createdTenantId;
try {
print('> Creating a new tenant...\n');
final newTenant = await tenantManager.createTenant(
UpdateTenantRequest(
displayName: 'example-tenant',
emailSignInConfig: EmailSignInProviderConfig(
enabled: true,
passwordRequired: true,
),
),
);
createdTenantId = newTenant.tenantId;
print('Tenant created successfully!');
print(' - Tenant ID: ${newTenant.tenantId}');
print(' - Display Name: ${newTenant.displayName}');
print('');
// Get the tenant
print('> Fetching tenant details...\n');
final tenant = await tenantManager.getTenant(createdTenantId);
print('Tenant details:');
print(' - ID: ${tenant.tenantId}');
print(' - Display Name: ${tenant.displayName}');
print('');
// Update the tenant
print('> Updating tenant...\n');
final updatedTenant = await tenantManager.updateTenant(
createdTenantId,
UpdateTenantRequest(displayName: 'updated-tenant'),
);
print('Tenant updated successfully!');
print(' - New Display Name: ${updatedTenant.displayName}');
print('');
// List tenants
print('> Listing all tenants...\n');
final listResult = await tenantManager.listTenants();
print('Found ${listResult.tenants.length} tenant(s)');
for (final t in listResult.tenants) {
print(' - ${t.tenantId}: ${t.displayName}');
}
print('');
// Delete the tenant
print('> Deleting tenant...\n');
await tenantManager.deleteTenant(createdTenantId);
print('Tenant deleted successfully!');
} on FirebaseAuthAdminException catch (e) {
if (e.code == 'auth/invalid-project-id') {
print('> Multi-tenancy is not enabled for this project.');
print(
' Enable it in Firebase Console under Identity Platform settings.',
);
} else {
print('> Auth error: ${e.code} - ${e.message}');
}
// Clean up if tenant was created
if (createdTenantId != null) {
try {
await tenantManager.deleteTenant(createdTenantId);
} catch (_) {
// Ignore cleanup errors
}
}
} catch (e) {
print('> Error managing tenants: $e');
// Clean up if tenant was created
if (createdTenantId != null) {
try {
await tenantManager.deleteTenant(createdTenantId);
} catch (_) {
// Ignore cleanup errors
}
}
}
}