Skip to content

Commit bd1bc06

Browse files
authored
Added snippets (#8)
* Added snippets * Added newline at eof * Inlined AppOptions instantiation
1 parent e0551b6 commit bd1bc06

File tree

4 files changed

+211
-1
lines changed

4 files changed

+211
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Google.Apis.Auth" Version="1.35.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\FirebaseAdmin\FirebaseAdmin.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2018, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using FirebaseAdmin;
17+
using FirebaseAdmin.Auth;
18+
using Google.Apis.Auth.OAuth2;
19+
20+
namespace FirebaseAdmin.Snippets
21+
{
22+
class FirebaseAppSnippets
23+
{
24+
static void InitSdkWithServiceAccount()
25+
{
26+
// [START initialize_sdk_with_service_account]
27+
FirebaseApp.Create(new AppOptions()
28+
{
29+
Credential = GoogleCredential.FromFile("path/to/serviceAccountKey.json"),
30+
});
31+
// [END initialize_sdk_with_service_account]
32+
}
33+
34+
static void InitSdkWithApplicationDefault()
35+
{
36+
// [START initialize_sdk_with_application_default]
37+
FirebaseApp.Create(new AppOptions()
38+
{
39+
Credential = GoogleCredential.GetApplicationDefault(),
40+
});
41+
// [END initialize_sdk_with_application_default]
42+
}
43+
44+
static void InitSdkWithRefreshToken()
45+
{
46+
// [START initialize_sdk_with_refresh_token]
47+
FirebaseApp.Create(new AppOptions()
48+
{
49+
Credential = GoogleCredential.FromFile("path/to/refreshToken.json"),
50+
});
51+
// [END initialize_sdk_with_refresh_token]
52+
}
53+
54+
static void InitSdkWithDefaultConfig()
55+
{
56+
// [START initialize_sdk_with_default_config]
57+
FirebaseApp.Create();
58+
// [END initialize_sdk_with_default_config]
59+
}
60+
61+
static void InitDefaultApp()
62+
{
63+
// [START access_services_default]
64+
// Initialize the default app
65+
var defaultApp = FirebaseApp.Create(new AppOptions()
66+
{
67+
Credential = GoogleCredential.GetApplicationDefault(),
68+
});
69+
Console.WriteLine(defaultApp.Name); // "[DEFAULT]"
70+
71+
// Retrieve services by passing the defaultApp variable...
72+
var defaultAuth = FirebaseAuth.GetAuth(defaultApp);
73+
74+
// ... or use the equivalent shorthand notation
75+
defaultAuth = FirebaseAuth.DefaultInstance;
76+
// [END access_services_default]
77+
}
78+
79+
static void InitCustomApp()
80+
{
81+
var defaultOptions = new AppOptions()
82+
{
83+
Credential = GoogleCredential.GetApplicationDefault(),
84+
};
85+
var otherAppConfig = new AppOptions()
86+
{
87+
Credential = GoogleCredential.GetApplicationDefault(),
88+
};
89+
90+
// [START access_services_nondefault]
91+
// Initialize the default app
92+
var defaultApp = FirebaseApp.Create(defaultOptions);
93+
94+
// Initialize another app with a different config
95+
var otherApp = FirebaseApp.Create(otherAppConfig, "other");
96+
97+
Console.WriteLine(defaultApp.Name); // "[DEFAULT]"
98+
Console.WriteLine(otherApp.Name); // "other"
99+
100+
// Use the shorthand notation to retrieve the default app's services
101+
var defaultAuth = FirebaseAuth.DefaultInstance;
102+
103+
// Use the otherApp variable to retrieve the other app's services
104+
var otherAuth = FirebaseAuth.GetAuth(otherApp);
105+
// [END access_services_nondefault]
106+
}
107+
108+
static void InitWithServiceAccountId()
109+
{
110+
// [START initialize_sdk_with_service_account_id]
111+
FirebaseApp.Create(new AppOptions()
112+
{
113+
Credential = GoogleCredential.GetApplicationDefault(),
114+
ServiceAccountId = "[email protected]",
115+
});
116+
// [END initialize_sdk_with_service_account_id]
117+
}
118+
}
119+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2018, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Threading.Tasks;
18+
using FirebaseAdmin.Auth;
19+
20+
namespace FirebaseAdmin.Snippets
21+
{
22+
class FirebaseAuthSnippets
23+
{
24+
static async Task CreateCustomTokenAsync()
25+
{
26+
// [START custom_token]
27+
var uid = "some-uid";
28+
29+
string customToken = await FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(uid);
30+
// Send token back to client
31+
// [END custom_token]
32+
Console.WriteLine("Created custom token: {0}", customToken);
33+
}
34+
35+
static async Task CreateCustomTokenWithClaimsAsync()
36+
{
37+
// [START custom_token_with_claims]
38+
var uid = "some-uid";
39+
var additionalClaims = new Dictionary<string, object>()
40+
{
41+
{ "premiumAccount", true },
42+
};
43+
44+
string customToken = await FirebaseAuth.DefaultInstance
45+
.CreateCustomTokenAsync(uid, additionalClaims);
46+
// Send token back to client
47+
// [END custom_token_with_claims]
48+
Console.WriteLine("Created custom token: {0}", customToken);
49+
}
50+
51+
static async Task VeridyIdTokenAsync(string idToken)
52+
{
53+
// [START verify_id_token]
54+
FirebaseToken decodedToken = await FirebaseAuth.DefaultInstance
55+
.VerifyIdTokenAsync(idToken);
56+
string uid = decodedToken.Uid;
57+
// [END verify_id_token]
58+
Console.WriteLine("Decoded ID token from user: {0}", uid);
59+
}
60+
}
61+
}

FirebaseAdmin/FirebaseAdmin.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.26124.0
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirebaseAdmin.Tests", "Fire
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirebaseAdmin.IntegrationTests", "FirebaseAdmin.IntegrationTests\FirebaseAdmin.IntegrationTests.csproj", "{709FCC13-249A-4752-972F-B770B0652919}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirebaseAdmin.Snippets", "FirebaseAdmin.Snippets\FirebaseAdmin.Snippets.csproj", "{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -58,5 +60,17 @@ Global
5860
{709FCC13-249A-4752-972F-B770B0652919}.Release|x64.Build.0 = Release|Any CPU
5961
{709FCC13-249A-4752-972F-B770B0652919}.Release|x86.ActiveCfg = Release|Any CPU
6062
{709FCC13-249A-4752-972F-B770B0652919}.Release|x86.Build.0 = Release|Any CPU
63+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Debug|Any CPU.Build.0 = Debug|Any CPU
65+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Debug|x64.ActiveCfg = Debug|x64
66+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Debug|x64.Build.0 = Debug|x64
67+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Debug|x86.ActiveCfg = Debug|x86
68+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Debug|x86.Build.0 = Debug|x86
69+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Release|Any CPU.ActiveCfg = Release|Any CPU
70+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Release|Any CPU.Build.0 = Release|Any CPU
71+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Release|x64.ActiveCfg = Release|x64
72+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Release|x64.Build.0 = Release|x64
73+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Release|x86.ActiveCfg = Release|x86
74+
{2AEFFFD2-2C76-492B-9326-01BC75B8A79B}.Release|x86.Build.0 = Release|x86
6175
EndGlobalSection
6276
EndGlobal

0 commit comments

Comments
 (0)