Skip to content

Commit cee2865

Browse files
mongodbennlarew
andauthored
(DOCSP-27598): Clean up web auth examples (#2640)
## Pull Request Info Clean up auth examples ### Jira - https://jira.mongodb.org/browse/DOCSP-27598 ### Staged Changes - [Authentication - Web SDK](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-27598/web/authenticate) ### Reminder Checklist If your PR modifies the docs, you might need to also update some corresponding pages. Check if completed or N/A. - [x] Create Jira ticket for corresponding docs-app-services update(s), if any - [x] Checked/updated Admin API - [x] Checked/updated CLI reference ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md) --------- Co-authored-by: Nick Larew <[email protected]>
1 parent edcbf82 commit cee2865

14 files changed

+184
-274
lines changed

examples/web/src/__tests__/authentication-log-in-and-out.test.js

Lines changed: 97 additions & 165 deletions
Large diffs are not rendered by default.
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
async function loginAnonymous() {
22
// Create an anonymous credential
33
const credentials = Realm.Credentials.anonymous();
4-
try {
5-
// Authenticate the user
6-
const user = await app.logIn(credentials);
7-
// `App.currentUser` updates to match the logged in user
8-
console.assert(user.id === app.currentUser.id);
9-
return user;
10-
} catch (err) {
11-
console.error("Failed to log in", err);
12-
}
4+
5+
// Authenticate the user
6+
const user = await app.logIn(credentials);
7+
// `App.currentUser` updates to match the logged in user
8+
console.assert(user.id === app.currentUser.id);
9+
10+
return user;
1311
}
1412
const user = await loginAnonymous();
15-
console.log("Successfully logged in!", user.id);
16-
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
async function loginApiKey(apiKey) {
22
// Create an API Key credential
33
const credentials = Realm.Credentials.apiKey(apiKey);
4-
try {
5-
// Authenticate the user
6-
const user = await app.logIn(credentials);
7-
// `App.currentUser` updates to match the logged in user
8-
console.assert(user.id === app.currentUser.id);
9-
return user;
10-
} catch (err) {
11-
console.error("Failed to log in", err);
12-
}
4+
5+
// Authenticate the user
6+
const user = await app.logIn(credentials);
7+
8+
// `App.currentUser` updates to match the logged in user
9+
console.assert(user.id === app.currentUser.id);
10+
11+
return user;
1312
}
13+
1414
const user = await loginApiKey(REALM_API_KEY.key); // add previously generated API key
15-
console.log("Successfully logged in!", user);
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// Get the ID token from the Apple SDK
2-
AppleID.auth
3-
.signIn()
4-
.then(({ id_token }) => {
5-
// Define credentials with the ID token from the Apple SDK
6-
const credentials = Realm.Credentials.apple(id_token);
7-
// Log the user in to your app
8-
return app.logIn(credentials);
9-
})
10-
.then((user) => {
11-
console.log(`Logged in with id: ${user.id}`);
12-
});
2+
const { id_token } = await AppleID.auth.signIn();
3+
4+
// Define credentials with the ID token from the Apple SDK
5+
const credentials = Realm.Credentials.apple(id_token);
6+
7+
// Log the user in to your app
8+
const user = await app.logIn(credentials);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// When the user is redirected back to your app, handle the redirect to
2+
// save the user's access token and close the redirect window. This
3+
// returns focus to the original application window and automatically
4+
// logs the user in.
5+
Realm.handleAuthRedirect();

source/examples/generated/web/authentication-log-in-and-out.test.snippet.builtin-apple-oauth.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@ const redirectUri = "https://app.example.com/handleOAuthLogin";
44
const credentials = Realm.Credentials.apple(redirectUri);
55

66
// Calling logIn() opens an Apple authentication screen in a new window.
7-
app.logIn(credentials).then((user) => {
8-
// The logIn() promise will not resolve until you call `handleAuthRedirect()`
9-
// from the new window after the user has successfully authenticated.
10-
console.log(`Logged in with id: ${user.id}`);
11-
});
7+
const user = app.logIn(credentials);
128

13-
// When the user is redirected back to your app, handle the redirect to
14-
// save the user's access token and close the redirect window. This
15-
// returns focus to the original application window and automatically
16-
// logs the user in.
17-
Realm.handleAuthRedirect();
9+
// The logIn() promise will not resolve until you call `Realm.handleAuthRedirect()`
10+
// from the new window after the user has successfully authenticated.
11+
console.log(`Logged in with id: ${user.id}`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// When the user is redirected back to your app, handle the redirect to
2+
// save the user's access token and close the redirect window. This
3+
// returns focus to the original application window and automatically
4+
// logs the user in.
5+
Realm.handleAuthRedirect();

source/examples/generated/web/authentication-log-in-and-out.test.snippet.builtin-facebook-oauth.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,8 @@ const redirectUri = "https://app.example.com/handleOAuthLogin";
44
const credentials = Realm.Credentials.facebook(redirectUri);
55

66
// Calling logIn() opens a Facebook authentication screen in a new window.
7-
app.logIn(credentials).then((user) => {
8-
// The logIn() promise will not resolve until you call `handleAuthRedirect()`
9-
// from the new window after the user has successfully authenticated.
10-
console.log(`Logged in with id: ${user.id}`);
11-
});
7+
const user = await app.logIn(credentials);
128

13-
// When the user is redirected back to your app, handle the redirect to
14-
// save the user's access token and close the redirect window. This
15-
// returns focus to the original application window and automatically
16-
// logs the user in.
17-
Realm.handleAuthRedirect();
9+
// The app.logIn() promise will not resolve until you call `Realm.handleAuthRedirect()`
10+
// from the new window after the user has successfully authenticated.
11+
console.log(`Logged in with id: ${user.id}`);
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
async function loginCustomFunction(payload) {
22
// Create a Custom Function credential
33
const credentials = Realm.Credentials.function(payload);
4-
try {
5-
// Authenticate the user
6-
const user = await app.logIn(credentials);
7-
// `App.currentUser` updates to match the logged in user
8-
console.assert(user.id === app.currentUser.id);
9-
return user;
10-
} catch (err) {
11-
console.error("Failed to log in", err);
12-
}
4+
5+
// Authenticate the user
6+
const user = await app.logIn(credentials);
7+
// `App.currentUser` updates to match the logged in user
8+
console.assert(user.id === app.currentUser.id);
9+
10+
return user;
1311
}
14-
loginCustomFunction({ username: "ilovemongodb" }).then((user) => {
15-
console.log("Successfully logged in!", user);
16-
});
12+
const user = await loginCustomFunction({ username: "ilovemongodb" });
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
async function loginCustomJwt(jwt) {
22
// Create a Custom JWT credential
33
const credentials = Realm.Credentials.jwt(jwt);
4-
try {
5-
// Authenticate the user
6-
const user = await app.logIn(credentials);
7-
// `App.currentUser` updates to match the logged in user
8-
console.assert(user.id === app.currentUser.id);
9-
return user;
10-
} catch (err) {
11-
console.error("Failed to log in", err);
12-
}
4+
5+
// Authenticate the user
6+
const user = await app.logIn(credentials);
7+
// `App.currentUser` updates to match the logged in user
8+
console.assert(user.id === app.currentUser.id);
9+
10+
return user;
1311
}
14-
loginCustomJwt("eyJ0eXAi...Q3NJmnU8oP3YkZ8").then((user) => {
15-
console.log("Successfully logged in!", user);
16-
});
12+
const user = await loginCustomJwt("eyJ0eXAi...Q3NJmnU8oP3YkZ8");
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
async function loginEmailPassword(email, password) {
22
// Create an email/password credential
33
const credentials = Realm.Credentials.emailPassword(email, password);
4-
try {
5-
// Authenticate the user
6-
const user = await app.logIn(credentials);
7-
// `App.currentUser` updates to match the logged in user
8-
console.assert(user.id === app.currentUser.id);
9-
return user;
10-
} catch (err) {
11-
console.error("Failed to log in", err);
12-
}
4+
5+
// Authenticate the user
6+
const user = await app.logIn(credentials);
7+
// `App.currentUser` updates to match the logged in user
8+
console.assert(user.id === app.currentUser.id);
9+
10+
return user;
1311
}
1412

1513
const user = await loginEmailPassword("[email protected]", "passw0rd");
16-
console.log("Successfully logged in!", user);

source/examples/generated/web/authentication-log-in-and-out.test.snippet.facebook-sdk-oauth.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ const { accessToken } = FB.getAuthResponse();
33
// Define credentials with the access token from the Facebook SDK
44
const credentials = Realm.Credentials.facebook(accessToken);
55
// Log the user in to your app
6-
app.logIn(credentials).then((user) => {
7-
console.log(`Logged in with id: ${user.id}`);
8-
});
6+
await app.logIn(credentials);

source/web/authenticate.txt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ To log in, create an anonymous credential and pass it to ``App.logIn()``:
3535

3636
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.anon-auth.js
3737
:language: javascript
38-
:emphasize-lines: 3, 6
3938

4039
.. _web-login-email-password:
4140

@@ -51,7 +50,6 @@ password and pass it to ``App.logIn()``:
5150

5251
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.email-password-auth.js
5352
:language: javascript
54-
:emphasize-lines: 3, 6
5553

5654
.. _web-login-api-key:
5755

@@ -66,7 +64,6 @@ API key and pass it to ``App.logIn()``:
6664

6765
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.api-key-auth.js
6866
:language: javascript
69-
:emphasize-lines: 3, 6
7067

7168
.. _web-login-custom-function:
7269

@@ -82,7 +79,6 @@ with a payload object and pass it to ``App.logIn()``:
8279

8380
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.custom-function-auth.js
8481
:language: javascript
85-
:emphasize-lines: 3, 6
8682

8783
.. _web-login-custom-jwt:
8884

@@ -99,7 +95,6 @@ and pass it to ``App.logIn()``:
9995

10096
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.custom-jwt.js
10197
:language: javascript
102-
:emphasize-lines: 3, 6
10398

10499

105100
.. _web-login-facebook:
@@ -129,16 +124,19 @@ require you to install the Facebook SDK. The built in flow follows three main st
129124
specify a URL for your app that is also listed as a redirect URI in the
130125
provider configuration.
131126

127+
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.builtin-facebook-oauth.js
128+
:language: javascript
129+
132130
2. A new window opens to a Facebook authentication screen and the user
133131
authenticates and authorizes your app in that window. Once complete, the new
134132
window redirects to the URL specified in the credential.
135133

136-
3. You call ``handleAuthRedirect()`` on the redirected page, which stores the
134+
3. You call ``Realm.handleAuthRedirect()`` on the redirected page, which stores the
137135
user's Atlas App Services access token and closes the window. Your original app window will
138136
automatically detect the access token and finish logging the user in.
139137

140-
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.builtin-facebook-oauth.js
141-
:language: javascript
138+
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.builtin-facebook-oauth-redirect.js
139+
:language: javascript
142140

143141
.. _web-facebook-auth-token:
144142

@@ -195,7 +193,7 @@ is a Google SDK that lets users sign up to or log in to a website with one click
195193
This example shows how to set up Google Authentication with App Services in
196194
a very basic web app. The app only contains an ``index.html`` file.
197195

198-
.. code-block:: html
196+
.. code-block:: xml
199197
:caption: index.html
200198

201199
<!DOCTYPE html>
@@ -257,7 +255,7 @@ require you to install a Google SDK. The built-in flow follows three main steps:
257255
authenticates and authorizes your app in that window. Once complete, the new
258256
window redirects to the URL specified in the credential.
259257

260-
#. Call ``handleAuthRedirect()`` on the redirected page, which stores the
258+
#. Call ``Realm.handleAuthRedirect()`` on the redirected page, which stores the
261259
user's App Services access token and closes the window. Your original app window will
262260
automatically detect the access token and finish logging the user in.
263261

@@ -270,9 +268,9 @@ require you to install a Google SDK. The built-in flow follows three main steps:
270268

271269
.
272270
├── auth.html
273-
── index.html
271+
── index.html
274272

275-
.. code-block:: html
273+
.. code-block:: xml
276274
:caption: index.html
277275

278276
<!DOCTYPE html>
@@ -311,7 +309,7 @@ require you to install a Google SDK. The built-in flow follows three main steps:
311309
</script>
312310
</html>
313311

314-
.. code-block:: html
312+
.. code-block:: xml
315313
:caption: auth.html
316314

317315
<!DOCTYPE html>
@@ -367,16 +365,19 @@ steps:
367365
specify a URL for your app that is also listed as a redirect URI in the
368366
provider configuration.
369367

368+
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.builtin-apple-oauth.js
369+
:language: javascript
370+
370371
2. A new window opens to an Apple authentication screen and the user
371372
authenticates and authorizes your app in that window. Once complete, the new
372373
window redirects to the URL specified in the credential.
373374

374-
3. You call ``handleAuthRedirect()`` on the redirected page, which stores the
375+
3. You call ``Realm.handleAuthRedirect()`` on the redirected page, which stores the
375376
user's App Services access token and closes the window. Your original app window will
376377
automatically detect the access token and finish logging the user in.
377378

378-
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.builtin-apple-oauth.js
379-
379+
.. literalinclude:: /examples/generated/web/authentication-log-in-and-out.test.snippet.builtin-apple-oath-handle-redirect.js
380+
:language: javascript
380381

381382
.. _web-apple-auth-token:
382383

source/web/mongodb.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ Find Multiple Documents
178178

179179
You can find multiple documents using :js-sdk:`collection.find() <Realm.MongoDBCollection.html#find>`.
180180

181-
The following snippet finds all documents that describe perennial plants in the
182-
:ref:`collection of documents that describe plants for sale in a group of stores
183-
<web-mongodb-example-dataset>`:
184-
185181
The following snippet finds all documents in a
186182
:ref:`collection of documents that describe plants for sale in a group of stores
187183
<web-mongodb-example-dataset>` that contain a field named
@@ -195,6 +191,11 @@ Running this snippet produces output resembling the following:
195191
.. literalinclude:: /examples/generated/web/mongodb-data-access.test.snippet.find-multiple-documents-result.js
196192
:language: javascript
197193

194+
You **cannot** use the ``.skip()`` operator with the Realm SDK.
195+
To paginate results, you can use range queries with the ``.sort()`` and ``.limit()``,
196+
as described in :manual:`Using Range Queries </reference/method/cursor.skip/#pagination-example>`
197+
documentation in the MongoDB Manual.
198+
198199
.. _web-mongodb-count:
199200

200201
Count Documents in the Collection

0 commit comments

Comments
 (0)