Skip to content

Commit

Permalink
Merge pull request #25 from spencercarli/master
Browse files Browse the repository at this point in the history
Hash password and add `createUser` function
  • Loading branch information
Mokto committed Mar 19, 2016
2 parents adfd97e + 46a1785 commit a454f1f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,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

Expand Down
11 changes: 10 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import SHA256 from 'crypto-js/sha256';

var i = 0;
export function uniqueId () {
return (i++).toString();
}

export function contains (array, element) {
return array.indexOf(element) !== -1;
}
}

export function hashPassword (password) {
return {
digest: SHA256(password).toString(),
algorithm: "sha-256"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions src/Accounts.js
Original file line number Diff line number Diff line change
@@ -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);
});
},
}
4 changes: 3 additions & 1 deletion src/Meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import ListView from './ListView';
import collection from './Collection';


module.exports = {
export Accounts from './Accounts';

export default {
MeteorListView: ListView,
collection: collection,
getData() {
Expand Down
11 changes: 4 additions & 7 deletions src/User.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AsyncStorage } from 'react-native';

import Data from './Data';
import { hashPassword } from '../utils';

const TOKEN_KEY = 'reactnativemeteor_usertoken';

Expand All @@ -19,9 +20,7 @@ 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) {
Expand All @@ -35,15 +34,13 @@ module.exports = {
this._startLoggingIn();
this.call("login", {
user: selector,
password: password
password: hashPassword(password)
}, (err, result)=>{
this._endLoggingIn();

this._handleLoginCallback(err, result);

if(typeof callback == 'function') {
callback(err)
}
typeof callback == 'function' && callback(err);
});
},
_startLoggingIn() {
Expand Down

0 comments on commit a454f1f

Please sign in to comment.