Skip to content

Commit 1fddc3b

Browse files
authored
Merged first part of revamp_sign_in_methods (#344)
* Merged first part of revamp_sign_in_methods * used new sign in methods in test app * added second part of revamp sign in methods * added new APIs to Auth externs * [AUTOMATED]: Prettier Code Styling * fixed indentation * updated auth externs for sign-in methods * changed format of type name is reference doc
1 parent b70a309 commit 1fddc3b

File tree

8 files changed

+1003
-27
lines changed

8 files changed

+1003
-27
lines changed

packages/auth/demo/public/script.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ function onSetPersistence() {
296296
function onSignUp() {
297297
var email = $('#signup-email').val();
298298
var password = $('#signup-password').val();
299-
auth.createUserWithEmailAndPassword(email, password)
300-
.then(onAuthSuccess, onAuthError);
299+
auth.createUserAndRetrieveDataWithEmailAndPassword(email, password)
300+
.then(onAuthUserCredentialSuccess, onAuthError);
301301
}
302302

303303

@@ -307,8 +307,8 @@ function onSignUp() {
307307
function onSignInWithEmailAndPassword() {
308308
var email = $('#signin-email').val();
309309
var password = $('#signin-password').val();
310-
auth.signInWithEmailAndPassword(email, password)
311-
.then(onAuthSuccess, onAuthError);
310+
auth.signInAndRetrieveDataWithEmailAndPassword(email, password)
311+
.then(onAuthUserCredentialSuccess, onAuthError);
312312
}
313313

314314

@@ -320,16 +320,17 @@ function onSignInWithCustomToken(event) {
320320
// The token can be directly specified on the html element.
321321
var token = $('#user-custom-token').val();
322322

323-
auth.signInWithCustomToken(token)
324-
.then(onAuthSuccess, onAuthError);
323+
auth.signInAndRetrieveDataWithCustomToken(token)
324+
.then(onAuthUserCredentialSuccess, onAuthError);
325325
}
326326

327327

328328
/**
329329
* Signs in anonymously.
330330
*/
331331
function onSignInAnonymously() {
332-
auth.signInAnonymously().then(onAuthSuccess, onAuthError);
332+
auth.signInAnonymouslyAndRetrieveData()
333+
.then(onAuthUserCredentialSuccess, onAuthError);
333334
}
334335

335336

packages/auth/src/additionaluserinfo.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ fireauth.AdditionalUserInfo.fromPlainObject = function(resp) {
9595
fireauth.AdditionalUserInfo.VerifyAssertionField = {
9696
ID_TOKEN: 'idToken',
9797
IS_NEW_USER: 'isNewUser',
98+
KIND: 'kind',
9899
PROVIDER_ID: 'providerId',
99100
RAW_USER_INFO: 'rawUserInfo',
100101
SCREEN_NAME: 'screenName'
@@ -129,9 +130,23 @@ fireauth.GenericAdditionalUserInfo = function(info) {
129130
// This is internal only.
130131
throw new Error('Invalid additional user info!');
131132
}
132-
// Check whether user is new. If not provided, default to false.
133-
var isNewUser =
134-
!!info[fireauth.AdditionalUserInfo.VerifyAssertionField.IS_NEW_USER];
133+
// For custom token and anonymous token, set provider ID to null.
134+
if (providerId == fireauth.idp.ProviderId.ANONYMOUS ||
135+
providerId == fireauth.idp.ProviderId.CUSTOM) {
136+
providerId = null;
137+
}
138+
// Check whether user is new. Temporary Solution since backend does not return
139+
// isNewUser field for SignupNewUserResponse.
140+
var isNewUser = false;
141+
if (typeof info[fireauth.AdditionalUserInfo.VerifyAssertionField.IS_NEW_USER]
142+
!== 'undefined') {
143+
isNewUser =
144+
!!info[fireauth.AdditionalUserInfo.VerifyAssertionField.IS_NEW_USER];
145+
} else if (info[fireauth.AdditionalUserInfo.VerifyAssertionField.KIND]
146+
=== 'identitytoolkit#SignupNewUserResponse') {
147+
//For SignupNewUserResponse, always set isNewUser to true.
148+
isNewUser = true;
149+
}
135150
// Set required providerId.
136151
fireauth.object.setReadonlyProperty(this, 'providerId', providerId);
137152
// Set read-only isNewUser property.

packages/auth/src/auth.js

Lines changed: 88 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,11 +1353,26 @@ fireauth.Auth.prototype.getIdTokenInternal = function(opt_forceRefresh) {
13531353

13541354

13551355
/**
1356-
* Sign in using a custom token (Bring Your Own Auth).
1356+
* Signs in a user asynchronously using a custom token.
13571357
* @param {string} token The custom token to sign in with.
13581358
* @return {!goog.Promise<!fireauth.AuthUser>}
13591359
*/
13601360
fireauth.Auth.prototype.signInWithCustomToken = function(token) {
1361+
// Get signInAndRetrieveDataWithCustomToken result and return the user only.
1362+
return this.signInAndRetrieveDataWithCustomToken(token)
1363+
.then(function(result) {
1364+
return result['user'];
1365+
});
1366+
};
1367+
1368+
1369+
/**
1370+
* Signs in a user asynchronously using a custom token and returns any
1371+
* additional user info data or credentials returned form the backend.
1372+
* @param {string} token The custom token to sign in with.
1373+
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
1374+
*/
1375+
fireauth.Auth.prototype.signInAndRetrieveDataWithCustomToken = function(token) {
13611376
var self = this;
13621377
// Wait for the redirect state to be determined before proceeding. If critical
13631378
// errors like web storage unsupported are detected, fail before RPC, instead
@@ -1371,20 +1386,20 @@ fireauth.Auth.prototype.signInWithCustomToken = function(token) {
13711386
// response will look like an anonymous user (no credentials visible).
13721387
user.updateProperty('isAnonymous', false);
13731388
// Save isAnonymous flag changes to current user in storage.
1374-
return self.handleUserStateChange_(user);
1375-
}).then(function() {
1376-
return self.currentUser_();
1389+
self.handleUserStateChange_(user);
1390+
return result;
13771391
});
13781392
};
13791393

13801394

13811395
/**
1382-
* Sign in using an email and password.
1396+
* Sign in using an email and password and returns any additional user info
1397+
* data or credentials returned form the backend.
13831398
* @param {string} email The email to sign in with.
13841399
* @param {string} password The password to sign in with.
1385-
* @return {!goog.Promise<!fireauth.AuthUser>}
1400+
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
13861401
*/
1387-
fireauth.Auth.prototype.signInWithEmailAndPassword =
1402+
fireauth.Auth.prototype.signInAndRetrieveDataWithEmailAndPassword =
13881403
function(email, password) {
13891404
var self = this;
13901405
// Wait for the redirect state to be determined before proceeding. If critical
@@ -1393,12 +1408,27 @@ fireauth.Auth.prototype.signInWithEmailAndPassword =
13931408
return this.redirectStateIsReady_.then(function() {
13941409
return self.signInWithIdTokenProvider_(
13951410
self.getRpcHandler().verifyPassword(email, password));
1396-
}).then(function(result) {
1397-
return result['user'];
13981411
});
13991412
};
14001413

14011414

1415+
/**
1416+
* Sign in using an email and password.
1417+
* @param {string} email The email to sign in with.
1418+
* @param {string} password The password to sign in with.
1419+
* @return {!goog.Promise<!fireauth.AuthUser>}
1420+
*/
1421+
fireauth.Auth.prototype.signInWithEmailAndPassword =
1422+
function(email, password) {
1423+
// Get signInAndRetrieveDataWithEmailAndPassword result and return
1424+
// the user only.
1425+
return this.signInAndRetrieveDataWithEmailAndPassword(email, password)
1426+
.then(function(result) {
1427+
return result['user'];
1428+
});
1429+
};
1430+
1431+
14021432
/**
14031433
* Create a new email and password account.
14041434
* @param {string} email The email to sign up with.
@@ -1407,15 +1437,31 @@ fireauth.Auth.prototype.signInWithEmailAndPassword =
14071437
*/
14081438
fireauth.Auth.prototype.createUserWithEmailAndPassword =
14091439
function(email, password) {
1440+
// Get createUserAndRetrieveDataWithEmailAndPassword result and return
1441+
// the user only.
1442+
return this.createUserAndRetrieveDataWithEmailAndPassword(email, password)
1443+
.then(function(result) {
1444+
return result['user'];
1445+
});
1446+
};
1447+
1448+
1449+
/**
1450+
* Creates a new email and password account and returns any additional user
1451+
* info data or credentials returned form the backend.
1452+
* @param {string} email The email to sign up with.
1453+
* @param {string} password The password to sign up with.
1454+
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
1455+
*/
1456+
fireauth.Auth.prototype.createUserAndRetrieveDataWithEmailAndPassword =
1457+
function(email, password) {
14101458
var self = this;
14111459
// Wait for the redirect state to be determined before proceeding. If critical
14121460
// errors like web storage unsupported are detected, fail before RPC, instead
14131461
// of after.
14141462
return this.redirectStateIsReady_.then(function() {
14151463
return self.signInWithIdTokenProvider_(
14161464
self.getRpcHandler().createAccount(email, password));
1417-
}).then(function(result) {
1418-
return result['user'];
14191465
});
14201466
};
14211467

@@ -1457,10 +1503,24 @@ fireauth.Auth.prototype.signInAndRetrieveDataWithCredential =
14571503

14581504

14591505
/**
1460-
* Sign in using anonymously a user.
1506+
* Signs in a user anonymously.
14611507
* @return {!goog.Promise<!fireauth.AuthUser>}
14621508
*/
14631509
fireauth.Auth.prototype.signInAnonymously = function() {
1510+
// Get signInAnonymouslyAndRetrieveData result and return the user only.
1511+
return this.signInAnonymouslyAndRetrieveData()
1512+
.then(function(result) {
1513+
return result['user'];
1514+
});
1515+
};
1516+
1517+
1518+
/**
1519+
* Signs in a user anonymously and returns any additional user info data or
1520+
* credentials returned form the backend.
1521+
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
1522+
*/
1523+
fireauth.Auth.prototype.signInAnonymouslyAndRetrieveData = function() {
14641524
var self = this;
14651525
// Wait for the redirect state to be determined before proceeding. If critical
14661526
// errors like web storage unsupported are detected, fail before RPC, instead
@@ -1469,7 +1529,20 @@ fireauth.Auth.prototype.signInAnonymously = function() {
14691529
var user = self.currentUser_();
14701530
// If an anonymous user is already signed in, no need to sign him again.
14711531
if (user && user['isAnonymous']) {
1472-
return user;
1532+
var additionalUserInfo = fireauth.object.makeReadonlyCopy({
1533+
'providerId': null,
1534+
'isNewUser': false
1535+
});
1536+
return fireauth.object.makeReadonlyCopy({
1537+
// Return the signed in user reference.
1538+
'user': user,
1539+
// Do not return credential for anonymous user.
1540+
'credential': null,
1541+
// Return any additional IdP data.
1542+
'additionalUserInfo': additionalUserInfo,
1543+
// Sign in operation type.
1544+
'operationType': fireauth.constants.OperationType.SIGN_IN
1545+
});
14731546
} else {
14741547
// No anonymous user currently signed in.
14751548
return self.signInWithIdTokenProvider_(
@@ -1484,9 +1557,8 @@ fireauth.Auth.prototype.signInAnonymously = function() {
14841557
// overwrites.
14851558
user.updateProperty('isAnonymous', true);
14861559
// Save isAnonymous flag changes to current user in storage.
1487-
return self.handleUserStateChange_(user);
1488-
}).then(function() {
1489-
return self.currentUser_();
1560+
self.handleUserStateChange_(user);
1561+
return result;
14901562
});
14911563
}
14921564
});

packages/auth/src/exports_auth.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ fireauth.exportlib.exportPrototypeMethods(
5858
name: 'createUserWithEmailAndPassword',
5959
args: [fireauth.args.string('email'), fireauth.args.string('password')]
6060
},
61+
createUserAndRetrieveDataWithEmailAndPassword: {
62+
name: 'createUserAndRetrieveDataWithEmailAndPassword',
63+
args: [fireauth.args.string('email'), fireauth.args.string('password')]
64+
},
6165
fetchProvidersForEmail: {
6266
name: 'fetchProvidersForEmail',
6367
args: [fireauth.args.string('email')]
@@ -111,6 +115,10 @@ fireauth.exportlib.exportPrototypeMethods(
111115
name: 'signInAnonymously',
112116
args: []
113117
},
118+
signInAnonymouslyAndRetrieveData: {
119+
name: 'signInAnonymouslyAndRetrieveData',
120+
args: []
121+
},
114122
signInWithCredential: {
115123
name: 'signInWithCredential',
116124
args: [fireauth.args.authCredential()]
@@ -119,10 +127,18 @@ fireauth.exportlib.exportPrototypeMethods(
119127
name: 'signInWithCustomToken',
120128
args: [fireauth.args.string('token')]
121129
},
130+
signInAndRetrieveDataWithCustomToken: {
131+
name: 'signInAndRetrieveDataWithCustomToken',
132+
args: [fireauth.args.string('token')]
133+
},
122134
signInWithEmailAndPassword: {
123135
name: 'signInWithEmailAndPassword',
124136
args: [fireauth.args.string('email'), fireauth.args.string('password')]
125137
},
138+
signInAndRetrieveDataWithEmailAndPassword: {
139+
name: 'signInAndRetrieveDataWithEmailAndPassword',
140+
args: [fireauth.args.string('email'), fireauth.args.string('password')]
141+
},
126142
signInWithPhoneNumber: {
127143
name: 'signInWithPhoneNumber',
128144
args: [

packages/auth/src/idp.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ goog.provide('fireauth.idp.Settings');
2525

2626

2727
/**
28-
* Enums for supported provider IDs.
28+
* Enums for supported provider IDs. These provider IDs correspond to the
29+
* sign_in_provider in the Firebase ID token and do not correspond to the
30+
* supported client exposed firebase.auth.AuthProviders.
2931
* @enum {string}
3032
*/
3133
fireauth.idp.ProviderId = {
3234
ANONYMOUS: 'anonymous',
35+
CUSTOM: 'custom',
3336
FACEBOOK: 'facebook.com',
3437
FIREBASE: 'firebase',
3538
GITHUB: 'github.com',

0 commit comments

Comments
 (0)