Skip to content

Commit

Permalink
Merge pull request #9 from tprzytula/1.0.0-WIP
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
tprzytula committed Aug 14, 2018
2 parents 1970a9e + 13f3438 commit 1913f50
Show file tree
Hide file tree
Showing 48 changed files with 1,970 additions and 1,255 deletions.
6 changes: 5 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
"no-underscore-dangle": 0,
"no-console": 0,
"one-var-declaration-per-line": 0,
"one-var": 0
"one-var": 0,
"function-paren-newline": 0,
"object-curly-newline": 0,
"require-string-literals": 0,
"import/prefer-default-export": 0
}
}
1 change: 1 addition & 0 deletions .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ tprzytula:remember-me
accounts-base
meteortesting:mocha
lmieulet:meteor-coverage
check
75 changes: 43 additions & 32 deletions .versions
Original file line number Diff line number Diff line change
@@ -1,45 +1,56 @@
[email protected]
[email protected]
[email protected]
babel-compiler@6.24.7
babel-runtime@1.1.1
[email protected].10
babel-compiler@7.1.1
babel-runtime@1.2.4
[email protected].11
[email protected]
boilerplate-generator@1.3.1
callback-hook@1.0.10
check@1.2.5
boilerplate-generator@1.5.0
callback-hook@1.1.0
check@1.3.1
[email protected]
ddp-client@2.2.0
ddp-common@1.3.0
ddp-client@2.3.2
ddp-common@1.4.0
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
tprzytula:[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
tprzytula:[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
28 changes: 7 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Meteor - Remember Me
# Meteor - Remember Me
[![Build Status](https://travis-ci.org/tprzytula/Meteor-Remember-Me.svg?branch=master)](https://travis-ci.org/tprzytula/Meteor-Remember-Me) [![Coverage Status](https://coveralls.io/repos/github/tprzytula/Meteor-Remember-Me/badge.svg)](https://coveralls.io/github/tprzytula/Meteor-Remember-Me)
### Integrated remember me functionality support for Meteor

##### RememberMe extension for the Meteor's accounts system

As you already know meteor login system is based on login tokens.
Each login token have it's own expiry time, until then it will stay active.
Expand Down Expand Up @@ -31,41 +30,28 @@ active for your phone!.

## Usage

To activate the functionality:

1. Import the package on server side:

```js
import RememberMe from 'meteor/tprzytula:remember-me';
```

2. Activate the functionality:

```js
RememberMe.activate()
```

3. Import the package on client side:
1. Import the package on the client side:

```js
import RememberMe from 'meteor/tprzytula:remember-me';
```

4. Change your login method to this:
2. Replace your login method with this:

```js
RememberMe.loginWithPassword(username, password, (error) => {
// Your previous logic
}, true)
}, false)
```

If you don't need a callback then you can simply change it to:

```js
RememberMe.loginWithPassword(username, password, true)
RememberMe.loginWithPassword(username, password, false)
```

## API
###### All methods are client side only

`loginWithPassword(string: username, string: password, func: callback, boolean: rememberMe = true)`

Expand Down
87 changes: 87 additions & 0 deletions client/accountsConfigurator.js
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;
59 changes: 59 additions & 0 deletions client/accountsWrapper.js
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;
Loading

0 comments on commit 1913f50

Please sign in to comment.