Skip to content

Commit ab3a872

Browse files
add ability to retreive the current logged in user as a promise
1 parent 50abe6c commit ab3a872

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

packages/auth/src/auth.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,21 @@ fireauth.Auth.prototype.onAuthStateChanged = function(
15961596
opt_completed);
15971597
};
15981598

1599+
/**
1600+
* @return {Promise} The fireauth.AuthUser if a user successfully logged in.
1601+
* Null if no user is logged in.
1602+
*/
1603+
fireauth.Auth.prototype.getSignedInUser = function() {
1604+
return new Promise((resolve, reject) => {
1605+
if (this.currentUser_()) {
1606+
resolve(this.currentUser_());
1607+
}
1608+
const unsubscribe = this.onAuthStateChanged(user => {
1609+
unsubscribe();
1610+
resolve(user);
1611+
}, reject);
1612+
});
1613+
}
15991614

16001615
/**
16011616
* Returns an STS token. If the cached one is unexpired it is directly returned.

packages/auth/test/auth_test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,97 @@ function testAuth_onAuthStateChanged() {
16571657
unsubscribe1 = auth1.onAuthStateChanged(observer1);
16581658
}
16591659

1660+
/**
1661+
* Test whether we can retrieve the signed-in user as a promise.
1662+
* We also verify the behavior when user signs out.
1663+
*/
1664+
function testAuth_getSignedInUser() {
1665+
stubs.reset();
1666+
var expectedTokenResponse2 = {
1667+
'idToken': jwt2,
1668+
'refreshToken': 'REFRESH_TOKEN2'
1669+
};
1670+
var user = new fireauth.AuthUser(
1671+
config3, expectedTokenResponse, {'uid': '1234'});
1672+
var user2 = new fireauth.AuthUser(
1673+
config3, expectedTokenResponse2, {'uid': '5678'});
1674+
// Simulate available token.
1675+
var counter = 0;
1676+
stubs.replace(
1677+
fireauth.StsTokenManager.prototype,
1678+
'getToken',
1679+
function(opt_forceRefresh) {
1680+
// Generate new token on each call.
1681+
counter++;
1682+
return goog.Promise.resolve({
1683+
accessToken: 'accessToken' + counter.toString(),
1684+
refreshToken: 'refreshToken'
1685+
});
1686+
});
1687+
// Simulate user initially logged in.
1688+
stubs.replace(
1689+
fireauth.storage.UserManager.prototype,
1690+
'getCurrentUser',
1691+
function() {
1692+
return goog.Promise.resolve(user);
1693+
});
1694+
initializeMockStorage();
1695+
// Suppress addStateChangeListener.
1696+
stubs.replace(
1697+
fireauth.storage.UserManager.prototype,
1698+
'addCurrentUserChangeListener',
1699+
function(listener) {});
1700+
// Simulate available token.
1701+
stubs.replace(
1702+
fireauth.AuthUser.prototype,
1703+
'reload',
1704+
function() {
1705+
// Token not refreshed, notifyAuthListeners_ should call regardless.
1706+
return goog.Promise.resolve();
1707+
});
1708+
// Simulate new user sign in.
1709+
stubs.replace(
1710+
fireauth.AuthUser,
1711+
'initializeFromIdTokenResponse',
1712+
function() {
1713+
return goog.Promise.resolve(user2);
1714+
});
1715+
app1 = firebase.initializeApp(config1, appId1);
1716+
auth1 = app1.auth();
1717+
1718+
// logged-in user should be returned
1719+
auth1.getSignedInUser().then((signedInUser) => {
1720+
assertEquals(0, marker++);
1721+
assertNotNull(signedInUser);
1722+
assertObjectEquals(signedInUser, user);
1723+
// signing out so that the next call returns null.
1724+
return auth1.signOut();
1725+
}).then(() => {
1726+
assertEquals(1, marker++);
1727+
return auth1.getSignedInUser();
1728+
}).then((signedInUser) => {
1729+
// user had been signed out so that we receive null
1730+
assertEquals(2, marker++);
1731+
assertNull(signedInUser)
1732+
// sign in user2 so that the next call will return user2.
1733+
return auth1.signInWithIdTokenResponse(expectedTokenResponse2);
1734+
}).then(() => {
1735+
assertEquals(3, marker++);
1736+
return auth1.getSignedInUser();
1737+
}).then((signedInUser) => {
1738+
// user2 should have been returned
1739+
assertEquals(4, marker++);
1740+
assertNotNull(signedInUser)
1741+
assertNotNull(user2)
1742+
assertObjectEquals(signedInUser, user2);
1743+
asyncTestCase.signal();
1744+
})
1745+
mockControl.$replayAll();
1746+
// Keep track of what is triggering the events.
1747+
var marker = 0;
1748+
asyncTestCase.waitForSignals(1);
1749+
}
1750+
16601751

16611752
function testFetchSignInMethodsForEmail() {
16621753
var email = '[email protected]';

0 commit comments

Comments
 (0)