-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from tprzytula/1.0.0-WIP
Release 1.0.0
- Loading branch information
Showing
48 changed files
with
1,970 additions
and
1,255 deletions.
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 |
---|---|---|
|
@@ -21,3 +21,4 @@ tprzytula:remember-me | |
accounts-base | ||
meteortesting:mocha | ||
lmieulet:meteor-coverage | ||
check |
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,87 @@ | ||
import { prepareLoginWithPasswordMethod } from './lib/overriding'; | ||
|
||
const IS_CALLBACK_REGISTERED = 'tprzytula:remember-me_callbackRegistered'; | ||
const IS_METHOD_OVERRIDDEN = 'tprzytula:remember-me_methodOverridden'; | ||
const LOGIN_WITH_PASSWORD_METHOD = 'loginWithPassword'; | ||
|
||
/** | ||
* Extends AccountsClient instance with RememberMe functionality. | ||
* @property {AccountsClient} _accounts | ||
*/ | ||
class AccountsConfigurator { | ||
constructor(accounts) { | ||
this._accounts = accounts; | ||
} | ||
|
||
/** | ||
* Starts the configuration process. | ||
* @public | ||
*/ | ||
configure() { | ||
this._registerLoginMethod(); | ||
this._registerCallback(); | ||
} | ||
|
||
/** | ||
* Extends accounts with a "loginWithPassword" method, which is | ||
* based on the "Meteor.loginWithPassword". | ||
* @private | ||
*/ | ||
_registerLoginMethod() { | ||
if (LOGIN_WITH_PASSWORD_METHOD in this._accounts) return; | ||
this._accounts.loginWithPassword = prepareLoginWithPasswordMethod(this._accounts); | ||
} | ||
|
||
/** | ||
* Registers "onLogin" callback. | ||
* After successful login attempt the loginMethod will be overridden. | ||
* @private | ||
*/ | ||
_registerCallback() { | ||
if (IS_CALLBACK_REGISTERED in this._accounts) return; | ||
this._accounts.onLogin(this._overrideCallLoginMethod.bind(this)); | ||
this._accounts[IS_CALLBACK_REGISTERED] = true; | ||
} | ||
|
||
/** | ||
* Overrides Meteor's loginMethod. | ||
* From now each time user will perform login/autologin | ||
* the new loginMethod will be invoked. | ||
* @private | ||
*/ | ||
_overrideCallLoginMethod() { | ||
if (IS_METHOD_OVERRIDDEN in this._accounts) return; | ||
this._accounts.callLoginMethod = this._getNewCallLoginMethod(); | ||
this._accounts[IS_METHOD_OVERRIDDEN] = true; | ||
} | ||
|
||
/** | ||
* Prepares login method. | ||
* | ||
* Extends the method arguments with "loggedAtLeastOnce". | ||
* This argument will indicate to the server that we were already | ||
* logged in during this device session, so our previously set rememberMe | ||
* option should not affect the outcome of the next login attempt. | ||
* | ||
* Calls original callLoginMethod at the end with extended "methodArguments". | ||
* @returns {Function} callLoginMethod | ||
* @private | ||
*/ | ||
_getNewCallLoginMethod() { | ||
const accountsCallLoginMethod = this._accounts.callLoginMethod.bind(this._accounts); | ||
return (options) => { | ||
const preparedOptions = options || {}; | ||
const argument = { loggedAtLeastOnce: true }; | ||
if (preparedOptions.methodArguments) { | ||
preparedOptions.methodArguments.push(argument); | ||
} else { | ||
preparedOptions.methodArguments = [argument]; | ||
} | ||
accountsCallLoginMethod(preparedOptions); | ||
}; | ||
} | ||
} | ||
|
||
export const factory = (...params) => new AccountsConfigurator(...params); | ||
|
||
export default AccountsConfigurator; |
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,59 @@ | ||
import AccountsConfigurator from './accountsConfigurator'; | ||
import * as Alerts from './lib/alerts'; | ||
|
||
const updateFlagMethod = 'tprzytula:rememberMe-update'; | ||
|
||
/** | ||
* Wrapper for currently used accounts system. | ||
* @property {AccountsClient} _accounts | ||
*/ | ||
class AccountsWrapper { | ||
constructor(accounts) { | ||
this._accounts = accounts; | ||
} | ||
|
||
/** | ||
* Configures accounts. | ||
* @public | ||
*/ | ||
configure() { | ||
this._accountsConfigurator = new AccountsConfigurator(this._accounts); | ||
this._accountsConfigurator.configure(); | ||
} | ||
|
||
/** | ||
* Wraps login method from the accounts instance. | ||
* @param {string} username | ||
* @param {string} password | ||
* @param {boolean} flag | ||
* @param {function} callback | ||
* @public | ||
*/ | ||
loginWithPassword({ username, password, flag, callback }) { | ||
this._accounts.loginWithPassword(username, password, (error) => { | ||
if (!error) { | ||
this._updateFlag(flag); | ||
} | ||
callback(error); | ||
}); | ||
} | ||
|
||
/** | ||
* Informs the server of the state update of rememberMe flag. | ||
* @param {boolean} flag | ||
* @private | ||
*/ | ||
_updateFlag(flag) { | ||
this._accounts.connection.call(updateFlagMethod, flag, (error) => { | ||
if (error && error.error === 404) { | ||
Alerts.rememberMeNotActive(); | ||
} else if (error) { | ||
Alerts.couldNotUpdateRememberMe(error); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export const factory = (...params) => new AccountsWrapper(...params); | ||
|
||
export default AccountsWrapper; |
Oops, something went wrong.