forked from vegeta999/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserHandler.js
More file actions
79 lines (68 loc) · 2.68 KB
/
Copy pathuserHandler.js
File metadata and controls
79 lines (68 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { ServiceConfiguration } from 'meteor/service-configuration';
import { logger } from './logger';
import { generateUsernameSuggestion } from '../../lib';
// Updates or creates a user after we authenticate with Blockstack
// Clones Accounts.updateOrCreateUserFromExternalService with some modifications
export const updateOrCreateUser = (serviceData, options) => {
const serviceConfig = ServiceConfiguration.configurations.findOne({ service: 'blockstack' });
logger.debug('Auth config', serviceConfig);
// Extract user data from service / token
const { id, did } = serviceData;
const { profile } = options;
// Look for existing Blockstack user
const user = Meteor.users.findOne({ 'services.blockstack.id': id });
let userId;
let isNew = false;
// Use found or create a user
if (user) {
logger.info(`User login with Blockstack ID ${ id }`);
userId = user._id;
} else {
isNew = true;
let emails = [];
if (!Array.isArray(profile.emails)) {
// Fix absense of emails by adding placeholder address using decentralised
// ID at blockstack.email - a holding domain only, no MX record, does not
// process email, may be used in future to provide decentralised email via
// gaia, encrypting mail for DID user only. @TODO: document this approach.
emails.push({ address: `${ did }@blockstack.email`, verified: false });
} else {
// Reformat array of emails into expected format if they exist
emails = profile.emails.map((address) => ({ address, verified: true }));
}
const newUser = {
name: profile.name,
active: true,
emails,
services: { blockstack: serviceData },
};
// Set username same as in blockstack, or suggest if none
if (profile.name) {
newUser.name = profile.name;
}
// Take profile username if exists, or generate one if enabled
if (profile.username && profile.username !== '') {
newUser.username = profile.username;
} else if (serviceConfig.generateUsername === true) {
newUser.username = generateUsernameSuggestion(newUser);
}
// If no username at this point it will suggest one from the name
// Create and get created user to make a couple more mods before returning
logger.info(`Creating user for Blockstack ID ${ id }`);
userId = Accounts.insertUserDoc({}, newUser);
logger.debug('New user ${ userId }', newUser);
}
// Add login token for blockstack auth session (take expiration from response)
// TODO: Regquired method result format ignores `.when`
const { token } = Accounts._generateStampedLoginToken();
const tokenExpires = serviceData.expiresAt;
return {
type: 'blockstack',
userId,
token,
tokenExpires,
isNew,
};
};