Skip to content
Merged
15 changes: 8 additions & 7 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 All @@ -320,16 +320,17 @@ function onSignInWithCustomToken(event) {
// The token can be directly specified on the html element.
var token = $('#user-custom-token').val();

auth.signInWithCustomToken(token)
.then(onAuthSuccess, onAuthError);
auth.signInAndRetrieveDataWithCustomToken(token)
.then(onAuthUserCredentialSuccess, onAuthError);
}


/**
* Signs in anonymously.
*/
function onSignInAnonymously() {
auth.signInAnonymously().then(onAuthSuccess, onAuthError);
auth.signInAnonymouslyAndRetrieveData()
.then(onAuthUserCredentialSuccess, onAuthError);
}


Expand Down
21 changes: 18 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,23 @@ 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];
// For custom token and anonymous token, set provider ID to null.
if (providerId == fireauth.idp.ProviderId.ANONYMOUS ||
providerId == fireauth.idp.ProviderId.CUSTOM) {
providerId = null;
}
// 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
104 changes: 88 additions & 16 deletions packages/auth/src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -1353,11 +1353,26 @@ fireauth.Auth.prototype.getIdTokenInternal = function(opt_forceRefresh) {


/**
* Sign in using a custom token (Bring Your Own Auth).
* Signs in a user asynchronously using a custom token.
* @param {string} token The custom token to sign in with.
* @return {!goog.Promise<!fireauth.AuthUser>}
*/
fireauth.Auth.prototype.signInWithCustomToken = function(token) {
// Get signInAndRetrieveDataWithCustomToken result and return the user only.
return this.signInAndRetrieveDataWithCustomToken(token)
.then(function(result) {
return result['user'];
});
};


/**
* Signs in a user asynchronously using a custom token and returns any
* additional user info data or credentials returned form the backend.
* @param {string} token The custom token to sign in with.
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
*/
fireauth.Auth.prototype.signInAndRetrieveDataWithCustomToken = function(token) {
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
Expand All @@ -1371,20 +1386,20 @@ fireauth.Auth.prototype.signInWithCustomToken = function(token) {
// response will look like an anonymous user (no credentials visible).
user.updateProperty('isAnonymous', false);
// Save isAnonymous flag changes to current user in storage.
return self.handleUserStateChange_(user);
}).then(function() {
return self.currentUser_();
self.handleUserStateChange_(user);
return result;
});
};


/**
* 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 +1408,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 +1437,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 Expand Up @@ -1457,10 +1503,24 @@ fireauth.Auth.prototype.signInAndRetrieveDataWithCredential =


/**
* Sign in using anonymously a user.
* Signs in a user anonymously.
* @return {!goog.Promise<!fireauth.AuthUser>}
*/
fireauth.Auth.prototype.signInAnonymously = function() {
// Get signInAnonymouslyAndRetrieveData result and return the user only.
return this.signInAnonymouslyAndRetrieveData()
.then(function(result) {
return result['user'];
});
};


/**
* Signs in a user anonymously and returns any additional user info data or
* credentials returned form the backend.
* @return {!goog.Promise<!fireauth.AuthEventManager.Result>}
*/
fireauth.Auth.prototype.signInAnonymouslyAndRetrieveData = function() {
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
Expand All @@ -1469,7 +1529,20 @@ fireauth.Auth.prototype.signInAnonymously = function() {
var user = self.currentUser_();
// If an anonymous user is already signed in, no need to sign him again.
if (user && user['isAnonymous']) {
return user;
var additionalUserInfo = fireauth.object.makeReadonlyCopy({
'providerId': null,
'isNewUser': false
});
return fireauth.object.makeReadonlyCopy({
// Return the signed in user reference.
'user': user,
// Do not return credential for anonymous user.
'credential': null,
// Return any additional IdP data.
'additionalUserInfo': additionalUserInfo,
// Sign in operation type.
'operationType': fireauth.constants.OperationType.SIGN_IN
});
} else {
// No anonymous user currently signed in.
return self.signInWithIdTokenProvider_(
Expand All @@ -1484,9 +1557,8 @@ fireauth.Auth.prototype.signInAnonymously = function() {
// overwrites.
user.updateProperty('isAnonymous', true);
// Save isAnonymous flag changes to current user in storage.
return self.handleUserStateChange_(user);
}).then(function() {
return self.currentUser_();
self.handleUserStateChange_(user);
return result;
});
}
});
Expand Down
16 changes: 16 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 @@ -111,6 +115,10 @@ fireauth.exportlib.exportPrototypeMethods(
name: 'signInAnonymously',
args: []
},
signInAnonymouslyAndRetrieveData: {
name: 'signInAnonymouslyAndRetrieveData',
args: []
},
signInWithCredential: {
name: 'signInWithCredential',
args: [fireauth.args.authCredential()]
Expand All @@ -119,10 +127,18 @@ fireauth.exportlib.exportPrototypeMethods(
name: 'signInWithCustomToken',
args: [fireauth.args.string('token')]
},
signInAndRetrieveDataWithCustomToken: {
name: 'signInAndRetrieveDataWithCustomToken',
args: [fireauth.args.string('token')]
},
signInWithEmailAndPassword: {
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
5 changes: 4 additions & 1 deletion packages/auth/src/idp.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ goog.provide('fireauth.idp.Settings');


/**
* Enums for supported provider IDs.
* Enums for supported provider IDs. These provider IDs correspond to the
* sign_in_provider in the Firebase ID token and do not correspond to the
* supported client exposed firebase.auth.AuthProviders.
* @enum {string}
*/
fireauth.idp.ProviderId = {
ANONYMOUS: 'anonymous',
CUSTOM: 'custom',
FACEBOOK: 'facebook.com',
FIREBASE: 'firebase',
GITHUB: 'github.com',
Expand Down
Loading