1 |
|
2 |
|
3 |
|
4 | var moment = require('moment');
|
5 |
|
6 | module.exports = function loadPlugin(projectPath, Plugin) {
|
7 | var plugin = new Plugin(__dirname);
|
8 |
|
9 | plugin.setConfigs({
|
10 | updateMethods: ['POST', 'PUT', 'PATCH'],
|
11 |
|
12 | queryDefaultLimit: 25,
|
13 | queryMaxLimit: 300,
|
14 |
|
15 | responseTypes: ['html', 'json'],
|
16 |
|
17 | sendNestedModels: true,
|
18 |
|
19 | permissions: require('./lib/acl/corePermissions.json'),
|
20 |
|
21 | port: process.env.PORT || '3000',
|
22 | hostname: 'http://localhost:' + ( process.env.PORT || '3000' ),
|
23 |
|
24 | favicon: __dirname + '/files/public/core-favicon.ico',
|
25 |
|
26 | appName: 'We.js app',
|
27 | appLogo: '/public/plugin/we-core/files/images/logo-small.png',
|
28 |
|
29 | defaultUserAvatar: projectPath + '/node_modules/we-core/files/public/images/avatars/user-avatar.png',
|
30 |
|
31 | log: { level: 'debug' },
|
32 |
|
33 | session: {
|
34 | secret: 'setASecreteKeyInYourAppConfig',
|
35 | resave: false,
|
36 | saveUninitialized: true,
|
37 | name: 'wejs.sid',
|
38 | rolling: false,
|
39 | cookie: {
|
40 | path: '/',
|
41 | httpOnly: true,
|
42 | secure: false,
|
43 | maxAge: null
|
44 | }
|
45 | },
|
46 |
|
47 | bodyParser: { limit: 20000000 },
|
48 |
|
49 | auth : {
|
50 | requireAccountActivation: true,
|
51 | allowUserSignup: true
|
52 | },
|
53 | acl : { disabled: true },
|
54 | passport: {
|
55 |
|
56 | enableSession: true,
|
57 |
|
58 | accessTokenTime: 300000000,
|
59 | cookieDomain: 'localhost:' + ( process.env.PORT || '3000' ),
|
60 | cookieName: 'weoauth',
|
61 | cookieSecure: false,
|
62 | expiresTime: 900000,
|
63 |
|
64 | strategies: {
|
65 |
|
66 | local: {
|
67 | Strategy: require('passport-local').Strategy,
|
68 |
|
69 | icon: '/public/plugin/we-core/files/images/login.png',
|
70 | authUrl: '/login',
|
71 |
|
72 | usernameField: 'email',
|
73 | passwordField: 'password',
|
74 | session: true,
|
75 | findUser: function findUserAndValidPassword(email, password, done) {
|
76 | var we = this.we;
|
77 |
|
78 | var query = { where: {} };
|
79 | query.where[we.config.passport.strategies.local.usernameField] = email;
|
80 |
|
81 | we.db.models.user.find(query).then (function (user) {
|
82 | if (!user) {
|
83 | return done(null, false, { message: 'auth.login.wrong.email.or.password' });
|
84 | }
|
85 |
|
86 | user.getPassword().then(function (passwordObj) {
|
87 | if (!passwordObj)
|
88 | return done(null, false, { message: 'auth.login.user.dont.have.password' });
|
89 |
|
90 | passwordObj.validatePassword(password, function (err, isValid) {
|
91 | if (err) return done(err);
|
92 | if (!isValid) {
|
93 | return done(null, false, { message: 'auth.login.user.incorrect.password.or.email' });
|
94 | } else {
|
95 | return done(null, user);
|
96 | }
|
97 | });
|
98 | })
|
99 | });
|
100 | }
|
101 | }
|
102 | }
|
103 | },
|
104 |
|
105 |
|
106 | email: {
|
107 |
|
108 | mailOptions: {
|
109 |
|
110 | sendToConsole: true,
|
111 |
|
112 | from: 'We.js project <contato@wejs.org>',
|
113 | subject: 'A We.js project email',
|
114 | },
|
115 |
|
116 | port: 25,
|
117 | auth: {
|
118 | user: '',
|
119 | pass: ''
|
120 | },
|
121 | debug: true,
|
122 | ignoreTLS: false,
|
123 | name: null,
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | },
|
135 |
|
136 | apiKeys: {},
|
137 |
|
138 | i18n: {
|
139 |
|
140 | locales:[],
|
141 |
|
142 | defaultLocale: 'en-us',
|
143 |
|
144 | cookie: 'weLocale',
|
145 |
|
146 | directory: projectPath + '/config/locales',
|
147 |
|
148 | updateFiles: false,
|
149 |
|
150 | indent: '\t',
|
151 |
|
152 |
|
153 | extension: '.json',
|
154 |
|
155 |
|
156 |
|
157 | prefix: '',
|
158 |
|
159 | objectNotation: false
|
160 | },
|
161 | clientside: {
|
162 |
|
163 | log: {},
|
164 |
|
165 | publicVars: {}
|
166 | },
|
167 | metadata: {},
|
168 | forms: {
|
169 | 'login': __dirname + '/server/forms/login.json',
|
170 | 'register': __dirname + '/server/forms/register.json',
|
171 | 'forgot-password': __dirname + '/server/forms/forgot-password.json',
|
172 | 'new-password': __dirname + '/server/forms/new-password.json',
|
173 | 'change-password': __dirname + '/server/forms/change-password.json'
|
174 | },
|
175 |
|
176 | themes: {
|
177 |
|
178 | enabled: [],
|
179 |
|
180 | app: null,
|
181 |
|
182 | admin: null
|
183 | },
|
184 | clientComponentTemplates: { 'components-core': true },
|
185 | database: { resetAllData: false },
|
186 |
|
187 |
|
188 | services: {},
|
189 |
|
190 | date: { defaultFormat: 'L LT' },
|
191 |
|
192 | cache: {
|
193 |
|
194 | resourceCacheActions: 'findOne',
|
195 | skipResourceCache: false,
|
196 |
|
197 | maxage: 86400000
|
198 | },
|
199 | security: {
|
200 |
|
201 |
|
202 | CORS: {
|
203 |
|
204 | origin: function(origin, cb){ cb(null, false) },
|
205 |
|
206 | methods: ['GET', 'OPTIONS'],
|
207 | allowedHeaders: ['Content-Type', 'Authorization', 'Accept']
|
208 | }
|
209 | }
|
210 | });
|
211 |
|
212 | plugin.setLayouts({
|
213 | default: __dirname + '/server/templates/default-layout.hbs',
|
214 | 'user/layout': __dirname + '/server/templates/user/layout.hbs'
|
215 | });
|
216 |
|
217 | plugin.assets.addCoreAssetsFiles(plugin);
|
218 |
|
219 | plugin.events.on('we:express:set:params', function(data) {
|
220 |
|
221 | data.express.param('userId', function (req, res, next, id) {
|
222 | if (!/^\d+$/.exec(String(id))) return res.notFound();
|
223 | data.we.db.models.user.findById(id).then(function (user) {
|
224 | if (!user) return res.notFound();
|
225 | res.locals.user = user;
|
226 | next();
|
227 | });
|
228 | })
|
229 | })
|
230 |
|
231 | |
232 |
|
233 |
|
234 | plugin.hooks.on('we:router:request:after:load:context', function (data, next) {
|
235 | var we = data.req.getWe();
|
236 | var res = data.res;
|
237 | var req = data.req;
|
238 |
|
239 | if (data.req.method !== 'POST') return next();
|
240 | if (!res.locale) return next();
|
241 |
|
242 | if (res.locals.Model && req.body) {
|
243 | res.locals.Model._dateAttributes.forEach(function (d) {
|
244 | if (req.body[d]) {
|
245 | req.body[d] = moment(req.body[d], we.config.date.defaultFormat).locale('en').format('L LT');
|
246 | }
|
247 | });
|
248 | }
|
249 | next();
|
250 | });
|
251 |
|
252 | return plugin;
|
253 | };
|