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