New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Anonymous Users #750
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a303702
initial commit
dplewis cc66a8b
unit tests
dplewis 62d5992
cleanup
dplewis d546549
Merge remote-tracking branch 'upstream/master' into anonymousUtils
dplewis caf464d
more tests
dplewis a89e87b
Improve documentation
dplewis c99991e
improve coverage
dplewis d91f01f
Merge branch 'master' into anonymousUtils
dplewis 0e98959
clean up
dplewis File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
jest.dontMock('../AnonymousUtils'); | ||
|
||
class MockUser { | ||
constructor () { | ||
this.className = '_User'; | ||
this.attributes = {}; | ||
} | ||
_isLinked() {} | ||
_linkWith() {} | ||
static _registerAuthenticationProvider() {} | ||
static _logInWith() {} | ||
} | ||
|
||
jest.setMock('../ParseUser', MockUser); | ||
|
||
const mockProvider = { | ||
restoreAuthentication() { | ||
return true; | ||
}, | ||
|
||
getAuthType() { | ||
return 'anonymous'; | ||
}, | ||
|
||
getAuthData() { | ||
return { | ||
authData: { | ||
id: '1234', | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
const AnonymousUtils = require('../AnonymousUtils').default; | ||
|
||
describe('AnonymousUtils', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.spyOn( | ||
AnonymousUtils, | ||
'_getAuthProvider' | ||
) | ||
.mockImplementation(() => mockProvider); | ||
}); | ||
|
||
it('can register provider', () => { | ||
AnonymousUtils._getAuthProvider.mockRestore(); | ||
jest.spyOn(MockUser, '_registerAuthenticationProvider'); | ||
AnonymousUtils._getAuthProvider(); | ||
AnonymousUtils._getAuthProvider(); | ||
expect(MockUser._registerAuthenticationProvider).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can check user isLinked', () => { | ||
const user = new MockUser(); | ||
jest.spyOn(user, '_isLinked'); | ||
AnonymousUtils.isLinked(user); | ||
expect(user._isLinked).toHaveBeenCalledTimes(1); | ||
expect(user._isLinked).toHaveBeenCalledWith('anonymous'); | ||
expect(AnonymousUtils._getAuthProvider).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can link user', () => { | ||
const user = new MockUser(); | ||
jest.spyOn(user, '_linkWith'); | ||
AnonymousUtils.link(user); | ||
expect(user._linkWith).toHaveBeenCalledTimes(1); | ||
expect(user._linkWith).toHaveBeenCalledWith('anonymous', mockProvider.getAuthData()); | ||
expect(AnonymousUtils._getAuthProvider).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can login user', () => { | ||
jest.spyOn(MockUser, '_logInWith'); | ||
AnonymousUtils.logIn(); | ||
expect(MockUser._logInWith).toHaveBeenCalledTimes(1); | ||
expect(MockUser._logInWith).toHaveBeenCalledWith('anonymous', mockProvider.getAuthData()); | ||
expect(AnonymousUtils._getAuthProvider).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice if this were exposed on the parse user.....