@@ -5,39 +5,33 @@ import { APP_ID } from "../realm.config.json";
5
5
const app = new Realm . App ( { id : APP_ID } ) ;
6
6
jest . setTimeout ( 15000 ) ;
7
7
describe ( "Log in user" , ( ) => {
8
+ const userPass = {
9
+
10
+ password : "passw0rd" ,
11
+ } ;
8
12
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 ) ;
18
19
}
19
20
} ) ;
20
- // afterEach(async () => {
21
- // await app?.currentUser?.logOut();
22
- // });
23
21
test ( "Anonymous log in" , async ( ) => {
24
22
// :snippet-start: anon-auth
25
23
async function loginAnonymous ( ) {
26
24
// Create an anonymous credential
27
25
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 ;
37
33
}
38
34
const user = await loginAnonymous ( ) ;
39
- console . log ( "Successfully logged in!" , user . id ) ;
40
-
41
35
// :snippet-end:
42
36
expect ( app . currentUser . isLoggedIn ) . toBe ( true ) ;
43
37
expect ( app . currentUser ?. id ) . toBe ( user . id ) ;
@@ -47,19 +41,16 @@ describe("Log in user", () => {
47
41
async function loginEmailPassword ( email , password ) {
48
42
// Create an email/password credential
49
43
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 ;
59
51
}
60
52
61
53
const user = await loginEmailPassword ( "[email protected] " , "passw0rd" ) ;
62
- console . log ( "Successfully logged in!" , user ) ;
63
54
// :snippet-end:
64
55
expect ( app . currentUser . isLoggedIn ) . toBe ( true ) ;
65
56
expect ( app . currentUser ?. id ) . toBe ( user . id ) ;
@@ -71,66 +62,56 @@ describe("Log in user", () => {
71
62
const now = new Date ( ) ;
72
63
const nonce = now . getTime ( ) ;
73
64
const REALM_API_KEY = await baseUser . apiKeys . create ( "myKey" + nonce ) ;
74
- console . log ( { REALM_API_KEY } ) ;
75
65
// :snippet-start: api-key-auth
76
66
async function loginApiKey ( apiKey ) {
77
67
// Create an API Key credential
78
68
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 ;
88
77
}
78
+
89
79
const user = await loginApiKey ( REALM_API_KEY . key ) ; // add previously generated API key
90
- console . log ( "Successfully logged in!" , user ) ;
91
80
// :snippet-end:
92
81
expect ( app . currentUser . isLoggedIn ) . toBe ( true ) ;
93
82
expect ( app . currentUser ?. id ) . toBe ( user . id ) ;
94
83
} ) ;
95
- test . skip ( "Custom function" , ( ) => {
84
+ test . skip ( "Custom function" , async ( ) => {
96
85
// :snippet-start: custom-function-auth
97
86
async function loginCustomFunction ( payload ) {
98
87
// Create a Custom Function credential
99
88
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 ;
109
96
}
110
- loginCustomFunction ( { username : "ilovemongodb" } ) . then ( ( user ) => {
111
- console . log ( "Successfully logged in!" , user ) ;
112
- } ) ;
97
+ const user = await loginCustomFunction ( { username : "ilovemongodb" } ) ;
113
98
// :snippet-end:
114
99
} ) ;
115
100
// TODO
116
- test . skip ( "Custom JWT" , ( ) => {
101
+ test . skip ( "Custom JWT" , async ( ) => {
117
102
// :snippet-start: custom-jwt
118
103
async function loginCustomJwt ( jwt ) {
119
104
// Create a Custom JWT credential
120
105
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 ;
130
113
}
131
- loginCustomJwt ( "eyJ0eXAi...Q3NJmnU8oP3YkZ8" ) . then ( ( user ) => {
132
- console . log ( "Successfully logged in!" , user ) ;
133
- } ) ;
114
+ const user = await loginCustomJwt ( "eyJ0eXAi...Q3NJmnU8oP3YkZ8" ) ;
134
115
// :snippet-end:
135
116
} ) ;
136
117
test . skip ( "Firebase with Custom JWT" , async ( ) => {
@@ -169,16 +150,14 @@ describe("Log in user", () => {
169
150
Realm . handleAuthRedirect ( ) ;
170
151
// :snippet-end:
171
152
} ) ;
172
- test ( "Facebook SDK OAuth" , ( ) => {
153
+ test ( "Facebook SDK OAuth" , async ( ) => {
173
154
// :snippet-start: facebook-sdk-oauth
174
155
// Get the access token from the Facebook SDK
175
156
const { accessToken } = FB . getAuthResponse ( ) ;
176
157
// Define credentials with the access token from the Facebook SDK
177
158
const credentials = Realm . Credentials . facebook ( accessToken ) ;
178
159
// 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 ) ;
182
161
// :snippet-end:
183
162
} ) ;
184
163
} ) ;
@@ -221,73 +200,34 @@ describe("Log in user", () => {
221
200
test ( "Apple SDK OAuth" , ( ) => {
222
201
// :snippet-start: apple-sdk-oauth
223
202
// 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
+ å ;
235
210
// :snippet-end:
236
211
} ) ;
237
212
} ) ;
238
213
} ) ;
239
214
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
+ } ) ;
279
220
} ) ;
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 ( ) ;
287
227
} ) ;
288
228
test ( "Log out current user" , async ( ) => {
289
229
const credentials = Realm . Credentials . emailPassword (
290
- "ian.jasper @example.com" ,
230
+ "hank.williams @example.com" ,
291
231
"passw0rd"
292
232
) ;
293
233
const user = await app . logIn ( credentials ) ;
@@ -297,24 +237,14 @@ describe("Log out user", () => {
297
237
// :snippet-end:
298
238
expect ( user . isLoggedIn ) . toBe ( false ) ;
299
239
expect ( app . currentUser ) . toBe ( null ) ;
300
- await app . logIn ( credentials ) ;
301
240
} ) ;
302
241
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" )
312
244
) ;
313
- const user3 = await app . logIn ( user3Creds ) ;
314
245
// :snippet-start: log-out-specific-user
315
246
const userId = app . currentUser . id ;
316
247
await app . allUsers [ userId ] . logOut ( ) ;
317
248
// :snippet-end:
318
- await app . logIn ( user3Creds ) ;
319
249
} ) ;
320
250
} ) ;
0 commit comments