UNPKG

8.5 kBJavaScriptView Raw
1/**
2 * We.js plugin config
3 */
4var moment = require('moment');
5
6module.exports = function loadPlugin(projectPath, Plugin) {
7 var plugin = new Plugin(__dirname);
8 // set plugin configs
9 plugin.setConfigs({
10 // // flag to skip project and plugin install methods
11 // skipInstall: false,
12
13 // enable suport to parse req.query.where to sequelize query
14 enableQueryWhere: false,
15 // update route methods
16 updateMethods: ['POST', 'PUT', 'PATCH'],
17 // default find limit
18 queryDefaultLimit: 25,
19 queryMaxLimit: 300,
20 // map reponseType response types
21 responseTypes: ['html', 'json'],
22 // send nested models in response
23 sendNestedModels: true,
24 // default app permissions
25 permissions: require('./lib/acl/corePermissions.json'),
26
27 port: process.env.PORT || '3000',
28 hostname: 'http://localhost:' + ( process.env.PORT || '3000' ),
29 // default favicon, change in your project config/local.js
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 // we.js url alias feature
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 // body parser settings to use in bodyParser.json()
55 bodyParser: { limit: 20000000 },
56 // auth settings
57 auth : {
58 requireAccountActivation: true,
59 allowUserSignup: true
60 },
61 acl : { disabled: true },
62 passport: {
63 // session is required for local strategy
64 enableSession: true,
65
66 accessTokenTime: 300000000,
67 cookieDomain: 'localhost:' + ( process.env.PORT || '3000' ),
68 cookieName: 'weoauth',
69 cookieSecure: false,
70 expiresTime: 900000, // time to expires token and session
71
72 strategies: {
73 // session
74 local: {
75 Strategy: require('passport-local').Strategy,
76 // url to image icon
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 // build the find user query
86 var query = { where: {} };
87 query.where[we.config.passport.strategies.local.usernameField] = email;
88 // find user in DB
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 // get the user password
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 // see https://github.com/andris9/nodemailer-smtp-transport for config options
114 email: {
115 // default mail options
116 mailOptions: {
117 // by default log emails in console
118 sendToConsole: true,
119 // default from and to
120 from: 'We.js project <contato@wejs.org>', // sender address
121 subject: 'A We.js project email', // Subject line
122 },
123 // connection configs
124 port: 25,
125 auth: {
126 user: '',
127 pass: ''
128 },
129 debug: true,
130 ignoreTLS: false,
131 name: null,
132 // optional params
133 // host: 'localhost',
134 // secure: 'true',
135 // localAddress: '',
136 // connectionTimeout: '',
137 // greetingTimeout: '',
138 // socketTimeout: '',
139
140 // authMethod: '',
141 // tls: ''
142 },
143 // external services API keys
144 apiKeys: {},
145 // node-i18n configs
146 i18n: {
147 // setup some locales - other locales default to en silently
148 locales:[],
149 // you may alter a site wide default locale
150 defaultLocale: 'en-us',
151 // sets a custom cookie name to parse locale settings from - defaults to NULL
152 cookie: 'weLocale',
153 // where to store json files - defaults to './locales' relative to modules directory
154 directory: projectPath + '/config/locales',
155 // whether to write new locale information to disk - defaults to true
156 updateFiles: false,
157 // what to use as the indentation unit - defaults to "\t"
158 indent: '\t',
159 // setting extension of json files - defaults to '.json'
160 // (you might want to set this to '.js' according to webtranslateit)
161 extension: '.json',
162 // setting prefix of json files name - default to none ''
163 // (in case you use different locale files naming scheme
164 // (webapp-en.json), rather then just en.json)
165 prefix: '',
166 // enable object notation
167 objectNotation: false
168 },
169 clientside: {
170 // client side logs
171 log: {},
172 // publivars
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 // // theme configs
184 themes: {
185 // list of all enabled themes how will be load in bootstrap
186 enabled: [],
187 // default app theme
188 app: null,
189 // default admin theme
190 admin: null
191 },
192 clientComponentTemplates: { 'components-core': true },
193 database: { resetAllData: false },
194 // services register
195 // { url: '', oauthCallback: '', name: ''}
196 services: {},
197
198 date: { defaultFormat: 'L LT' },
199 // cache configs
200 cache: {
201 // resource cache, Last-Modified cache
202 resourceCacheActions: 'findOne',
203 skipResourceCache: false,
204 //Cache-Control: public, max-age=[maxage]
205 maxage: 86400000 // one day
206 },
207 security: {
208 // see https://github.com/expressjs/cors#configuration-options for configuration options
209 // This may be override by every route configs
210 CORS: {
211 // block all CORS requests by default
212 origin: function(origin, cb){ cb(null, false) },
213 // default methods
214 methods: ['GET', 'OPTIONS'],
215 allowedHeaders: ['Content-Type', 'Authorization', 'Accept']
216 }
217 },
218 router: {
219 alias: {
220 // dont load alias for this routes
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 // user pre-loader
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 * Convert body data fields to database data tipo
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};