forked from parse-community/Parse-SDK-JS
/
AnonymousUtils.js
113 lines (105 loc) · 3.16 KB
/
AnonymousUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* 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');
let registered = false;
/**
* Provides utility functions for working with Anonymously logged-in users. <br />
* Anonymous users have some unique characteristics:
* <ul>
* <li>Anonymous users don't need a user name or password.</li>
* <ul>
* <li>Once logged out, an anonymous user cannot be recovered.</li>
* </ul>
* <li>signUp converts an anonymous user to a standard user with the given username and password.</li>
* <ul>
* <li>Data associated with the anonymous user is retained.</li>
* </ul>
* <li>logIn switches users without converting the anonymous user.</li>
* <ul>
* <li>Data associated with the anonymous user will be lost.</li>
* </ul>
* <li>Service logIn (e.g. Facebook, Twitter) will attempt to convert
* the anonymous user into a standard user by linking it to the service.</li>
* <ul>
* <li>If a user already exists that is linked to the service, it will instead switch to the existing user.</li>
* </ul>
* <li>Service linking (e.g. Facebook, Twitter) will convert the anonymous user
* into a standard user by linking it to the service.</li>
* </ul>
* @class Parse.AnonymousUtils
* @static
*/
const AnonymousUtils = {
/**
* Gets whether the user has their account linked to anonymous user.
*
* @method isLinked
* @name Parse.AnonymousUtils.isLinked
* @param {Parse.User} user User to check for.
* The user must be logged in on this device.
* @return {Boolean} <code>true</code> 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
* @returns {Promise}
* @static
*/
logIn() {
const provider = this._getAuthProvider();
return ParseUser._logInWith(provider.getAuthType(), provider.getAuthData());
},
/**
* 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.
* @returns {Promise}
* @static
*/
link(user: ParseUser) {
const provider = this._getAuthProvider();
return user._linkWith(provider.getAuthType(), provider.getAuthData());
},
_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;