Skip to content

Commit f813f12

Browse files
committed
Update API docs for changeAuthState
1 parent 77cf75c commit f813f12

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

API.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,45 @@ ref.flush(); // added foo after null
169169

170170
Authentication methods for simulating changes to the auth state of a Firebase reference.
171171

172-
##### `changeAuthState(authData)` -> `undefined`
172+
##### `changeAuthState(user)` -> `undefined`
173173

174-
Changes the active authentication credentials to the `authData` object. Before changing the authentication state, `changeAuthState` checks whether the `authData` object is deeply equal to the current authentication data. `onAuth` listeners will only be triggered if the data is not deeply equal. To simulate no user being authenticated, pass `null` for `authData`. This operation is queued until the next `flush`.
174+
Changes the active authentication credentials to the `authData` object.
175+
Before changing the authentication state, `changeAuthState` checks the
176+
`user` object against the current authentication data.
177+
`onAuthStateChanged` listeners will only be triggered if the data is not
178+
deeply equal.
175179

176-
`authData` should adhere to the [documented schema](https://www.firebase.com/docs/web/api/firebase/onauth.html).
180+
To simulate no user being authenticated, pass `null` for `user`.
181+
This operation is queued until the next `flush`.
177182

178183
Example:
179184

180185
```js
181-
ref.changeAuthState({
186+
ref.changeAuthState(new User(ref, {
182187
uid: 'theUid',
183-
provider: 'github',
184-
token: 'theToken',
185-
expires: Math.floor(new Date() / 1000) + 24 * 60 * 60, // expire in 24 hours
186-
auth: {
187-
myAuthProperty: true
188-
}
189-
});
188+
189+
emailVerified: true,
190+
displayName: 'Mr. Meeseeks',
191+
phoneNumber: '+1-508-123-4567',
192+
photoURL: 'https://example.com/image.png',
193+
isAnonymous: false,
194+
providerId: 'github',
195+
providerData: [],
196+
refreshToken: '123e4567-e89b-12d3-a456-426655440000',
197+
metadata: {}, // firebase-mock offers limited support for this field
198+
customClaims: {
199+
isAdmin: true,
200+
// etc.
201+
},
202+
_idtoken: 'theToken',
203+
_tokenValidity: {
204+
authTime: '2019-11-22T08:46:15Z',
205+
issuedAtTime: '2019-11-22T08:46:15Z',
206+
expirationTime: '2019-11-22T09:46:15Z',
207+
},
208+
}));
190209
ref.flush();
191-
console.assert(ref.getAuth().auth.myAuthProperty, 'authData has custom property');
210+
console.assert(ref.getAuth().displayName === 'Mr. Meeseeks', 'Auth name is correct');
192211
```
193212

194213
<hr>

test/unit/docs.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const User = require('../../src/user');
2+
3+
describe('API.md', () => {
4+
describe('Auth example for', () => {
5+
6+
let ref;
7+
beforeEach(() => {
8+
const Authentication = require('../../').MockAuthentication;
9+
ref = new Authentication();
10+
});
11+
12+
it('changeAuthState works as described', () => {
13+
ref.changeAuthState(new User(ref, {
14+
uid: 'theUid',
15+
16+
emailVerified: true,
17+
displayName: 'Mr. Meeseeks',
18+
phoneNumber: '+1-508-123-4567',
19+
photoURL: 'https://example.com/image.png',
20+
isAnonymous: false,
21+
providerId: 'github',
22+
providerData: [],
23+
refreshToken: '123e4567-e89b-12d3-a456-426655440000',
24+
metadata: {}, // firebase-mock offers limited support for this field
25+
customClaims: {
26+
isAdmin: true,
27+
// etc.
28+
},
29+
_idtoken: 'theToken',
30+
_tokenValidity: {
31+
authTime: '2019-11-22T08:46:15Z',
32+
issuedAtTime: '2019-11-22T08:46:15Z',
33+
expirationTime: '2019-11-22T09:46:15Z',
34+
},
35+
}));
36+
ref.flush();
37+
console.assert(ref.getAuth().displayName === 'Mr. Meeseeks', 'Auth name is correct');
38+
});
39+
});
40+
});

0 commit comments

Comments
 (0)