/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow-weak
*/
import ParseUser from './ParseUser';
const uuidv4 = require('uuid/v4');
/*:: import type { RequestOptions } from './RESTController';*/
let registered = false;
/**
* Provides utility functions for working with Anonymously logged-in users.
* Anonymous users have some unique characteristics:
*
true
if the user has their account
* linked to an anonymous user.
* @static
*/
isLinked(user
/*: ParseUser*/
) {
const provider = this._getAuthProvider();
return user._isLinked(provider.getAuthType());
},
/**
* Logs in a user Anonymously.
*
* @method logIn
* @name Parse.AnonymousUtils.logIn
* @param {Object} options MasterKey / SessionToken.
* @returns {Promise}
* @static
*/
logIn(options
/*:: ?: RequestOptions*/
) {
const provider = this._getAuthProvider();
return ParseUser._logInWith(provider.getAuthType(), provider.getAuthData(), options);
},
/**
* Links Anonymous User to an existing PFUser.
*
* @method link
* @name Parse.AnonymousUtils.link
* @param {Parse.User} user User to link. This must be the current user.
* @param {Object} options MasterKey / SessionToken.
* @returns {Promise}
* @static
*/
link(user
/*: ParseUser*/
, options
/*:: ?: RequestOptions*/
) {
const provider = this._getAuthProvider();
return user._linkWith(provider.getAuthType(), provider.getAuthData(), options);
},
_getAuthProvider() {
const provider = {
restoreAuthentication() {
return true;
},
getAuthType() {
return 'anonymous';
},
getAuthData() {
return {
authData: {
id: uuidv4()
}
};
}
};
if (!registered) {
ParseUser._registerAuthenticationProvider(provider);
registered = true;
}
return provider;
}
};
export default AnonymousUtils;