forked from apostrophecms-legacy/apostrophe-signup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (78 loc) · 2.92 KB
/
index.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const _ = require('lodash');
module.exports = {
fields: [ 'firstName', 'lastName', 'username' ],
requiredFields: [ 'password' ],
hours: 48,
signupUrl: '/signup',
signupConfirmUrl: '/signup-confirm',
afterSignupUrl: '/',
afterConstruct: function(self, callback) {
self.pushAssets();
self.pushCreateSingleton();
self.addConfirmSecret();
self.addRoutes();
return self.ensureGroup(callback);
},
construct: function(self, options) {
require('./lib/routes.js')(self, options);
self.pushAssets = function() {
self.pushAsset('script', 'user', { when: 'user' });
self.pushAsset('stylesheet', 'user', { when: 'user' });
};
self.addConfirmSecret = function() {
self.apos.users.addSecret('signupConfirm');
};
// Ensure the existence of an apostrophe-group for newly
// created users, as configured via the `group` subproperty
// of the `create` option.
self.ensureGroup = function(callback) {
if (!(self.options.group)) {
return setImmediate(callback);
}
return self.apos.users.ensureGroup(self.options.group, function(err, group) {
self.group = group;
return callback(err);
});
};
// Invoked just before a newly signed-up user is inserted into
// the database. The user IS NOT confirmed yet. Override
// as you see fit
self.beforeInsert = function(req, user, callback) {
return callback(null);
};
// Invoked just after a newly signed-up user is inserted into
// the database. The user IS NOT confirmed yet. Override
// as you see fit
self.afterInsert = function(req, user, callback) {
return callback(null);
};
// Invoked just after a newly confirmed user is logged in, and
// just before they are redirected. Override as you see fit
self.afterConfirm = function(req, user, callback) {
return callback(null);
};
// Redirects the user to the URL indicated by the
// `afterSignupUrl` option. Override to customize
// this behavior further. You must end the response
// in some way (available in `req.res`)
self.redirectAfterSignup = function(req, user) {
return req.res.redirect(self.options.afterSignupUrl);
};
self.getSchema = function() {
const requiredFields = self.options.requiredFields;
let subset = self.apos.schemas.subset(self.apos.users.schema, self.options.fields.concat([ 'email', 'password' ]));
for (let i = 0; i < requiredFields.length; i++) {
let requiredFieldAt = _.findIndex(subset, { name: requiredFields[i] });
if (requiredFieldAt !== -1) {
let field = _.clone(subset[requiredFieldAt]);
field.required = true;
subset.splice(requiredFieldAt, 1, field);
}
}
return subset;
};
self.getSignupConfirmLifetimeInMilliseconds = function() {
return 1000 * 60 * 60 * (self.options.hours || 48);
};
}
};