Skip to content

Commit 813a774

Browse files
author
Ti Wang
committed
Merged first part of revamp_sign_in_methods
1 parent e979cc7 commit 813a774

File tree

5 files changed

+332
-10
lines changed

5 files changed

+332
-10
lines changed

packages/auth/src/additionaluserinfo.js

Lines changed: 13 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,18 @@ 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+
// Check whether user is new. Temporary Solution since backend does not return
134+
// isNewUser field for SignupNewUserResponse.
135+
var isNewUser = false;
136+
if (typeof info[fireauth.AdditionalUserInfo.VerifyAssertionField.IS_NEW_USER]
137+
!== 'undefined') {
138+
isNewUser =
139+
!!info[fireauth.AdditionalUserInfo.VerifyAssertionField.IS_NEW_USER];
140+
} else if (info[fireauth.AdditionalUserInfo.VerifyAssertionField.KIND]
141+
=== 'identitytoolkit#SignupNewUserResponse') {
142+
//For SignupNewUserResponse, always set isNewUser to true.
143+
isNewUser = true;
144+
}
135145
// Set required providerId.
136146
fireauth.object.setReadonlyProperty(this, 'providerId', providerId);
137147
// Set read-only isNewUser property.

packages/auth/src/auth.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,12 +1379,13 @@ fireauth.Auth.prototype.signInWithCustomToken = function(token) {
13791379

13801380

13811381
/**
1382-
* Sign in using an email and password.
1382+
* Sign in using an email and password and returns any additional user info
1383+
* data or credentials returned form the backend.
13831384
* @param {string} email The email to sign in with.
13841385
* @param {string} password The password to sign in with.
1385-
* @return {!goog.Promise<!fireauth.AuthUser>}
1386+
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
13861387
*/
1387-
fireauth.Auth.prototype.signInWithEmailAndPassword =
1388+
fireauth.Auth.prototype.signInAndRetrieveDataWithEmailAndPassword =
13881389
function(email, password) {
13891390
var self = this;
13901391
// Wait for the redirect state to be determined before proceeding. If critical
@@ -1393,12 +1394,27 @@ fireauth.Auth.prototype.signInWithEmailAndPassword =
13931394
return this.redirectStateIsReady_.then(function() {
13941395
return self.signInWithIdTokenProvider_(
13951396
self.getRpcHandler().verifyPassword(email, password));
1396-
}).then(function(result) {
1397-
return result['user'];
13981397
});
13991398
};
14001399

14011400

1401+
/**
1402+
* Sign in using an email and password.
1403+
* @param {string} email The email to sign in with.
1404+
* @param {string} password The password to sign in with.
1405+
* @return {!goog.Promise<!fireauth.AuthUser>}
1406+
*/
1407+
fireauth.Auth.prototype.signInWithEmailAndPassword =
1408+
function(email, password) {
1409+
// Get signInAndRetrieveDataWithEmailAndPassword result and return
1410+
// the user only.
1411+
return this.signInAndRetrieveDataWithEmailAndPassword(email, password)
1412+
.then(function(result) {
1413+
return result['user'];
1414+
});
1415+
};
1416+
1417+
14021418
/**
14031419
* Create a new email and password account.
14041420
* @param {string} email The email to sign up with.
@@ -1407,15 +1423,31 @@ fireauth.Auth.prototype.signInWithEmailAndPassword =
14071423
*/
14081424
fireauth.Auth.prototype.createUserWithEmailAndPassword =
14091425
function(email, password) {
1426+
// Get createUserAndRetrieveDataWithEmailAndPassword result and return
1427+
// the user only.
1428+
return this.createUserAndRetrieveDataWithEmailAndPassword(email, password)
1429+
.then(function(result) {
1430+
return result['user'];
1431+
});
1432+
};
1433+
1434+
1435+
/**
1436+
* Creates a new email and password account and returns any additional user
1437+
* info data or credentials returned form the backend.
1438+
* @param {string} email The email to sign up with.
1439+
* @param {string} password The password to sign up with.
1440+
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
1441+
*/
1442+
fireauth.Auth.prototype.createUserAndRetrieveDataWithEmailAndPassword =
1443+
function(email, password) {
14101444
var self = this;
14111445
// Wait for the redirect state to be determined before proceeding. If critical
14121446
// errors like web storage unsupported are detected, fail before RPC, instead
14131447
// of after.
14141448
return this.redirectStateIsReady_.then(function() {
14151449
return self.signInWithIdTokenProvider_(
14161450
self.getRpcHandler().createAccount(email, password));
1417-
}).then(function(result) {
1418-
return result['user'];
14191451
});
14201452
};
14211453

packages/auth/src/exports_auth.js

Lines changed: 8 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')]
@@ -123,6 +127,10 @@ fireauth.exportlib.exportPrototypeMethods(
123127
name: 'signInWithEmailAndPassword',
124128
args: [fireauth.args.string('email'), fireauth.args.string('password')]
125129
},
130+
signInAndRetrieveDataWithEmailAndPassword: {
131+
name: 'signInAndRetrieveDataWithEmailAndPassword',
132+
args: [fireauth.args.string('email'), fireauth.args.string('password')]
133+
},
126134
signInWithPhoneNumber: {
127135
name: 'signInWithPhoneNumber',
128136
args: [

packages/auth/test/additionaluserinfo_test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,48 @@ var expectedGenericAdditionalUserInfo = {
7373
'providerId': 'phone'
7474
};
7575

76+
// "iss": "https://securetoken.google.com/12345678",
77+
// "picture": "https://plus.google.com/abcdefghijklmnopqrstu",
78+
// "aud": "12345678",
79+
// "auth_time": 1510357622,
80+
// "user_id": "abcdefghijklmnopqrstu",
81+
// "sub": "abcdefghijklmnopqrstu",
82+
// "iat": 1510357622,
83+
// "exp": 1510361222,
84+
// "email": "[email protected]",
85+
// "email_verified": true,
86+
// "firebase": {"identities": {
87+
// "email": ["[email protected]"]
88+
// }, "sign_in_provider": "password"}
89+
var tokenEmail = 'HEAD.ew0KICAiaXNzIjogImh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlL' +
90+
'mNvbS8xMjM0NTY3OCIsDQogICJwaWN0dXJlIjogImh0dHBzOi8vcGx1cy5' +
91+
'nb29nbGUuY29tL2FiY2RlZmdoaWprbG1ub3BxcnN0dSIsDQogICJhdWQiO' +
92+
'iAiMTIzNDU2NzgiLA0KICAiYXV0aF90aW1lIjogMTUxMDM1NzYyMiwNCiA' +
93+
'gInVzZXJfaWQiOiAiYWJjZGVmZ2hpamtsbW5vcHFyc3R1IiwNCiAgInN1Y' +
94+
'iI6ICJhYmNkZWZnaGlqa2xtbm9wcXJzdHUiLA0KICAiaWF0IjogMTUxMDM' +
95+
'1NzYyMiwNCiAgImV4cCI6IDE1MTAzNjEyMjIsDQogICJlbWFpbCI6ICJ1c' +
96+
'2VyQGV4YW1wbGUuY29tIiwNCiAgImVtYWlsX3ZlcmlmaWVkIjogdHJ1ZSw' +
97+
'NCiAgImZpcmViYXNlIjogew0KICAgICJpZGVudGl0aWVzIjogew0KICAgI' +
98+
'CAgImVtYWlsIjogWw0KICAgICAgICAidXNlckBleGFtcGxlLmNvbSINCiA' +
99+
'gICAgIF0NCiAgICB9LA0KICAgICJzaWduX2luX3Byb3ZpZGVyIjogInBhc' +
100+
'3N3b3JkIg0KICB9DQp9.SIGNATURE';
101+
102+
// SignupNewUserResponse response without isNewUser field.
103+
var signUpNewUserResponse = {
104+
'kind': 'identitytoolkit#SignupNewUserResponse',
105+
'idToken': tokenEmail,
106+
'refreshToken': 'REFRESH_TOKEN',
107+
'expiresIn': '3600',
108+
'localId': '123456'
109+
};
110+
111+
// Expected generic additional user info object for the above signUpNewUser
112+
// response.
113+
var expectedGenericAdditionalUserInfoForSignUpNewUser = {
114+
'isNewUser': true,
115+
'providerId': 'password'
116+
};
117+
76118
// Typical minimal verifyAssertion response for generic IdP user with no profile
77119
// data.
78120
var noProfileVerifyAssertion = {
@@ -371,6 +413,19 @@ function testGenericAdditionalUserInfo() {
371413
}
372414

373415

416+
417+
function testGenericAdditionalUserInfo_fromSignUpNewUserResponse() {
418+
var genericAdditionalUserInfo = new fireauth.GenericAdditionalUserInfo(
419+
signUpNewUserResponse);
420+
assertObjectEquals(
421+
expectedGenericAdditionalUserInfoForSignUpNewUser,
422+
genericAdditionalUserInfo);
423+
assertObjectEquals(
424+
genericAdditionalUserInfo,
425+
fireauth.AdditionalUserInfo.fromPlainObject(signUpNewUserResponse));
426+
}
427+
428+
374429
function testFederatedAdditionalUserInfo_withProfile() {
375430
var federatedAdditionalUserInfo =
376431
new fireauth.FederatedAdditionalUserInfo(facebookVerifyAssertion);

0 commit comments

Comments
 (0)