From 566c305433ae9a0ecea08f5c8dc5e3c8649d0fe1 Mon Sep 17 00:00:00 2001 From: Spencer Carli Date: Thu, 10 Mar 2016 22:09:18 -0500 Subject: [PATCH 1/7] Trim login inputs and hash password --- lib/utils.js | 6 +++++- package.json | 1 + src/User.js | 8 +++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 1e03cf2..22f4558 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -5,4 +5,8 @@ export function uniqueId () { export function contains (array, element) { return array.indexOf(element) !== -1; -} \ No newline at end of file +} + +export function trimString (str) { + return str.replace(/^\s+|\s+$/gm,''); +} diff --git a/package.json b/package.json index bfbf901..96951b6 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "homepage": "https://github.com/inProgress-team/react-native-meteor#readme", "dependencies": { + "crypto-js": "^3.1.6", "ejson": "^2.1.2", "minimongo-cache": "0.0.48", "react-mixin": "^3.0.3", diff --git a/src/User.js b/src/User.js index c145410..5300a44 100644 --- a/src/User.js +++ b/src/User.js @@ -1,5 +1,7 @@ import { AsyncStorage } from 'react-native'; +import SHA256 from 'crypto-js/sha256'; +import { trimString } from '../lib/utils'; import Data from './Data'; const TOKEN_KEY = 'reactnativemeteor_usertoken'; @@ -27,15 +29,15 @@ module.exports = { loginWithPassword(selector, password, callback) { if (typeof selector === 'string') { if (selector.indexOf('@') === -1) - selector = {username: selector}; + selector = {username: trimString(selector)}; else - selector = {email: selector}; + selector = {email: trimString(selector)}; } this._startLoggingIn(); this.call("login", { user: selector, - password: password + password: SHA256(password).toString() }, (err, result)=>{ this._endLoggingIn(); From 544d24f1d839aa3cca6d26545aa316773fe935c7 Mon Sep 17 00:00:00 2001 From: Spencer Carli Date: Fri, 11 Mar 2016 09:00:42 -0500 Subject: [PATCH 2/7] Create user --- src/User.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/User.js b/src/User.js index 5300a44..8c14e13 100644 --- a/src/User.js +++ b/src/User.js @@ -48,6 +48,24 @@ module.exports = { } }); }, + createUser(options, callback) { + if (options.username) options.username = trimString(options.username); + if (options.email) options.email = trimString(options.email); + + // Replace password with the hashed password. + options.password = SHA256(options.password).toString(); + + this._startLoggingIn(); + this.call("createUser", options, (err, result)=>{ + this._endLoggingIn(); + + this._handleLoginCallback(err, result); + + if(typeof callback == 'function') { + callback(err) + } + }); + }, _startLoggingIn() { this._isLoggingIn = true; Data._notifyLoggingIn(); From cde5f0419ba8579a41cedba999158444a890a610 Mon Sep 17 00:00:00 2001 From: Spencer Carli Date: Wed, 16 Mar 2016 20:58:58 -0400 Subject: [PATCH 3/7] Trim password --- lib/utils.js | 1 + src/User.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 22f4558..98bbb2f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,5 +8,6 @@ export function contains (array, element) { } export function trimString (str) { + if (typeof str !== 'string') return; return str.replace(/^\s+|\s+$/gm,''); } diff --git a/src/User.js b/src/User.js index 8c14e13..425b92d 100644 --- a/src/User.js +++ b/src/User.js @@ -37,7 +37,7 @@ module.exports = { this._startLoggingIn(); this.call("login", { user: selector, - password: SHA256(password).toString() + password: SHA256(trimString(password)).toString() }, (err, result)=>{ this._endLoggingIn(); @@ -53,7 +53,7 @@ module.exports = { if (options.email) options.email = trimString(options.email); // Replace password with the hashed password. - options.password = SHA256(options.password).toString(); + options.password = SHA256(trimString(options.password)).toString(); this._startLoggingIn(); this.call("createUser", options, (err, result)=>{ From 775b2ac02c7c69f12b3583763846fd6d122eb348 Mon Sep 17 00:00:00 2001 From: Spencer Carli Date: Fri, 18 Mar 2016 16:08:47 -0400 Subject: [PATCH 4/7] Remove trimString, concise callback check, add digest to password field --- lib/utils.js | 5 ----- src/User.js | 28 +++++++++++++++------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 98bbb2f..e91a1d4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -6,8 +6,3 @@ export function uniqueId () { export function contains (array, element) { return array.indexOf(element) !== -1; } - -export function trimString (str) { - if (typeof str !== 'string') return; - return str.replace(/^\s+|\s+$/gm,''); -} diff --git a/src/User.js b/src/User.js index 425b92d..de94b33 100644 --- a/src/User.js +++ b/src/User.js @@ -1,11 +1,17 @@ import { AsyncStorage } from 'react-native'; import SHA256 from 'crypto-js/sha256'; -import { trimString } from '../lib/utils'; import Data from './Data'; const TOKEN_KEY = 'reactnativemeteor_usertoken'; +const hashPassword = (password) => { + return { + digest: SHA256(password).toString(), + algorithm: "sha-256" + } +} + module.exports = { user() { return this.collection('users').findOne(this._userIdSaved); @@ -21,39 +27,35 @@ module.exports = { logout(callback) { this.call("logout", function(err) { AsyncStorage.removeItem(TOKEN_KEY); - if(typeof callback == 'function') { - callback(err); - } + typeof callback == 'function' && callback(err); }); }, loginWithPassword(selector, password, callback) { if (typeof selector === 'string') { if (selector.indexOf('@') === -1) - selector = {username: trimString(selector)}; + selector = {username: selector}; else - selector = {email: trimString(selector)}; + selector = {email: selector}; } this._startLoggingIn(); this.call("login", { user: selector, - password: SHA256(trimString(password)).toString() + password: hashPassword(password) }, (err, result)=>{ this._endLoggingIn(); this._handleLoginCallback(err, result); - if(typeof callback == 'function') { - callback(err) - } + typeof callback == 'function' && callback(err); }); }, createUser(options, callback) { - if (options.username) options.username = trimString(options.username); - if (options.email) options.email = trimString(options.email); + if (options.username) options.username = options.username; + if (options.email) options.email = options.email; // Replace password with the hashed password. - options.password = SHA256(trimString(options.password)).toString(); + options.password = hashPassword(options.password); this._startLoggingIn(); this.call("createUser", options, (err, result)=>{ From 5ba034e3980e8a6a493d6e7e282be94ae3d1f214 Mon Sep 17 00:00:00 2001 From: Spencer Carli Date: Fri, 18 Mar 2016 16:19:26 -0400 Subject: [PATCH 5/7] Move createuser to accounts --- lib/utils.js | 7 +++++++ src/Accounts.js | 22 ++++++++++++++++++++++ src/Meteor.js | 4 +++- src/User.js | 26 +------------------------- 4 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 src/Accounts.js diff --git a/lib/utils.js b/lib/utils.js index e91a1d4..f462c94 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -6,3 +6,10 @@ export function uniqueId () { export function contains (array, element) { return array.indexOf(element) !== -1; } + +export function hashPassword (password) { + return { + digest: SHA256(password).toString(), + algorithm: "sha-256" + } +} diff --git a/src/Accounts.js b/src/Accounts.js new file mode 100644 index 0000000..d767880 --- /dev/null +++ b/src/Accounts.js @@ -0,0 +1,22 @@ +import User from './User'; +import Meteor from './Meteor'; +import { hashPassword } from '../utils'; + +export default { + createUser(options, callback) { + if (options.username) options.username = options.username; + if (options.email) options.email = options.email; + + // Replace password with the hashed password. + options.password = hashPassword(options.password); + + User._startLoggingIn(); + Meteor.call("createUser", options, (err, result)=>{ + User._endLoggingIn(); + + User._handleLoginCallback(err, result); + + typeof callback == 'function' && callback(err); + }); + }, +} diff --git a/src/Meteor.js b/src/Meteor.js index 1c5650c..281df65 100644 --- a/src/Meteor.js +++ b/src/Meteor.js @@ -9,7 +9,9 @@ import Mixin from './Mixin'; import User from './User'; import ListView from './ListView'; -module.exports = { +export Accounts from './Accounts'; + +export default { MeteorListView: ListView, connectMeteor(reactClass) { return reactMixin.onClass(reactClass, Mixin); diff --git a/src/User.js b/src/User.js index de94b33..9b33f32 100644 --- a/src/User.js +++ b/src/User.js @@ -2,16 +2,10 @@ import { AsyncStorage } from 'react-native'; import SHA256 from 'crypto-js/sha256'; import Data from './Data'; +import { hashPassword } from '../utils'; const TOKEN_KEY = 'reactnativemeteor_usertoken'; -const hashPassword = (password) => { - return { - digest: SHA256(password).toString(), - algorithm: "sha-256" - } -} - module.exports = { user() { return this.collection('users').findOne(this._userIdSaved); @@ -50,24 +44,6 @@ module.exports = { typeof callback == 'function' && callback(err); }); }, - createUser(options, callback) { - if (options.username) options.username = options.username; - if (options.email) options.email = options.email; - - // Replace password with the hashed password. - options.password = hashPassword(options.password); - - this._startLoggingIn(); - this.call("createUser", options, (err, result)=>{ - this._endLoggingIn(); - - this._handleLoginCallback(err, result); - - if(typeof callback == 'function') { - callback(err) - } - }); - }, _startLoggingIn() { this._isLoggingIn = true; Data._notifyLoggingIn(); From 49d60fc883ccf8dfb380a27c55c3606dae9bec01 Mon Sep 17 00:00:00 2001 From: Spencer Carli Date: Fri, 18 Mar 2016 16:25:52 -0400 Subject: [PATCH 6/7] Fix an import issue --- lib/utils.js | 2 ++ src/User.js | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index f462c94..de49fc5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +1,5 @@ +import SHA256 from 'crypto-js/sha256'; + var i = 0; export function uniqueId () { return (i++).toString(); diff --git a/src/User.js b/src/User.js index 9b33f32..b037f32 100644 --- a/src/User.js +++ b/src/User.js @@ -1,5 +1,4 @@ import { AsyncStorage } from 'react-native'; -import SHA256 from 'crypto-js/sha256'; import Data from './Data'; import { hashPassword } from '../utils'; From 46a1785f5f7ea82e417e09c180eac020a9dfa8a1 Mon Sep 17 00:00:00 2001 From: Spencer Carli Date: Fri, 18 Mar 2016 16:27:06 -0400 Subject: [PATCH 7/7] Add createUser to the docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f0e60dc..d36e0d4 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ Disconnect from the DDP server. * [Meteor.loginWithPassword](http://docs.meteor.com/#/full/meteor_loginwithpassword) (Please note that user is auto-resigned in - like in Meteor Web applications - thanks to React Native AsyncStorage.) * [Meteor.logout](http://docs.meteor.com/#/full/meteor_logout) * [Meteor.call](http://docs.meteor.com/#/full/meteor_call) - +* [Accounts.createUser](http://docs.meteor.com/#/full/accounts_createuser) ## Meteor.ddp