Skip to content

Commit 78382f1

Browse files
committed
clean up auth code examples
1 parent 4755f18 commit 78382f1

11 files changed

+130
-213
lines changed

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

Lines changed: 74 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,33 @@ import { APP_ID } from "../realm.config.json";
55
const app = new Realm.App({ id: APP_ID });
66
jest.setTimeout(15000);
77
describe("Log in user", () => {
8+
const userPass = {
9+
10+
password: "passw0rd",
11+
};
812
beforeAll(async () => {
9-
try {
10-
await app.emailPasswordAuth.registerUser({
11-
12-
password: "passw0rd",
13-
});
14-
} catch (err) {
15-
console.log("account already exists for: ");
16-
console.log("email:", "[email protected]");
17-
console.log("password:", "passw0rd");
13+
await app.emailPasswordAuth.registerUser(userPass);
14+
});
15+
afterAll(async () => {
16+
if (app.currentUser) {
17+
await app.deleteUser(app.currentUser);
18+
await app.currentUser.logOut(app.currentUser);
1819
}
1920
});
20-
// afterEach(async () => {
21-
// await app?.currentUser?.logOut();
22-
// });
2321
test("Anonymous log in", async () => {
2422
// :snippet-start: anon-auth
2523
async function loginAnonymous() {
2624
// Create an anonymous credential
2725
const credentials = Realm.Credentials.anonymous();
28-
try {
29-
// Authenticate the user
30-
const user = await app.logIn(credentials);
31-
// `App.currentUser` updates to match the logged in user
32-
console.assert(user.id === app.currentUser.id);
33-
return user;
34-
} catch (err) {
35-
console.error("Failed to log in", err);
36-
}
26+
27+
// Authenticate the user
28+
const user = await app.logIn(credentials);
29+
// `App.currentUser` updates to match the logged in user
30+
console.assert(user.id === app.currentUser.id);
31+
32+
return user;
3733
}
3834
const user = await loginAnonymous();
39-
console.log("Successfully logged in!", user.id);
40-
4135
// :snippet-end:
4236
expect(app.currentUser.isLoggedIn).toBe(true);
4337
expect(app.currentUser?.id).toBe(user.id);
@@ -47,19 +41,16 @@ describe("Log in user", () => {
4741
async function loginEmailPassword(email, password) {
4842
// Create an email/password credential
4943
const credentials = Realm.Credentials.emailPassword(email, password);
50-
try {
51-
// Authenticate the user
52-
const user = await app.logIn(credentials);
53-
// `App.currentUser` updates to match the logged in user
54-
console.assert(user.id === app.currentUser.id);
55-
return user;
56-
} catch (err) {
57-
console.error("Failed to log in", err);
58-
}
44+
45+
// Authenticate the user
46+
const user = await app.logIn(credentials);
47+
// `App.currentUser` updates to match the logged in user
48+
console.assert(user.id === app.currentUser.id);
49+
50+
return user;
5951
}
6052

6153
const user = await loginEmailPassword("[email protected]", "passw0rd");
62-
console.log("Successfully logged in!", user);
6354
// :snippet-end:
6455
expect(app.currentUser.isLoggedIn).toBe(true);
6556
expect(app.currentUser?.id).toBe(user.id);
@@ -71,66 +62,56 @@ describe("Log in user", () => {
7162
const now = new Date();
7263
const nonce = now.getTime();
7364
const REALM_API_KEY = await baseUser.apiKeys.create("myKey" + nonce);
74-
console.log({ REALM_API_KEY });
7565
// :snippet-start: api-key-auth
7666
async function loginApiKey(apiKey) {
7767
// Create an API Key credential
7868
const credentials = Realm.Credentials.apiKey(apiKey);
79-
try {
80-
// Authenticate the user
81-
const user = await app.logIn(credentials);
82-
// `App.currentUser` updates to match the logged in user
83-
console.assert(user.id === app.currentUser.id);
84-
return user;
85-
} catch (err) {
86-
console.error("Failed to log in", err);
87-
}
69+
70+
// Authenticate the user
71+
const user = await app.logIn(credentials);
72+
73+
// `App.currentUser` updates to match the logged in user
74+
console.assert(user.id === app.currentUser.id);
75+
76+
return user;
8877
}
78+
8979
const user = await loginApiKey(REALM_API_KEY.key); // add previously generated API key
90-
console.log("Successfully logged in!", user);
9180
// :snippet-end:
9281
expect(app.currentUser.isLoggedIn).toBe(true);
9382
expect(app.currentUser?.id).toBe(user.id);
9483
});
95-
test.skip("Custom function", () => {
84+
test.skip("Custom function", async () => {
9685
// :snippet-start: custom-function-auth
9786
async function loginCustomFunction(payload) {
9887
// Create a Custom Function credential
9988
const credentials = Realm.Credentials.function(payload);
100-
try {
101-
// Authenticate the user
102-
const user = await app.logIn(credentials);
103-
// `App.currentUser` updates to match the logged in user
104-
console.assert(user.id === app.currentUser.id);
105-
return user;
106-
} catch (err) {
107-
console.error("Failed to log in", err);
108-
}
89+
90+
// Authenticate the user
91+
const user = await app.logIn(credentials);
92+
// `App.currentUser` updates to match the logged in user
93+
console.assert(user.id === app.currentUser.id);
94+
95+
return user;
10996
}
110-
loginCustomFunction({ username: "ilovemongodb" }).then((user) => {
111-
console.log("Successfully logged in!", user);
112-
});
97+
const user = await loginCustomFunction({ username: "ilovemongodb" });
11398
// :snippet-end:
11499
});
115100
// TODO
116-
test.skip("Custom JWT", () => {
101+
test.skip("Custom JWT", async () => {
117102
// :snippet-start: custom-jwt
118103
async function loginCustomJwt(jwt) {
119104
// Create a Custom JWT credential
120105
const credentials = Realm.Credentials.jwt(jwt);
121-
try {
122-
// Authenticate the user
123-
const user = await app.logIn(credentials);
124-
// `App.currentUser` updates to match the logged in user
125-
console.assert(user.id === app.currentUser.id);
126-
return user;
127-
} catch (err) {
128-
console.error("Failed to log in", err);
129-
}
106+
107+
// Authenticate the user
108+
const user = await app.logIn(credentials);
109+
// `App.currentUser` updates to match the logged in user
110+
console.assert(user.id === app.currentUser.id);
111+
112+
return user;
130113
}
131-
loginCustomJwt("eyJ0eXAi...Q3NJmnU8oP3YkZ8").then((user) => {
132-
console.log("Successfully logged in!", user);
133-
});
114+
const user = await loginCustomJwt("eyJ0eXAi...Q3NJmnU8oP3YkZ8");
134115
// :snippet-end:
135116
});
136117
test.skip("Firebase with Custom JWT", async () => {
@@ -169,16 +150,14 @@ describe("Log in user", () => {
169150
Realm.handleAuthRedirect();
170151
// :snippet-end:
171152
});
172-
test("Facebook SDK OAuth", () => {
153+
test("Facebook SDK OAuth", async () => {
173154
// :snippet-start: facebook-sdk-oauth
174155
// Get the access token from the Facebook SDK
175156
const { accessToken } = FB.getAuthResponse();
176157
// Define credentials with the access token from the Facebook SDK
177158
const credentials = Realm.Credentials.facebook(accessToken);
178159
// Log the user in to your app
179-
app.logIn(credentials).then((user) => {
180-
console.log(`Logged in with id: ${user.id}`);
181-
});
160+
await app.logIn(credentials);
182161
// :snippet-end:
183162
});
184163
});
@@ -221,73 +200,34 @@ describe("Log in user", () => {
221200
test("Apple SDK OAuth", () => {
222201
// :snippet-start: apple-sdk-oauth
223202
// Get the ID token from the Apple SDK
224-
AppleID.auth
225-
.signIn()
226-
.then(({ id_token }) => {
227-
// Define credentials with the ID token from the Apple SDK
228-
const credentials = Realm.Credentials.apple(id_token);
229-
// Log the user in to your app
230-
return app.logIn(credentials);
231-
})
232-
.then((user) => {
233-
console.log(`Logged in with id: ${user.id}`);
234-
});
203+
const user = AppleID.auth.signIn().then(({ id_token }) => {
204+
// Define credentials with the ID token from the Apple SDK
205+
const credentials = Realm.Credentials.apple(id_token);
206+
// Log the user in to your app
207+
return app.logIn(credentials);
208+
});
209+
å;
235210
// :snippet-end:
236211
});
237212
});
238213
});
239214
describe("Log out user", () => {
240-
beforeAll(async () => {
241-
const userIds = [...Object.keys(app.allUsers)];
242-
243-
for await (const userId of userIds) {
244-
await app.deleteUser(app.allUsers[userId]);
245-
}
246-
247-
try {
248-
await app.emailPasswordAuth.registerUser({
249-
250-
password: "passw0rd",
251-
});
252-
} catch (err) {
253-
console.log("accounts already exist");
254-
}
255-
try {
256-
await app.emailPasswordAuth.registerUser({
257-
258-
password: "passw0rd",
259-
});
260-
} catch (err) {
261-
console.log("accounts already exist");
262-
}
263-
try {
264-
await app.emailPasswordAuth.registerUser({
265-
266-
password: "passw0rd",
267-
});
268-
} catch (err) {
269-
console.log("accounts already exist");
270-
}
271-
try {
272-
await app.emailPasswordAuth.registerUser({
273-
274-
password: "passw0rd",
275-
});
276-
} catch (err) {
277-
console.log("accounts already exist");
278-
}
215+
beforeEach(async () => {
216+
await app.emailPasswordAuth.registerUser({
217+
218+
password: "passw0rd",
219+
});
279220
});
280-
afterAll(async () => {
281-
const userIds = [...Object.keys(app.allUsers)];
282-
283-
for await (const userId of userIds) {
284-
console.log(userIds);
285-
await app.deleteUser(app.allUsers[userId]);
286-
}
221+
afterEach(async () => {
222+
const currentUser = await app.logIn(
223+
Realm.Credentials.emailPassword("[email protected]", "passw0rd")
224+
);
225+
await app.deleteUser(app.currentUser);
226+
await currentUser?.logOut();
287227
});
288228
test("Log out current user", async () => {
289229
const credentials = Realm.Credentials.emailPassword(
290-
"ian.jasper@example.com",
230+
"hank.williams@example.com",
291231
"passw0rd"
292232
);
293233
const user = await app.logIn(credentials);
@@ -297,24 +237,14 @@ describe("Log out user", () => {
297237
// :snippet-end:
298238
expect(user.isLoggedIn).toBe(false);
299239
expect(app.currentUser).toBe(null);
300-
await app.logIn(credentials);
301240
});
302241
test("Log out specific user", async () => {
303-
const user1 = await app.logIn(
304-
Realm.Credentials.emailPassword("[email protected]", "passw0rd")
305-
);
306-
const user2 = await app.logIn(
307-
Realm.Credentials.emailPassword("[email protected]", "passw0rd")
308-
);
309-
const user3Creds = Realm.Credentials.emailPassword(
310-
311-
"passw0rd"
242+
await app.logIn(
243+
Realm.Credentials.emailPassword("[email protected]", "passw0rd")
312244
);
313-
const user3 = await app.logIn(user3Creds);
314245
// :snippet-start: log-out-specific-user
315246
const userId = app.currentUser.id;
316247
await app.allUsers[userId].logOut();
317248
// :snippet-end:
318-
await app.logIn(user3Creds);
319249
});
320250
});
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 user = AppleID.auth.signIn().then(({ id_token }) => {
3+
// Define credentials with the ID token from the Apple SDK
4+
const credentials = Realm.Credentials.apple(id_token);
5+
// Log the user in to your app
6+
return app.logIn(credentials);
7+
});
8+
å;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: add abstracted example

0 commit comments

Comments
 (0)