Skip to content

Commit 5f1cf98

Browse files
authored
add auth page with code snippets (#512)
* add auth page with code snippets
1 parent 2b46e00 commit 5f1cf98

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+497
-414
lines changed

examples/dotnet/Examples/Examples.cs

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
using System;
2+
using System.Linq;
3+
using dotnet;
4+
using MongoDB.Bson;
5+
using NUnit.Framework;
6+
using Realms;
7+
using Realms.Sync;
8+
9+
namespace UnitTests
10+
{
11+
public class Examples
12+
{
13+
App app;
14+
ObjectId testTaskId;
15+
User user;
16+
SyncConfiguration config;
17+
const string myRealmAppId = "tuts-tijya";
18+
19+
[SetUp]
20+
public async System.Threading.Tasks.Task Setup()
21+
{
22+
// :code-block-start: initialize-realm
23+
app = App.Create(myRealmAppId);
24+
// :code-block-end:
25+
user = app.LogInAsync(Credentials.EmailPassword("[email protected]", "foobar")).Result;
26+
config = new SyncConfiguration("My Project", user);
27+
Realm realm = await Realm.GetInstanceAsync(config);
28+
// :code-block-end:
29+
// :code-block-start: open-synced-realm-sync
30+
Realm synchronousRealm = Realm.GetInstance(config);
31+
// :code-block-end:
32+
// :code-block-start: create
33+
RealmTask testTask = new RealmTask
34+
{
35+
Name = "Do this thing",
36+
Status = TaskStatus.Open.ToString()
37+
};
38+
39+
realm.Write(() =>
40+
{
41+
realm.Add(testTask);
42+
});
43+
// :code-block-end:
44+
testTaskId = testTask.Id;
45+
return;
46+
}
47+
48+
[Test]
49+
public async System.Threading.Tasks.Task GetsSyncedTasks()
50+
{
51+
// :code-block-start: anon-login
52+
User user = app.LogInAsync(Credentials.Anonymous()).Result;
53+
// :code-block-end:
54+
// :code-block-start: config
55+
config = new SyncConfiguration("My Project", user);
56+
Realm realm = await Realm.GetInstanceAsync(config);
57+
// :code-block-end:
58+
// :code-block-start: read-all
59+
var tasks = realm.All<RealmTask>();
60+
// :code-block-end:
61+
Assert.AreEqual(1, tasks.Count());
62+
// :code-block-start: read-some
63+
tasks = realm.All<RealmTask>().Where(t => t.Status == "Open");
64+
// :code-block-end:
65+
Assert.AreEqual(1, tasks.Count());
66+
return;
67+
}
68+
69+
[Test]
70+
public async System.Threading.Tasks.Task ModifiesATask()
71+
{
72+
config = new SyncConfiguration("My Project", user);
73+
Realm realm = await Realm.GetInstanceAsync(config);
74+
// :code-block-start: modify
75+
RealmTask t = realm.All<RealmTask>()
76+
.Where(t => t.Id == testTaskId)
77+
.FirstOrDefault();
78+
79+
realm.Write(() =>
80+
{
81+
t.Status = TaskStatus.InProgress.ToString();
82+
});
83+
84+
// :code-block-end:
85+
var allTasks = realm.All<RealmTask>().ToList();
86+
Assert.AreEqual(1, allTasks.Count);
87+
Assert.AreEqual(TaskStatus.InProgress.ToString(), allTasks.First().Status);
88+
89+
return;
90+
}
91+
92+
[Test]
93+
public async System.Threading.Tasks.Task LogsOnManyWays()
94+
{
95+
// :code-block-start: logon_anon
96+
User anonUser = await app.LogInAsync(Credentials.Anonymous());
97+
// :code-block-end:
98+
Assert.AreEqual(UserState.LoggedIn, anonUser.State);
99+
await anonUser.LogOutAsync();
100+
// :code-block-start: logon_EP
101+
User emailUser = await app.LogInAsync(
102+
Credentials.EmailPassword("[email protected]", "shhhItsASektrit!"));
103+
// :code-block-end:
104+
Assert.AreEqual(UserState.LoggedIn, emailUser.State);
105+
await emailUser.LogOutAsync();
106+
var apiKey = "eRECwv1e6gkLEse99XokWOgegzoguEkwmvYvXk08zAucG4kXmZu7TTgV832SwFCv";
107+
// :code-block-start: logon_API
108+
User apiUser = await app.LogInAsync(Credentials.ApiKey(apiKey));
109+
// :code-block-end:
110+
Assert.AreEqual(UserState.LoggedIn, apiUser.State);
111+
await apiUser.LogOutAsync();
112+
// :code-block-start: logon_Function
113+
var functionParameters = new
114+
{
115+
username= "caleb",
116+
password = "shhhItsASektrit!",
117+
IQ = 42,
118+
isCool = false
119+
};
120+
121+
User functionUser =
122+
await app.LogInAsync(Credentials.Function(functionParameters));
123+
// :code-block-end:
124+
Assert.AreEqual(UserState.LoggedIn, functionUser.State);
125+
await functionUser.LogOutAsync();
126+
var jwt_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkNhbGViIiwiaWF0IjoxNjAxNjc4ODcyLCJleHAiOjI1MTYyMzkwMjIsImF1ZCI6InR1dHMtdGlqeWEifQ.LHbeSI2FDWrlUVOBxe-rasuFiW-etv2Gu5e3eAa6Y6k";
127+
// :code-block-start: logon_JWT
128+
User jwtUser =
129+
await app.LogInAsync(Credentials.JWT(jwt_token));
130+
// :code-block-end:
131+
Assert.AreEqual(UserState.LoggedIn, jwtUser.State);
132+
await jwtUser.LogOutAsync();
133+
try
134+
{
135+
var facebookToken = "";
136+
// :code-block-start: logon_fb
137+
User fbUser =
138+
await app.LogInAsync(Credentials.Facebook(facebookToken));
139+
// :code-block-end:
140+
}
141+
catch (Exception e)
142+
{
143+
Assert.AreEqual("InvalidSession: authentication via 'oauth2-facebook' is unsupported", e.Message);
144+
}
145+
try
146+
{
147+
var googleAuthCode = "";
148+
// :code-block-start: logon_google
149+
User googleUser =
150+
await app.LogInAsync(Credentials.Google(googleAuthCode));
151+
// :code-block-end:
152+
}
153+
catch (Exception e)
154+
{
155+
Assert.AreEqual("InvalidSession: authentication via 'oauth2-google' is unsupported", e.Message);
156+
}
157+
try
158+
{
159+
var appleToken = "";
160+
// :code-block-start: logon_apple
161+
User appleUser =
162+
await app.LogInAsync(Credentials.Apple(appleToken));
163+
// :code-block-end:
164+
}
165+
166+
catch (Exception e)
167+
{
168+
Assert.AreEqual("InvalidSession: authentication via 'oauth2-apple' is unsupported", e.Message);
169+
}
170+
}
171+
172+
[TearDown]
173+
public async System.Threading.Tasks.Task TearDown()
174+
{
175+
config = new SyncConfiguration("My Project", user);
176+
Realm realm = await Realm.GetInstanceAsync(config);
177+
// :code-block-start: delete
178+
realm.Write(() =>
179+
{
180+
realm.RemoveAll<RealmTask>();
181+
});
182+
// :code-block-end:
183+
// :code-block-start: logout
184+
await user.LogOutAsync();
185+
// :code-block-end:
186+
return;
187+
}
188+
}
189+
}

examples/dotnet/Examples/Examples.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
<PackageReference Include="nunit" Version="3.12.0" />
1111
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
13-
<PackageReference Include="Realm.Fody" Version="5.0.1-PR-2041-31">
13+
<PackageReference Include="MongoDB.Bson" Version="2.11.2" />
14+
<PackageReference Include="Realm.Fody" Version="10.0.0-alpha.39">
1415
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1516
<PrivateAssets>all</PrivateAssets>
1617
</PackageReference>
17-
<PackageReference Include="Realm" Version="5.0.1-PR-2041-31" />
18+
<PackageReference Include="Realm" Version="10.0.0-alpha.39" />
1819
</ItemGroup>
1920

2021
<ItemGroup>

examples/dotnet/Examples/Project.cs renamed to examples/dotnet/Examples/RealmProject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace dotnet
55
{
6-
public class Project : RealmObject
6+
public class RealmProject : RealmObject
77
{
88
[PrimaryKey]
99
[MapTo("_id")]

examples/dotnet/Examples/Task.cs renamed to examples/dotnet/Examples/RealmTask.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Realms;
44
namespace dotnet
55
{
6-
public class Task : RealmObject
6+
public class RealmTask : RealmObject
77
{
88
[PrimaryKey]
99
[MapTo("_id")]
@@ -13,7 +13,7 @@ public class Task : RealmObject
1313
public string Partition { get; set; }
1414

1515
[MapTo("assignee")]
16-
public User Assignee { get; set; }
16+
public RealmUser Assignee { get; set; }
1717

1818
[MapTo("name")]
1919
[Required]
@@ -23,7 +23,7 @@ public class Task : RealmObject
2323
[Required]
2424
public string Status { get; set; }
2525

26-
public Task()
26+
public RealmTask()
2727
{
2828
this.Id = ObjectId.GenerateNewId();
2929
}

examples/dotnet/Examples/RealmTests.cs

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

examples/dotnet/Examples/User.cs renamed to examples/dotnet/Examples/RealmUser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace dotnet
55
{
6-
public class User : RealmObject
6+
public class RealmUser : RealmObject
77
{
88
[PrimaryKey]
99
[MapTo("_id")]

examples/dotnet/dotnet.sln

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{C97249A2-33A1-457A-AAEC-389B62984885}"
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}"
55
EndProject
66
Global
77
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -13,17 +13,17 @@ Global
1313
Release|iPhone = Release|iPhone
1414
EndGlobalSection
1515
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16-
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17-
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|Any CPU.Build.0 = Debug|Any CPU
18-
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|Any CPU.ActiveCfg = Release|Any CPU
19-
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
21-
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
22-
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
23-
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
24-
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhone.ActiveCfg = Debug|Any CPU
25-
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhone.Build.0 = Debug|Any CPU
26-
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhone.ActiveCfg = Release|Any CPU
27-
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhone.Build.0 = Release|Any CPU
16+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
21+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
22+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
23+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
24+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|iPhone.ActiveCfg = Debug|Any CPU
25+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|iPhone.Build.0 = Debug|Any CPU
26+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|iPhone.ActiveCfg = Release|Any CPU
27+
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|iPhone.Build.0 = Release|Any CPU
2828
EndGlobalSection
2929
EndGlobal

0 commit comments

Comments
 (0)