Skip to content

Commit ffb2d31

Browse files
committed
Update multiple users examples
1 parent e32b972 commit ffb2d31

13 files changed

+167
-78
lines changed

examples/ios/Examples/CompleteQuickStart.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class CompleteQuickStartTest: XCTestCase {
5858

5959
func testRunExample() {
6060
let expectation = XCTestExpectation(description: "Run complete quick start")
61+
// :code-block-start: quick-start
6162
// Now logged in, do something with user
6263
let user = app.currentUser
6364
let partitionValue = "myPartition"
@@ -126,8 +127,11 @@ class CompleteQuickStartTest: XCTestCase {
126127

127128
// Invalidate notification tokens when done observing
128129
notificationToken.invalidate()
130+
// :hide-start:
129131
expectation.fulfill()
132+
// :hide-end:
130133
}
134+
// :code-block-end
131135
wait(for: [expectation], timeout: 10.0)
132136
}
133137
}

examples/ios/Examples/MultipleUsers.swift

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,94 @@ class MultipleUsers: XCTestCase {
1111
wait(for: [expectation], timeout: 3)
1212
}
1313

14+
func testAddUser() {
15+
let joeExpectation = XCTestExpectation(description: "joe log in completes")
16+
let emmaExpectation = XCTestExpectation(description: "emma log in completes")
17+
// :code-block-start: add-user
18+
let app = App(id: YOUR_REALM_APP_ID)
19+
20+
let joeCredentials = Credentials.emailPassword(email: "[email protected]", password: "passw0rd")
21+
app.login(credentials: joeCredentials) { (joe, error) in
22+
DispatchQueue.main.sync {
23+
guard error == nil else {
24+
print("Login failed: \(error!)")
25+
// :hide-start:
26+
joeExpectation.fulfill()
27+
// :hide-end:
28+
return
29+
}
30+
31+
// The active user is now Joe
32+
assert(joe == app.currentUser)
33+
}
34+
}
35+
36+
let emmaCredentials = Credentials.emailPassword(email: "[email protected]", password: "pa55word")
37+
app.login(credentials: emmaCredentials) { (emma, error) in
38+
DispatchQueue.main.sync {
39+
guard error == nil else {
40+
print("Login failed: \(error!)")
41+
// :hide-start:
42+
emmaExpectation.fulfill()
43+
// :hide-end:
44+
return
45+
}
46+
47+
// The active user is now Emma
48+
assert(emma == app.currentUser)
49+
}
50+
}
51+
// :code-block-end:
52+
wait(for: [joeExpectation, emmaExpectation], timeout: 10)
53+
}
54+
55+
func testListUsers() {
56+
// :code-block-start: list-users
57+
let app = App(id: YOUR_REALM_APP_ID)
58+
let users = app.allUsers
59+
users.forEach({ (key, user) in
60+
print("User: \(key) \(user)")
61+
})
62+
// :code-block-end:
63+
}
64+
65+
func testSwitchUsers() {
66+
// :code-block-start: switch-user
67+
let app = App(id: YOUR_REALM_APP_ID)
68+
// ... log in ...
69+
// :hide-start:
70+
func getSomeOtherUser() -> User {
71+
var result: User? = nil
72+
let expectation = XCTestExpectation(description: "it logs in")
73+
app.login(credentials: Credentials.anonymous) { (user, error) in
74+
result = user!
75+
user!.logOut { (error) in
76+
expectation.fulfill()
77+
}
78+
}
79+
wait(for: [expectation], timeout: 10)
80+
return result!
81+
}
82+
// :hide-end:
83+
// Get another user on the device, for example with `app.allUsers`
84+
let secondUser: User = getSomeOtherUser()
85+
86+
assert(app.currentUser != secondUser)
87+
88+
// Switch to another user
89+
app.switch(to: secondUser)
90+
91+
// The switch-to user becomes the app.currentUser
92+
assert(app.currentUser == secondUser)
93+
// :code-block-end:
94+
}
95+
1496
func testLinkIdentities() {
1597
let expectation = XCTestExpectation(description: "Link user completes")
16-
17-
// :code-block-start: link-identity
98+
99+
// :code-block-start: link-identity
18100
let app = App(id: YOUR_REALM_APP_ID)
19-
101+
20102
func logInAnonymously() {
21103
app.login(credentials: Credentials.anonymous) { (user, error) in
22104
guard error == nil else {
@@ -31,26 +113,25 @@ class MultipleUsers: XCTestCase {
31113
registerNewAccount(anonymousUser: user!)
32114
}
33115
}
34-
116+
35117
func registerNewAccount(anonymousUser: User) {
36118
let email = "[email protected]"
37119
let password = "ganondorf"
38120
app.emailPasswordAuth.registerUser(email: email, password: password) { (error) in
39-
guard error == nil else {
40-
print("Failed to register new account: \(error!.localizedDescription)")
41-
// :hide-start:
42-
XCTAssert(false, "Failed to register new account: \(error!.localizedDescription)")
43-
// :hide-end:
44-
return
45-
}
46-
47-
// Successfully created account, now link it
48-
// with the existing anon user
49-
link(user: anonymousUser, with: Credentials.emailPassword(email: email, password: password))
121+
guard error == nil else {
122+
print("Failed to register new account: \(error!.localizedDescription)")
123+
// :hide-start:
124+
XCTAssert(false, "Failed to register new account: \(error!.localizedDescription)")
125+
// :hide-end:
126+
return
127+
}
128+
129+
// Successfully created account, now link it
130+
// with the existing anon user
131+
link(user: anonymousUser, with: Credentials.emailPassword(email: email, password: password))
50132
}
51-
52133
}
53-
134+
54135
func link(user: User, with credentials: Credentials) {
55136
user.linkUser(credentials: credentials) { (user, error) in
56137
guard error == nil else {
@@ -60,15 +141,15 @@ class MultipleUsers: XCTestCase {
60141
// :hide-end:
61142
return
62143
}
63-
144+
64145
print("Successfully linked user: \(user!)")
65-
146+
66147
// :hide-start:
67148
expectation.fulfill()
68149
// :hide-end:
69150
}
70151
}
71-
152+
72153
logInAnonymously()
73154
// :code-block-end:
74155
wait(for: [expectation], timeout: 10)

source/examples/MultiUser/AddUser/AddUser.swift

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

source/examples/MultiUser/LogoutUser/LogoutUser.swift

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

source/examples/MultiUser/SwitchUser/SwitchUser.swift

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
let app = App(id: YOUR_REALM_APP_ID)
2+
3+
let joeCredentials = Credentials.emailPassword(email: "[email protected]", password: "passw0rd")
4+
app.login(credentials: joeCredentials) { (joe, error) in
5+
DispatchQueue.main.sync {
6+
guard error == nil else {
7+
print("Login failed: \(error!)")
8+
return
9+
}
10+
11+
// The active user is now Joe
12+
assert(joe == app.currentUser)
13+
}
14+
}
15+
16+
let emmaCredentials = Credentials.emailPassword(email: "[email protected]", password: "pa55word")
17+
app.login(credentials: emmaCredentials) { (emma, error) in
18+
DispatchQueue.main.sync {
19+
guard error == nil else {
20+
print("Login failed: \(error!)")
21+
return
22+
}
23+
24+
// The active user is now Emma
25+
assert(emma == app.currentUser)
26+
}
27+
}

source/examples/generated/code/start/MultipleUsers.codeblock.link-identity.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ func registerNewAccount(anonymousUser: User) {
1616
let email = "[email protected]"
1717
let password = "ganondorf"
1818
app.emailPasswordAuth.registerUser(email: email, password: password) { (error) in
19-
guard error == nil else {
20-
print("Failed to register new account: \(error!.localizedDescription)")
21-
return
22-
}
23-
24-
// Successfully created account, now link it
25-
// with the existing anon user
26-
link(user: anonymousUser, with: Credentials.emailPassword(email: email, password: password))
27-
}
19+
guard error == nil else {
20+
print("Failed to register new account: \(error!.localizedDescription)")
21+
return
22+
}
2823

24+
// Successfully created account, now link it
25+
// with the existing anon user
26+
link(user: anonymousUser, with: Credentials.emailPassword(email: email, password: password))
27+
}
2928
}
3029

3130
func link(user: User, with credentials: Credentials) {
@@ -34,9 +33,9 @@ func link(user: User, with credentials: Credentials) {
3433
print("Failed to link user: \(error!.localizedDescription)")
3534
return
3635
}
37-
36+
3837
print("Successfully linked user: \(user!)")
39-
38+
4039
}
4140
}
4241

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
let users = app.allUsers()
1+
let app = App(id: YOUR_REALM_APP_ID)
2+
let users = app.allUsers
23
users.forEach({ (key, user) in
34
print("User: \(key) \(user)")
4-
})
5+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let app = App(id: YOUR_REALM_APP_ID)
2+
// ... log in ...
3+
// Get another user on the device, for example with `app.allUsers`
4+
let secondUser: User = getSomeOtherUser()
5+
6+
assert(app.currentUser != secondUser)
7+
8+
// Switch to another user
9+
app.switch(to: secondUser)
10+
11+
// The switch-to user becomes the app.currentUser
12+
assert(app.currentUser == secondUser)

source/ios/open-a-realm.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ includes the partition name. The following code demonstrates this:
2222

2323
.. code-block:: swift
2424

25-
let user = app.currentUser()
25+
let user = app.currentUser
2626
let partitionValue = "myPartition"
2727

2828
Realm.asyncOpen(configuration: user!.configuration(partitionValue: partitionValue)) { realm, error in

source/ios/quick-start.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ includes the partition name. The following code demonstrates this:
129129

130130
.. code-block:: swift
131131

132-
let user = app.currentUser()
132+
let user = app.currentUser
133133
let partitionValue = "myPartition"
134134

135135
Realm.asyncOpen(configuration: user!.configuration(partitionValue: partitionValue)) { realm, error in
@@ -234,7 +234,7 @@ Once logged in, you can log out:
234234

235235
.. code-block:: swift
236236

237-
app.currentUser()?.logOut() { (error) in
237+
app.currentUser?.logOut { (error) in
238238
// Logged out or error occurred
239239
}
240240

source/ios/sync-data.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ authenticated user's ``configuration`` method:
3333

3434
let app = App(id: "myRealmAppId")
3535
// ... log in ...
36-
let user = app.currentUser()!
36+
let user = app.currentUser!
3737
let partitionValue = "myProject"
3838
let realm = try! Realm(configuration: user.configuration(partitionValue: partitionValue))
3939

@@ -55,7 +55,7 @@ realm, use the ``Realm.asyncOpen()`` method instead.
5555

5656
let app = App(id: "myRealmAppId")
5757
// ...after log in...
58-
let user = app.currentUser()!
58+
let user = app.currentUser!
5959
let partitionValue = "myPartition"
6060
Realm.asyncOpen(configuration: user.configuration(partitionValue: partitionValue),
6161
callback: { (maybeRealm, error) in

source/ios/work-with-multiple-users.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ the application's :ref:`active user <active-user>`.
7676
.. tab::
7777
:tabid: swift
7878

79-
.. literalinclude:: /examples/MultiUser/AddUser/AddUser.swift
79+
.. literalinclude:: /examples/generated/code/start/MultipleUsers.codeblock.add-user.swift
8080
:language: swift
8181

8282
.. _ios-list-users:
@@ -93,7 +93,7 @@ app on a given device regardless of whether they are currently authenticated.
9393
.. tab::
9494
:tabid: swift
9595

96-
.. literalinclude:: /examples/MultiUser/ListUsers/ListUsers.swift
96+
.. literalinclude:: /examples/generated/code/start/MultipleUsers.codeblock.list-users.swift
9797
:language: swift
9898

9999
.. _ios-switch-user:
@@ -109,7 +109,7 @@ logged in user at any time.
109109
.. tab::
110110
:tabid: swift
111111

112-
.. literalinclude:: /examples/MultiUser/SwitchUser/SwitchUser.swift
112+
.. literalinclude:: /examples/generated/code/start/MultipleUsers.codeblock.switch-users.swift
113113
:language: swift
114114

115115
.. _ios-remove-user:
@@ -125,5 +125,5 @@ log the user out.
125125
.. tab::
126126
:tabid: swift
127127

128-
.. literalinclude:: /examples/MultiUser/LogoutUser/LogoutUser.swift
129-
:language: swift
128+
.. literalinclude:: /examples/generated/code/start/Authenticate.codeblock.logout.swift
129+
:language: swift

0 commit comments

Comments
 (0)