Skip to content
Merged
8 changes: 4 additions & 4 deletions packages/auth/demo/public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ function onSetPersistence() {
function onSignUp() {
var email = $('#signup-email').val();
var password = $('#signup-password').val();
auth.createUserWithEmailAndPassword(email, password)
.then(onAuthSuccess, onAuthError);
auth.createUserAndRetrieveDataWithEmailAndPassword(email, password)
.then(onAuthUserCredentialSuccess, onAuthError);
}


Expand All @@ -307,8 +307,8 @@ function onSignUp() {
function onSignInWithEmailAndPassword() {
var email = $('#signin-email').val();
var password = $('#signin-password').val();
auth.signInWithEmailAndPassword(email, password)
.then(onAuthSuccess, onAuthError);
auth.signInAndRetrieveDataWithEmailAndPassword(email, password)
.then(onAuthUserCredentialSuccess, onAuthError);
}


Expand Down
16 changes: 13 additions & 3 deletions packages/auth/src/additionaluserinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fireauth.AdditionalUserInfo.fromPlainObject = function(resp) {
fireauth.AdditionalUserInfo.VerifyAssertionField = {
ID_TOKEN: 'idToken',
IS_NEW_USER: 'isNewUser',
KIND: 'kind',
PROVIDER_ID: 'providerId',
RAW_USER_INFO: 'rawUserInfo',
SCREEN_NAME: 'screenName'
Expand Down Expand Up @@ -129,9 +130,18 @@ fireauth.GenericAdditionalUserInfo = function(info) {
// This is internal only.
throw new Error('Invalid additional user info!');
}
// Check whether user is new. If not provided, default to false.
var isNewUser =
!!info[fireauth.AdditionalUserInfo.VerifyAssertionField.IS_NEW_USER];
// Check whether user is new. Temporary Solution since backend does not return
// isNewUser field for SignupNewUserResponse.
var isNewUser = false;
if (typeof info[fireauth.AdditionalUserInfo.VerifyAssertionField.IS_NEW_USER]
!== 'undefined') {
isNewUser =
!!info[fireauth.AdditionalUserInfo.VerifyAssertionField.IS_NEW_USER];
} else if (info[fireauth.AdditionalUserInfo.VerifyAssertionField.KIND]
=== 'identitytoolkit#SignupNewUserResponse') {
//For SignupNewUserResponse, always set isNewUser to true.
isNewUser = true;
}
// Set required providerId.
fireauth.object.setReadonlyProperty(this, 'providerId', providerId);
// Set read-only isNewUser property.
Expand Down
46 changes: 39 additions & 7 deletions packages/auth/src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -1379,12 +1379,13 @@ fireauth.Auth.prototype.signInWithCustomToken = function(token) {


/**
* Sign in using an email and password.
* Sign in using an email and password and returns any additional user info
* data or credentials returned form the backend.
* @param {string} email The email to sign in with.
* @param {string} password The password to sign in with.
* @return {!goog.Promise<!fireauth.AuthUser>}
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
*/
fireauth.Auth.prototype.signInWithEmailAndPassword =
fireauth.Auth.prototype.signInAndRetrieveDataWithEmailAndPassword =
function(email, password) {
var self = this;
// Wait for the redirect state to be determined before proceeding. If critical
Expand All @@ -1393,12 +1394,27 @@ fireauth.Auth.prototype.signInWithEmailAndPassword =
return this.redirectStateIsReady_.then(function() {
return self.signInWithIdTokenProvider_(
self.getRpcHandler().verifyPassword(email, password));
}).then(function(result) {
return result['user'];
});
};


/**
* Sign in using an email and password.
* @param {string} email The email to sign in with.
* @param {string} password The password to sign in with.
* @return {!goog.Promise<!fireauth.AuthUser>}
*/
fireauth.Auth.prototype.signInWithEmailAndPassword =
function(email, password) {
// Get signInAndRetrieveDataWithEmailAndPassword result and return
// the user only.
return this.signInAndRetrieveDataWithEmailAndPassword(email, password)
.then(function(result) {
return result['user'];
});
};


/**
* Create a new email and password account.
* @param {string} email The email to sign up with.
Expand All @@ -1407,15 +1423,31 @@ fireauth.Auth.prototype.signInWithEmailAndPassword =
*/
fireauth.Auth.prototype.createUserWithEmailAndPassword =
function(email, password) {
// Get createUserAndRetrieveDataWithEmailAndPassword result and return
// the user only.
return this.createUserAndRetrieveDataWithEmailAndPassword(email, password)
.then(function(result) {
return result['user'];
});
};


/**
* Creates a new email and password account and returns any additional user
* info data or credentials returned form the backend.
* @param {string} email The email to sign up with.
* @param {string} password The password to sign up with.
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
*/
fireauth.Auth.prototype.createUserAndRetrieveDataWithEmailAndPassword =
function(email, password) {
var self = this;
// Wait for the redirect state to be determined before proceeding. If critical
// errors like web storage unsupported are detected, fail before RPC, instead
// of after.
return this.redirectStateIsReady_.then(function() {
return self.signInWithIdTokenProvider_(
self.getRpcHandler().createAccount(email, password));
}).then(function(result) {
return result['user'];
});
};

Expand Down
8 changes: 8 additions & 0 deletions packages/auth/src/exports_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ fireauth.exportlib.exportPrototypeMethods(
name: 'createUserWithEmailAndPassword',
args: [fireauth.args.string('email'), fireauth.args.string('password')]
},
createUserAndRetrieveDataWithEmailAndPassword: {
name: 'createUserAndRetrieveDataWithEmailAndPassword',
args: [fireauth.args.string('email'), fireauth.args.string('password')]
},
fetchProvidersForEmail: {
name: 'fetchProvidersForEmail',
args: [fireauth.args.string('email')]
Expand Down Expand Up @@ -123,6 +127,10 @@ fireauth.exportlib.exportPrototypeMethods(
name: 'signInWithEmailAndPassword',
args: [fireauth.args.string('email'), fireauth.args.string('password')]
},
signInAndRetrieveDataWithEmailAndPassword: {
name: 'signInAndRetrieveDataWithEmailAndPassword',
args: [fireauth.args.string('email'), fireauth.args.string('password')]
},
signInWithPhoneNumber: {
name: 'signInWithPhoneNumber',
args: [
Expand Down
55 changes: 55 additions & 0 deletions packages/auth/test/additionaluserinfo_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,48 @@ var expectedGenericAdditionalUserInfo = {
'providerId': 'phone'
};

// "iss": "https://securetoken.google.com/12345678",
// "picture": "https://plus.google.com/abcdefghijklmnopqrstu",
// "aud": "12345678",
// "auth_time": 1510357622,
// "user_id": "abcdefghijklmnopqrstu",
// "sub": "abcdefghijklmnopqrstu",
// "iat": 1510357622,
// "exp": 1510361222,
// "email": "[email protected]",
// "email_verified": true,
// "firebase": {"identities": {
// "email": ["[email protected]"]
// }, "sign_in_provider": "password"}
var tokenEmail = 'HEAD.ew0KICAiaXNzIjogImh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlL' +
'mNvbS8xMjM0NTY3OCIsDQogICJwaWN0dXJlIjogImh0dHBzOi8vcGx1cy5' +
'nb29nbGUuY29tL2FiY2RlZmdoaWprbG1ub3BxcnN0dSIsDQogICJhdWQiO' +
'iAiMTIzNDU2NzgiLA0KICAiYXV0aF90aW1lIjogMTUxMDM1NzYyMiwNCiA' +
'gInVzZXJfaWQiOiAiYWJjZGVmZ2hpamtsbW5vcHFyc3R1IiwNCiAgInN1Y' +
'iI6ICJhYmNkZWZnaGlqa2xtbm9wcXJzdHUiLA0KICAiaWF0IjogMTUxMDM' +
'1NzYyMiwNCiAgImV4cCI6IDE1MTAzNjEyMjIsDQogICJlbWFpbCI6ICJ1c' +
'2VyQGV4YW1wbGUuY29tIiwNCiAgImVtYWlsX3ZlcmlmaWVkIjogdHJ1ZSw' +
'NCiAgImZpcmViYXNlIjogew0KICAgICJpZGVudGl0aWVzIjogew0KICAgI' +
'CAgImVtYWlsIjogWw0KICAgICAgICAidXNlckBleGFtcGxlLmNvbSINCiA' +
'gICAgIF0NCiAgICB9LA0KICAgICJzaWduX2luX3Byb3ZpZGVyIjogInBhc' +
'3N3b3JkIg0KICB9DQp9.SIGNATURE';

// SignupNewUserResponse response without isNewUser field.
var signUpNewUserResponse = {
'kind': 'identitytoolkit#SignupNewUserResponse',
'idToken': tokenEmail,
'refreshToken': 'REFRESH_TOKEN',
'expiresIn': '3600',
'localId': '123456'
};

// Expected generic additional user info object for the above signUpNewUser
// response.
var expectedGenericAdditionalUserInfoForSignUpNewUser = {
'isNewUser': true,
'providerId': 'password'
};

// Typical minimal verifyAssertion response for generic IdP user with no profile
// data.
var noProfileVerifyAssertion = {
Expand Down Expand Up @@ -371,6 +413,19 @@ function testGenericAdditionalUserInfo() {
}



function testGenericAdditionalUserInfo_fromSignUpNewUserResponse() {
var genericAdditionalUserInfo = new fireauth.GenericAdditionalUserInfo(
signUpNewUserResponse);
assertObjectEquals(
expectedGenericAdditionalUserInfoForSignUpNewUser,
genericAdditionalUserInfo);
assertObjectEquals(
genericAdditionalUserInfo,
fireauth.AdditionalUserInfo.fromPlainObject(signUpNewUserResponse));
}


function testFederatedAdditionalUserInfo_withProfile() {
var federatedAdditionalUserInfo =
new fireauth.FederatedAdditionalUserInfo(facebookVerifyAssertion);
Expand Down
Loading