accounts 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,36 +1,4 @@
1
1
  {
2
2
  "name": "accounts",
3
- "version": "0.2.0",
4
- "description": "Accounts Management module",
5
- "main": "lib/index.js",
6
- "scripts": {
7
- "test": "./node_modules/mocha/bin/mocha -R spec -t 60000",
8
- "docs": "jsdoc -d ./docs/ ./lib/*"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "https://github.com/Mitica/node-accounts.git"
13
- },
14
- "keywords": [
15
- "accounts",
16
- "users"
17
- ],
18
- "author": "Dumitru Cantea",
19
- "license": "ISC",
20
- "bugs": {
21
- "url": "https://github.com/Mitica/node-accounts/issues"
22
- },
23
- "homepage": "https://github.com/Mitica/node-accounts",
24
- "dependencies": {
25
- "bluebird": "^3.3.4",
26
- "debug": "^2.2.0",
27
- "joi": "^6.10.1",
28
- "lodash": "^4.6.1",
29
- "randomstring": "^1.1.4",
30
- "uuid": "^2.0.1"
31
- },
32
- "devDependencies": {
33
- "dotenv": "^2.0.0",
34
- "mocha": "^2.4.5"
35
- }
3
+ "version": "0.3.0"
36
4
  }
package/README.md DELETED
@@ -1,97 +0,0 @@
1
- # node-accounts
2
-
3
- **accounts** is a simple User Management module for Node.js.
4
-
5
- Current version supports logins only with providers(Google, Yahoo, Facebook, etc.).
6
-
7
- ## Usage
8
-
9
- All you need to do is to create an application, and then use appId for managing users.
10
-
11
- #### Create a new app
12
- ```
13
- var Accounts = require('accounts')(storage, options);
14
- var appId;
15
-
16
- Accounts.apps.create({
17
- name: 'Test app'
18
- }).then(function(app) {
19
- appId = app.id;
20
- });
21
- ```
22
-
23
- #### Provider login
24
- ```
25
- var Accounts = require('accounts')(storage);
26
- var appId = process.env.ACCOUNTS_APP_ID;
27
- var App = Accounts.app(appId);
28
-
29
- App.login('social', profile)
30
- .then(function(user) {
31
- if (user) {
32
- console.log(user);
33
- }
34
- else {
35
- console.log('login faild');
36
- }
37
- });
38
- ```
39
- Where `Profile` is a Passport [User Profile](http://passportjs.org/guide/profile/)
40
- ```
41
- var profile = {
42
- provider: 'facebook',
43
- id: '123124234235123',
44
- displayName: 'Dumitru K',
45
- accessData: {
46
- accessToken:'dsgsgs', refreshToken:'gerge'
47
- }
48
- };
49
- ```
50
-
51
- ## API
52
-
53
- ### (storage, options)
54
-
55
- Creates a new Client.
56
-
57
- ## Client API
58
-
59
- ### admin
60
-
61
- - **sync**() - Syncronize DB tables.
62
- - **drop**(secret) - Drop DB tables. Useful for tests.
63
-
64
- ### apps
65
-
66
- - **create**(data) - Create a new application.
67
- - **getById**(id) - Get an application by id.
68
-
69
- ### app(appId)
70
-
71
- Creates a new Application Client for a given app id.
72
-
73
- ## Application Client API
74
-
75
- ### login(type, data, options) - User login.
76
-
77
- ### users
78
-
79
- - **create**(data, options) - Create a new user.
80
- - **getById**(id, options) - Get an user object by id.
81
- - **update**(data, options) - Update user fields.
82
- - **deleteById**(id, options) - Delete an user by id.
83
-
84
- ### identities
85
-
86
- - **create**(data, options) - Create a new identity.
87
- - **getById**(id, options) - Get an user identity by id.
88
- - **findByUserId**(userId, options) - Find user identities by user id.
89
- - **update**(data, options) - Update an identity.
90
- - **deleteById**(id, options) - Delete an user identity by id.
91
- - **deleteByUserId**(userId, options) - Delete user identities by user id.
92
-
93
-
94
- ## Storages
95
-
96
- - DynamoDB: [dynamo-accounts](https://github.com/Mitica/dynamo-accounts);
97
- - MongoDB: [mongo-accounts](https://github.com/Mitica/mongo-accounts);
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- var debug = require('debug')('accounts');
4
-
5
- exports.pre = {
6
- sync: function() {
7
- debug('Syncing accounts DB...');
8
- },
9
- drop: function(secret, options) {
10
- if (arguments.length < 2) {
11
- throw new Error('Invalid method arguments');
12
- }
13
- if (secret !== options.secret) {
14
- throw new Error('Secret argument is invalid!');
15
- }
16
- }
17
- };
18
-
19
- exports.post = {
20
- sync: function() {
21
- debug('Synced accounts DB');
22
- },
23
- drop: function() {
24
- debug('Accounts DB droped!');
25
- }
26
- };
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- var storageBind = require('../storage_bind');
4
- var hooks = require('./hooks');
5
- var methods = require('./methods');
6
-
7
- module.exports = function createAdmin(storage, options) {
8
-
9
- var client = {};
10
-
11
- storageBind(client, storage.admin, methods, hooks, options);
12
-
13
- return client;
14
- };
@@ -1,6 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = {
4
- sync: { args: 0 },
5
- drop: { args: 1 }
6
- };
package/lib/app.js DELETED
@@ -1,50 +0,0 @@
1
- 'use strict';
2
-
3
- var createUsers = require('./users');
4
- var createIdentities = require('./identities');
5
- var createLogin = require('./login');
6
- var Options = require('./options');
7
-
8
- /**
9
- * Creates a new Api object.
10
- * @constructs Api
11
- * @param {Storage} storage A Storage instance.
12
- * @param {ApiOptions} [options] Api options.
13
- * @return {Api} An instance of Api.
14
- */
15
- module.exports = function createApi(storage, appId, options) {
16
-
17
- options = Options.app(options);
18
-
19
- var users = createUsers(storage, appId, options);
20
- var identities = createIdentities(storage, appId, options);
21
- // var fields = createFields(storage, appId, options);
22
-
23
- /**
24
- * Api object
25
- * @lends Api#
26
- */
27
- var client = {
28
- // fields: fields,
29
- /**
30
- * An Users object
31
- * @type {Users}
32
- */
33
- users: users,
34
- /**
35
- * An Identities object
36
- * @type {Identities}
37
- */
38
- identities: identities,
39
- /**
40
- * Login an user
41
- * @function
42
- * @param {UserProfile} profile - User's Profile
43
- * @param {DataOptions} [options] - Login options
44
- * @return {Promise<String>} Returns an user id.
45
- */
46
- login: createLogin(users, identities, storage, appId, options)
47
- };
48
-
49
- return client;
50
- };
@@ -1,11 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('../utils');
4
- var randomString = utils.randomString;
5
-
6
- exports.newId = function newId() {
7
- return randomString({
8
- length: 16,
9
- charset: 'alphanumeric'
10
- }).toLowerCase();
11
- };
package/lib/apps/hooks.js DELETED
@@ -1,32 +0,0 @@
1
- 'use strict';
2
-
3
- var debug = require('debug')('accounts');
4
- var validator = require('../validator');
5
-
6
- exports.pre = {
7
- create: function(data) {
8
- debug('Creating a new App', data);
9
-
10
- data = validator.create('apps', data);
11
-
12
- return data;
13
- },
14
-
15
- update: function(data) {
16
- debug('Updating App', data);
17
-
18
- data = validator.update('apps', data);
19
-
20
- return data;
21
- }
22
- };
23
-
24
- exports.post = {
25
- create: function(result) {
26
- debug('Created App', result.name);
27
- },
28
-
29
- update: function(result) {
30
- debug('Updated App', result.name);
31
- }
32
- };
package/lib/apps/index.js DELETED
@@ -1,25 +0,0 @@
1
- 'use strict';
2
-
3
- var storageBind = require('../storage_bind');
4
- var hooks = require('./hooks');
5
- var methods = require('./methods');
6
-
7
- /**
8
- * Creates an Apps object
9
- * @constructs Apps
10
- * @param {Storage} storage - A Storage object
11
- * @param {ClientOptions} [options] - Client options
12
- * @return {Apps}
13
- */
14
- module.exports = function createApps(storage, options) {
15
-
16
- /**
17
- * Apps object
18
- * @lends Apps
19
- */
20
- var client = {};
21
-
22
- storageBind(client, storage.apps, methods, hooks, options);
23
-
24
- return client;
25
- };
@@ -1,23 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * @lends Apps#
5
- */
6
- module.exports = {
7
- /**
8
- * Create a new App
9
- * @func
10
- * @param {AppRecord} data - App data
11
- * @param {DataOptions} [options] - Data options
12
- * @return {Promise<App>}
13
- */
14
- create: { args: 1 },
15
- /**
16
- * Get an app by id
17
- * @func
18
- * @param {String} id - App id
19
- * @param {DataOptions} [options] - Data options
20
- * @return {Promise<App>}
21
- */
22
- getById: { args: 1 }
23
- };
@@ -1,49 +0,0 @@
1
- 'use strict';
2
-
3
- var Joi = require('joi');
4
- var helpers = require('./helpers');
5
-
6
- /**
7
- * App object
8
- * @class App
9
- */
10
- exports.create = {
11
- /**
12
- * Application id.
13
- * A hex string. Min 16, max 32 chars.
14
- * @memberof App#
15
- * @type {String}
16
- */
17
- id: Joi.string().alphanum().lowercase().min(16).max(32).default(helpers.newId, 'new id'),
18
- /**
19
- * Application name. Min 3, max 64 chars.
20
- * @memberof App#
21
- * @type {String}
22
- */
23
- name: Joi.string().trim().min(3).max(64).required(),
24
- /**
25
- * Created date. Unix time in milliseconds.
26
- * @memberof App#
27
- * @type {Number}
28
- */
29
- createdAt: Joi.number().integer().min(0).default(Date.now, 'time of creation'),
30
- /**
31
- * Updated date. Unix time in milliseconds.
32
- * @memberof App#
33
- * @type {Number}
34
- */
35
- updatedAt: Joi.number().integer().min(0),
36
- /**
37
- * Application custom data. Max 1000 chars.
38
- * @memberof App#
39
- * @type {String}
40
- */
41
- metadata: Joi.string().trim().max(1000)
42
- };
43
-
44
- exports.update = {
45
- id: Joi.string().alphanum().lowercase().min(16).max(32).required(),
46
- name: Joi.string().trim().min(3).max(64).not(null),
47
- updatedAt: Joi.number().integer().min(0).default(Date.now, 'time of updating').not(null),
48
- metadata: Joi.string().trim().max(1000)
49
- };
@@ -1,8 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('../utils');
4
- var md5 = utils.md5;
5
-
6
- exports.createId = function createId(appId, provider, profileId) {
7
- return md5([appId.trim(), provider.trim().toLowerCase(), profileId.trim()].join('|'));
8
- };
@@ -1,30 +0,0 @@
1
- 'use strict';
2
-
3
- var debug = require('debug')('accounts');
4
- var validator = require('../validator');
5
-
6
- exports.pre = {
7
- create: function(appId, data) {
8
- debug('Creating a new Field...', appId);
9
-
10
- data = validator.create('fields', data);
11
-
12
- return data;
13
- },
14
- update: function(appId, data) {
15
- debug('Updating a Field...', data);
16
-
17
- data = validator.update('fields', data);
18
-
19
- return data;
20
- }
21
- };
22
-
23
- exports.post = {
24
- create: function(result) {
25
- debug('Created a new Field', result.id);
26
- },
27
- update: function(result) {
28
- debug('Updated a Field', result.id);
29
- }
30
- };
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- var storageBind = require('../storage_bind');
4
- var hooks = require('./hooks');
5
- var methods = require('./methods');
6
-
7
- /**
8
- * Creates a new Fields object.
9
- * @constructs Fields
10
- * @param {Storage} storage - A Storage instance.
11
- * @param {String} appId - An applicatin id
12
- * @param {AppOptions} [options] App options.
13
- * @return {Fields}
14
- */
15
- module.exports = function createFields(storage, appId, options) {
16
-
17
- /**
18
- * Fields object
19
- * @lends Fields
20
- */
21
- var client = {};
22
-
23
- storageBind(client, storage.fields, methods, hooks, options, appId);
24
-
25
- return client;
26
- };
@@ -1,13 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * @lends Fields
5
- */
6
- module.exports = {
7
- create: { args: 1 },
8
- update: { args: 1 },
9
- getById: { args: 1 },
10
- findByUserId: { args: 1 },
11
- deleteById: { args: 1 },
12
- deleteByUserId: { args: 1 }
13
- };
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- var Joi = require('joi');
4
-
5
- /**
6
- * Connection class
7
- * @class Connection
8
- */
9
- exports.create = {
10
- userId: Joi.string().guid().required(),
11
- name: Joi.string().trim().min(2).max(50).lowercase().required(),
12
- value: Joi.string().trim().max(400).required()
13
- };
14
-
15
- exports.update = {
16
- userId: Joi.string().guid().required(),
17
- name: Joi.string().trim().min(2).max(50).lowercase().required().not(null),
18
- value: Joi.string().trim().max(400).required().not(null)
19
- };
@@ -1,20 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('../utils');
4
- // var md5 = utils.md5;
5
- var sha1 = utils.sha1;
6
-
7
- function hash(data) {
8
- return sha1(data.join('|'));
9
- }
10
-
11
- exports.createId = function createId(type, key) {
12
- return hash([type.trim().toLowerCase(), key.trim()]);
13
- };
14
-
15
- exports.createKey = function createKey(type, name, value) {
16
- if (type === 'social') {
17
- return hash([name.trim(), value.trim()]);
18
- }
19
- return hash([value.trim()]);
20
- };
@@ -1,30 +0,0 @@
1
- 'use strict';
2
-
3
- var debug = require('debug')('accounts');
4
- var validator = require('../validator');
5
-
6
- exports.pre = {
7
- create: function(appId, data) {
8
- debug('Creating a new Identity...', appId);
9
-
10
- data = validator.create('identities', data);
11
-
12
- return data;
13
- },
14
- update: function(appId, data) {
15
- debug('Updating a Identity...', data);
16
-
17
- data = validator.update('identities', data);
18
-
19
- return data;
20
- }
21
- };
22
-
23
- exports.post = {
24
- create: function(result) {
25
- debug('Created a new Identity', result.id);
26
- },
27
- update: function(result) {
28
- debug('Updated a Identity', result.id);
29
- }
30
- };
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- var storageBind = require('../storage_bind');
4
- var hooks = require('./hooks');
5
- var methods = require('./methods');
6
-
7
- /**
8
- * Creates a new Identities object.
9
- * @constructs Identities
10
- * @param {Storage} storage - A Storage instance.
11
- * @param {String} appId - An applicatin id
12
- * @param {AppOptions} [options] App options.
13
- * @return {Identities}
14
- */
15
- module.exports = function createIdentities(storage, appId, options) {
16
-
17
- /**
18
- * Identities object
19
- * @lends Identities
20
- */
21
- var client = {};
22
-
23
- storageBind(client, storage.identities, methods, hooks, options, appId);
24
-
25
- return client;
26
- };
@@ -1,61 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * @lends Connections
5
- */
6
- module.exports = {
7
- /**
8
- * Creates a new Connection
9
- * @function
10
- * @param {ConnectionRecord} data - User object
11
- * @param {DataOptions} [options] - Data options
12
- * @return {Promise<ConnectionRecord>}
13
- * @instance
14
- */
15
- create: { args: 1 },
16
- /**
17
- * Update a Connection fields
18
- * @function
19
- * @param {ConnectionRecord} data - Connection object
20
- * @param {DataOptions} [options] - Data options
21
- * @return {Promise<ConnectionRecord>}
22
- * @instance
23
- */
24
- update: { args: 1 },
25
- /**
26
- * Get a Connection by id
27
- * @function
28
- * @param {String} id - Connection id
29
- * @param {DataOptions} [options] - Data options
30
- * @return {Promise<ConnectionRecord>}
31
- * @instance
32
- */
33
- getById: { args: 1 },
34
- /**
35
- * Find Connections by user id
36
- * @function
37
- * @param {String} userId - User id
38
- * @param {DataOptions} [options] - Data options
39
- * @return {Promise<ConnectionRecord[]>}
40
- * @instance
41
- */
42
- findByUserId: { args: 1 },
43
- /**
44
- * Delete an Connection by id
45
- * @function
46
- * @param {String} id - Connection id
47
- * @param {DataOptions} [options] - Data options
48
- * @return {Promise<Number>}
49
- * @instance
50
- */
51
- deleteById: { args: 1 },
52
- /**
53
- * Delete an Connections by user id
54
- * @function
55
- * @param {String} userId - User id
56
- * @param {DataOptions} [options] - Data options
57
- * @return {Promise<Number>}
58
- * @instance
59
- */
60
- deleteByUserId: { args: 1 }
61
- };
@@ -1,137 +0,0 @@
1
- 'use strict';
2
-
3
- var Joi = require('joi');
4
-
5
- var IDENTITY_TYPES = ['social', 'email', 'username', 'phone'];
6
-
7
- var profileSchema = Joi.object().keys({
8
- /**
9
- * User email.
10
- * @memberof Identity#
11
- * @type {String}
12
- */
13
- email: Joi.string().trim().lowercase().email(),
14
- /**
15
- * User username.
16
- * @memberof Identity#
17
- * @type {String}
18
- */
19
- username: Joi.string().trim().min(3).max(50),
20
- /**
21
- * User display name.
22
- * @memberof Identity#
23
- * @type {String}
24
- */
25
- displayName: Joi.string().trim().min(3).max(100),
26
- /**
27
- * User family name.
28
- * @memberof Identity#
29
- * @type {String}
30
- */
31
- familyName: Joi.string().trim().max(50),
32
- /**
33
- * User given name.
34
- * @memberof Identity#
35
- * @type {String}
36
- */
37
- givenName: Joi.string().trim().max(50),
38
- /**
39
- * User middle name.
40
- * @memberof Identity#
41
- * @type {String}
42
- */
43
- middleName: Joi.string().trim().max(50),
44
- /**
45
- * User gender: `male` or `female`.
46
- * @memberof Identity#
47
- * @type {String}
48
- */
49
- gender: Joi.valid('male', 'female'),
50
- /**
51
- * User photo.
52
- * @memberof Identity#
53
- * @type {String}
54
- */
55
- photo: Joi.string().trim().max(255),
56
- /**
57
- * User profile url.
58
- * @memberof Identity#
59
- * @type {String}
60
- */
61
- url: Joi.string().trim().max(255)
62
- });
63
-
64
- /**
65
- * Identity class
66
- * @class Identity
67
- */
68
- exports.create = {
69
- /**
70
- * Identity id.
71
- * A hex string. Min 32, max 40 chars. hash(TYPE#KEY)
72
- * @memberof Identity#
73
- * @type {String}
74
- */
75
- id: Joi.string().hex().min(32).max(40).required(),
76
-
77
- /**
78
- * User id.
79
- * @memberof Identity#
80
- * @type {String}
81
- */
82
- userId: Joi.string().regex(/^[a-z0-9][a-z0-9-]+[a-z0-9]$/).min(32).max(40).required(),
83
-
84
- /**
85
- * Identity type: social, email, username, phone number
86
- * @type {String}
87
- */
88
- type: Joi.valid(IDENTITY_TYPES).required(),
89
-
90
- /**
91
- * Identity name: social provider name(facebook, google, etc.), email, username, phone number
92
- * @type {String}
93
- */
94
- name: Joi.string().min(2).max(255).lowercase().required(),
95
-
96
- /**
97
- * Identity value: social provider id, password hash
98
- * @type {String}
99
- */
100
- value: Joi.string().min(2).max(255).required(),
101
-
102
- /**
103
- * Identity key: hash(facebook:profileId, username, email or phone number)
104
- * @type {[type]}
105
- */
106
- key: Joi.string().min(32).max(40).lowercase().required(),
107
-
108
- profile: profileSchema,
109
-
110
- /**
111
- * Created date. Unix time in milliseconds.
112
- * @memberof Identity#
113
- * @type {Number}
114
- */
115
- createdAt: Joi.number().integer().min(1).default(Date.now, 'time of creation'),
116
-
117
- /**
118
- * Updated date. Unix time in milliseconds.
119
- * @memberof Identity#
120
- * @type {Number}
121
- */
122
- updatedAt: Joi.number().integer().min(1),
123
-
124
- /**
125
- * Identity custom data. Max 1000 chars.
126
- * @memberof Identity#
127
- * @type {String}
128
- */
129
- metadata: Joi.string().trim().max(1000)
130
- };
131
-
132
- exports.update = {
133
- id: Joi.string().hex().min(32).max(40).required(),
134
- profile: profileSchema,
135
- updatedAt: Joi.number().integer().min(0).default(Date.now, 'time of updating').not(null),
136
- metadata: Joi.string().trim().max(1000)
137
- };
package/lib/index.js DELETED
@@ -1,51 +0,0 @@
1
- 'use strict';
2
-
3
- var createApp = require('./app');
4
- var createAdmin = require('./admin');
5
- var createApps = require('./apps');
6
- var Options = require('./options');
7
-
8
- /**
9
- * Creates a new Client object.
10
- * @constructs Client
11
- * @param {Storage} storage A Storage instance.
12
- * @param {ClientOptions} options Client options.
13
- * @return {Client} An instance of Client.
14
- */
15
- module.exports = function createClient(storage, options) {
16
-
17
- if (typeof storage !== 'object') {
18
- throw new Error('`storage` param is invalid');
19
- }
20
-
21
- options = Options.client(options);
22
-
23
- /**
24
- * Client object
25
- * @lends Client#
26
- */
27
- var client = {
28
- /**
29
- * An Admin object
30
- * @type {Admin}
31
- */
32
- admin: createAdmin(storage, options),
33
- /**
34
- * An Apps object
35
- * @type {Apps}
36
- */
37
- apps: createApps(storage, options),
38
- /**
39
- * Creates a new Api object.
40
- * @param {string} appId - Application id.
41
- * @param {ApiOptions} [apiOptions] - Api options.
42
- * @return {Api}
43
- */
44
- app: function(appId, apiOptions) {
45
- return createApp(storage, appId, apiOptions);
46
- }
47
- };
48
-
49
- return client;
50
-
51
- };
package/lib/login.js DELETED
@@ -1,61 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('./utils');
4
- var Promise = utils.Promise;
5
- var UserProfile = require('./user_profile');
6
- var debug = require('debug')('accounts');
7
-
8
- module.exports = function createLogin(users, identities) {
9
-
10
- function postSocialLogged(identity) {
11
- return Promise.all([
12
- users.update({
13
- id: identity.userId,
14
- lastLoginAt: Date.now()
15
- })
16
- ]).then(function() {
17
- return identity.userId;
18
- });
19
- }
20
-
21
- function socialLogin(profile) {
22
-
23
- var identity = UserProfile.toIdentity(profile);
24
-
25
- debug('Provider logining...', profile);
26
-
27
- return identities.getById(identity.id)
28
- .then(function(dbIdentity) {
29
- if (dbIdentity) {
30
- debug('found identity', dbIdentity.id);
31
- return postSocialLogged(dbIdentity);
32
- } else {
33
- debug('NEW identity', identity.type, identity.id);
34
- // create a new user
35
- return users.create(identity.profile)
36
- .then(function(dbUser) {
37
- identity.userId = dbUser.id;
38
- debug('created a new user', dbUser);
39
- debug('creating a new identity', identity.type, identity.id);
40
-
41
- return identities.create(identity)
42
- .then(function(dbIdentity2) {
43
- debug('created a new identity', dbIdentity2);
44
- return postSocialLogged(dbIdentity2);
45
- });
46
- });
47
- }
48
- });
49
- }
50
-
51
- function login(type, data, options) {
52
- switch (type) {
53
- case 'social':
54
- return socialLogin(data, options);
55
- }
56
-
57
- return Promise.reject(new Error('Login `type` is invalid'));
58
- }
59
-
60
- return login;
61
- };
package/lib/options.js DELETED
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('./utils');
4
- var _ = utils._;
5
- var randomString = utils.randomString;
6
-
7
- var CLIENT_OPTIONS = {
8
- secret: randomString(16)
9
- };
10
-
11
- var APP_OPTIONS = {
12
-
13
- };
14
-
15
- exports.client = function createClientOptions(options) {
16
- return _.defaultsDeep({}, options, CLIENT_OPTIONS);
17
- };
18
-
19
- exports.app = function createAppOptions(options) {
20
- return _.defaultsDeep({}, options, APP_OPTIONS);
21
- };
@@ -1,96 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('./utils');
4
- var Promise = utils.Promise;
5
-
6
- function validateMethodArgs(methods, method, args) {
7
- var info = methods[method];
8
- if (info.args > args.length) {
9
- throw new Error('Method `' + method + '` accepts minimum ' + info.args + ' argument(s)');
10
- }
11
- }
12
-
13
- function bindMethod(storageContainer, methods, method, hooks, options, appId) {
14
-
15
- function preHook(args) {
16
- var op = hooks.pre[method];
17
- var promise;
18
-
19
- if (op) {
20
- try {
21
- promise = op.apply(null, args);
22
- } catch (e) {
23
- return Promise.reject(e);
24
- }
25
- return Promise.resolve(promise)
26
- .then(function(result) {
27
- // update `data` param
28
- if (typeof result !== 'undefined') {
29
- args[appId ? 1 : 0] = result;
30
- }
31
- });
32
- }
33
- return Promise.resolve();
34
- }
35
-
36
- function postHook(args, result) {
37
- var op = hooks.post[method];
38
- var promise;
39
- if (op) {
40
- args.unshift(result);
41
- try {
42
- promise = op.apply(null, args);
43
- } catch (e) {
44
- return Promise.reject(e);
45
- }
46
- return Promise.resolve(promise)
47
- .then(function(postResult) {
48
- if (typeof postResult !== 'undefined') {
49
- return postResult;
50
- }
51
- return result;
52
- });
53
- }
54
-
55
- return result;
56
- }
57
-
58
- function methodBind() {
59
- // validating storage method
60
- if (!storageContainer[method]) {
61
- return Promise.reject(new Error('Method `' + method + '` not found on client'));
62
- }
63
-
64
- var args = Array.prototype.slice.call(arguments);
65
-
66
- try {
67
- validateMethodArgs(methods, method, args);
68
- } catch (e) {
69
- return Promise.reject(e);
70
- }
71
-
72
- // last hook argument is the `accounts` options
73
- args.push(options);
74
-
75
- if (appId) {
76
- // first hook argument is the app id
77
- args.unshift(appId);
78
- }
79
-
80
- return preHook(args)
81
- .then(function() {
82
- return storageContainer[method].apply(storageContainer, args)
83
- .then(function(result) {
84
- return postHook(args, result);
85
- });
86
- });
87
- }
88
-
89
- return methodBind;
90
- }
91
-
92
- module.exports = function storageBind(target, storageContainer, methods, hooks, options, appId) {
93
- Object.keys(methods).forEach(function(method) {
94
- target[method] = bindMethod(storageContainer, methods, method, hooks, options, appId);
95
- });
96
- };
@@ -1,44 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('./utils');
4
- var _ = utils._;
5
- var identityHelpers = require('./identities/helpers');
6
-
7
- exports.toProfile = function convertToProfile(profile) {
8
- var data = _.pick(profile, ['displayName', 'username', 'gender']);
9
-
10
- if (profile.name) {
11
- data.familyName = profile.name.familyName;
12
- data.givenName = profile.name.givenName;
13
- data.middleName = profile.name.middleName;
14
- }
15
-
16
- if (profile.emails && profile.emails.length > 0) {
17
- data.email = profile.emails[0].value;
18
- }
19
- if (profile.photos && profile.photos.length > 0) {
20
- data.photo = profile.photos[0].value;
21
- }
22
-
23
- if (profile.profileUrl) {
24
- data.url = profile.profileUrl;
25
- }
26
-
27
- data.displayName = data.displayName || data.username || data.givenName;
28
-
29
- return data;
30
- };
31
-
32
- exports.toIdentity = function convertToIdentity(profile) {
33
- var data = {};
34
-
35
- data.profile = exports.toProfile(profile);
36
-
37
- data.type = 'social';
38
- data.name = profile.provider;
39
- data.value = profile.id;
40
- data.key = identityHelpers.createKey(data.type, data.name, data.value);
41
- data.id = identityHelpers.createId(data.type, data.key);
42
-
43
- return data;
44
- };
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('../utils');
4
- var _ = utils._;
5
- var md5 = utils.md5;
6
- var randomString = utils.randomString;
7
- var atonic = utils.atonic;
8
- var uuid = utils.uuid;
9
-
10
- exports.createUsername = function createUsername(username) {
11
- username = username || randomString(8);
12
- username = username.trim().toLowerCase().replace(/[\s-]+/g, '-').replace(/-{2,}/, '-');
13
- return atonic.lowerCase(username);
14
- };
15
-
16
- exports.isValidUsername = function(username) {
17
- return _.isString(username) && username.trim().length > 2 && username.length <= 32;
18
- };
19
-
20
- exports.createUsernameKey = function createUsernameKey(appId, username) {
21
- return md5([appId.trim(), username.trim()].join('|').toLowerCase()).toLowerCase();
22
- };
23
-
24
- exports.newId = function newId() {
25
- return uuid().toLowerCase();
26
- };
@@ -1,30 +0,0 @@
1
- 'use strict';
2
-
3
- var debug = require('debug')('accounts');
4
- var validator = require('../validator');
5
-
6
- exports.pre = {
7
- create: function(appId, data) {
8
- debug('Creating a new user', appId, data);
9
-
10
- data = validator.create('users', data);
11
-
12
- return data;
13
- },
14
- update: function(appId, data) {
15
- debug('Updating a User...', data);
16
-
17
- data = validator.update('users', data);
18
-
19
- return data;
20
- }
21
- };
22
-
23
- exports.post = {
24
- create: function(result) {
25
- debug('Created user', result.username);
26
- },
27
- update: function(result) {
28
- debug('Updated a User...', result.id);
29
- }
30
- };
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- var storageBind = require('../storage_bind');
4
- var hooks = require('./hooks');
5
- var methods = require('./methods');
6
-
7
- /**
8
- * Creates a new Users object.
9
- * @constructs Users
10
- * @param {Storage} storage - A Storage instance.
11
- * @param {String} appId - An applicatin id
12
- * @param {AppOptions} [options] App options.
13
- * @return {Users}
14
- */
15
- module.exports = function createUsers(storage, appId, options) {
16
-
17
- /**
18
- * Users object
19
- * @lends Users
20
- */
21
- var client = {};
22
-
23
- storageBind(client, storage.users, methods, hooks, options, appId);
24
-
25
- return client;
26
- };
@@ -1,43 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * @lends Users
5
- */
6
- module.exports = {
7
- /**
8
- * Creates a new User
9
- * @func
10
- * @param {UserRecord} data - User object
11
- * @param {DataOptions} [options] - Data options
12
- * @return {Promise}
13
- * @instance
14
- */
15
- create: { args: 1 },
16
- /**
17
- * Update an User fields
18
- * @func
19
- * @param {UserRecord} data - User object
20
- * @param {DataOptions} [options] - Data options
21
- * @return {Promise}
22
- * @instance
23
- */
24
- update: { args: 1 },
25
- /**
26
- * Get an User by id
27
- * @func
28
- * @param {String} id - User id
29
- * @param {DataOptions} [options] - Data options
30
- * @return {Promise}
31
- * @instance
32
- */
33
- getById: { args: 1 },
34
- /**
35
- * Delete an User by id
36
- * @func
37
- * @param {String} id - User id
38
- * @param {DataOptions} [options] - Data options
39
- * @return {Promise}
40
- * @instance
41
- */
42
- deleteById: { args: 1 }
43
- };
@@ -1,112 +0,0 @@
1
- 'use strict';
2
-
3
- var Joi = require('joi');
4
- var helpers = require('./helpers');
5
-
6
- /**
7
- * User class
8
- * @class User
9
- */
10
- exports.create = {
11
- /**
12
- * User id.
13
- * @memberof User#
14
- * @type {String}
15
- */
16
- id: Joi.string().regex(/^[a-z0-9][a-z0-9-]+[a-z0-9]$/).min(32).max(40).default(helpers.newId, 'new user id'),
17
- /**
18
- * User email.
19
- * @memberof User#
20
- * @type {String}
21
- */
22
- email: Joi.string().trim().lowercase().email(),
23
- /**
24
- * User username.
25
- * @memberof User#
26
- * @type {String}
27
- */
28
- username: Joi.string().trim().min(3).max(50),
29
- /**
30
- * User display name.
31
- * @memberof User#
32
- * @type {String}
33
- */
34
- displayName: Joi.string().trim().min(3).max(100).required(),
35
- /**
36
- * User family name.
37
- * @memberof User#
38
- * @type {String}
39
- */
40
- familyName: Joi.string().trim().max(50),
41
- /**
42
- * User given name.
43
- * @memberof User#
44
- * @type {String}
45
- */
46
- givenName: Joi.string().trim().max(50),
47
- /**
48
- * User middle name.
49
- * @memberof User#
50
- * @type {String}
51
- */
52
- middleName: Joi.string().trim().max(50),
53
- /**
54
- * User gender: `male` or `female`.
55
- * @memberof User#
56
- * @type {String}
57
- */
58
- gender: Joi.valid('male', 'female'),
59
- /**
60
- * User status. Default: `active`.
61
- * @memberof User#
62
- * @type {String}
63
- */
64
- status: Joi.string().trim().max(50).default('active'),
65
- /**
66
- * User role. Default: `user`.
67
- * @memberof User#
68
- * @type {String}
69
- */
70
- role: Joi.string().trim().max(50).default('user'),
71
- /**
72
- * Created date. Unix time in milliseconds.
73
- * @memberof User#
74
- * @type {Number}
75
- */
76
- createdAt: Joi.number().integer().min(1).default(Date.now, 'time of creation'),
77
- /**
78
- * Updated date. Unix time in milliseconds.
79
- * @memberof User#
80
- * @type {Number}
81
- */
82
- updatedAt: Joi.number().integer().min(1),
83
- /**
84
- * Last login date. Unix time in milliseconds.
85
- * @memberof User#
86
- * @type {Number}
87
- */
88
- lastLoginAt: Joi.number().integer().min(1),
89
- /**
90
- * User custom data. Max 1000 chars.
91
- * @memberof User#
92
- * @type {String}
93
- */
94
- metadata: Joi.string().trim().max(1000)
95
- };
96
-
97
- exports.update = {
98
- id: Joi.string().regex(/^[a-z0-9][a-z0-9-]+[a-z0-9]$/).min(32).max(40).required(),
99
- email: Joi.string().trim().lowercase().email().not(null),
100
- username: Joi.string().trim().min(3).max(50).not(null),
101
- displayName: Joi.string().trim().min(3).max(100).not(null),
102
- familyName: Joi.string().trim().max(50),
103
- givenName: Joi.string().trim().max(50),
104
- middleName: Joi.string().trim().max(50),
105
- gender: Joi.valid('male', 'female'),
106
- status: Joi.string().trim().max(50).not(null),
107
- role: Joi.string().trim().max(50).not(null),
108
- photo: Joi.string().trim().max(255),
109
- updatedAt: Joi.number().integer().min(0).default(Date.now, 'time of updating'),
110
- lastLoginAt: Joi.number().integer().min(0).not(null),
111
- metadata: Joi.string().trim().max(1000)
112
- };
package/lib/utils.js DELETED
@@ -1,18 +0,0 @@
1
- 'use strict';
2
-
3
- var crypto = require('crypto');
4
-
5
- exports._ = require('lodash');
6
- exports.Promise = require('bluebird');
7
- exports.uuid = require('uuid').v4;
8
- exports.randomString = require('randomstring').generate;
9
-
10
- exports.md5 = function md5(value) {
11
- return crypto.createHash('md5').update(value).digest('hex').toLowerCase();
12
- };
13
-
14
- exports.sha1 = function sha1(value) {
15
- return crypto.createHash('sha1').update(value).digest('hex').toLowerCase();
16
- };
17
-
18
- exports.noop = function() {};
package/lib/validator.js DELETED
@@ -1,46 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('./utils');
4
- var _ = utils._;
5
- var Joi = require('joi');
6
-
7
- var config = {};
8
-
9
- ['apps', 'identities', 'users'].forEach(function(model) {
10
- var schemas = require('./' + model + '/schemas');
11
- config[model] = {
12
- create: schemas.create,
13
- createKeys: Object.keys(schemas.create),
14
- update: schemas.update,
15
- updateKeys: Object.keys(schemas.update)
16
- };
17
- });
18
-
19
- function validate(method, model, data) {
20
- // console.log(model, method);
21
- // console.log('validating:', data);
22
-
23
- var schema = config[model][method];
24
- var schemaKeys = config[model][method + 'Keys'];
25
-
26
- data = _.pick(data, schemaKeys);
27
-
28
- var valid = Joi.validate(data, schema, {
29
- convert: true,
30
- noDefaults: false,
31
- allowUnknown: false
32
- });
33
- if (valid.error) {
34
- throw valid.error;
35
- }
36
- // console.log(valid.value);
37
- return valid.value;
38
- }
39
-
40
- exports.create = function(model, data) {
41
- return validate('create', model, data);
42
- };
43
-
44
- exports.update = function(model, data) {
45
- return validate('update', model, data);
46
- };