|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Parse, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @flow-weak |
| 10 | + */ |
| 11 | +import ParseUser from './ParseUser'; |
| 12 | +const uuidv4 = require('uuid/v4'); |
| 13 | + |
| 14 | +let registered = false; |
| 15 | + |
| 16 | +/** |
| 17 | + * Provides utility functions for working with Anonymously logged-in users. <br /> |
| 18 | + * Anonymous users have some unique characteristics: |
| 19 | + * <ul> |
| 20 | + * <li>Anonymous users don't need a user name or password.</li> |
| 21 | + * <ul> |
| 22 | + * <li>Once logged out, an anonymous user cannot be recovered.</li> |
| 23 | + * </ul> |
| 24 | + * <li>signUp converts an anonymous user to a standard user with the given username and password.</li> |
| 25 | + * <ul> |
| 26 | + * <li>Data associated with the anonymous user is retained.</li> |
| 27 | + * </ul> |
| 28 | + * <li>logIn switches users without converting the anonymous user.</li> |
| 29 | + * <ul> |
| 30 | + * <li>Data associated with the anonymous user will be lost.</li> |
| 31 | + * </ul> |
| 32 | + * <li>Service logIn (e.g. Facebook, Twitter) will attempt to convert |
| 33 | + * the anonymous user into a standard user by linking it to the service.</li> |
| 34 | + * <ul> |
| 35 | + * <li>If a user already exists that is linked to the service, it will instead switch to the existing user.</li> |
| 36 | + * </ul> |
| 37 | + * <li>Service linking (e.g. Facebook, Twitter) will convert the anonymous user |
| 38 | + * into a standard user by linking it to the service.</li> |
| 39 | + * </ul> |
| 40 | + * @class Parse.AnonymousUtils |
| 41 | + * @static |
| 42 | + */ |
| 43 | +const AnonymousUtils = { |
| 44 | + /** |
| 45 | + * Gets whether the user has their account linked to anonymous user. |
| 46 | + * |
| 47 | + * @method isLinked |
| 48 | + * @name Parse.AnonymousUtils.isLinked |
| 49 | + * @param {Parse.User} user User to check for. |
| 50 | + * The user must be logged in on this device. |
| 51 | + * @return {Boolean} <code>true</code> if the user has their account |
| 52 | + * linked to an anonymous user. |
| 53 | + * @static |
| 54 | + */ |
| 55 | + isLinked(user: ParseUser) { |
| 56 | + const provider = this._getAuthProvider(); |
| 57 | + return user._isLinked(provider.getAuthType()); |
| 58 | + }, |
| 59 | + |
| 60 | + /** |
| 61 | + * Logs in a user Anonymously. |
| 62 | + * |
| 63 | + * @method logIn |
| 64 | + * @name Parse.AnonymousUtils.logIn |
| 65 | + * @returns {Promise} |
| 66 | + * @static |
| 67 | + */ |
| 68 | + logIn() { |
| 69 | + const provider = this._getAuthProvider(); |
| 70 | + return ParseUser._logInWith(provider.getAuthType(), provider.getAuthData()); |
| 71 | + }, |
| 72 | + |
| 73 | + /** |
| 74 | + * Links Anonymous User to an existing PFUser. |
| 75 | + * |
| 76 | + * @method link |
| 77 | + * @name Parse.AnonymousUtils.link |
| 78 | + * @param {Parse.User} user User to link. This must be the current user. |
| 79 | + * @returns {Promise} |
| 80 | + * @static |
| 81 | + */ |
| 82 | + link(user: ParseUser) { |
| 83 | + const provider = this._getAuthProvider(); |
| 84 | + return user._linkWith(provider.getAuthType(), provider.getAuthData()); |
| 85 | + }, |
| 86 | + |
| 87 | + _getAuthProvider() { |
| 88 | + const provider = { |
| 89 | + restoreAuthentication() { |
| 90 | + return true; |
| 91 | + }, |
| 92 | + |
| 93 | + getAuthType() { |
| 94 | + return 'anonymous'; |
| 95 | + }, |
| 96 | + |
| 97 | + getAuthData() { |
| 98 | + return { |
| 99 | + authData: { |
| 100 | + id: uuidv4(), |
| 101 | + }, |
| 102 | + }; |
| 103 | + }, |
| 104 | + }; |
| 105 | + if (!registered) { |
| 106 | + ParseUser._registerAuthenticationProvider(provider); |
| 107 | + registered = true; |
| 108 | + } |
| 109 | + return provider; |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +export default AnonymousUtils; |
0 commit comments