UNPKG

8.06 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 updateMethods: ['POST', 'PUT', 'PATCH'],
11
12 queryDefaultLimit: 25,
13 queryMaxLimit: 300,
14 // map reponseType response types
15 responseTypes: ['html', 'json'],
16 // send nested models in response
17 sendNestedModels: true,
18 // default app permissions
19 permissions: require('./lib/acl/corePermissions.json'),
20
21 port: process.env.PORT || '3000',
22 hostname: 'http://localhost:' + ( process.env.PORT || '3000' ),
23 // default favicon, change in your project config/local.js
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 // body parser settings to use in bodyParser.json()
47 bodyParser: { limit: 20000000 },
48 // auth settings
49 auth : {
50 requireAccountActivation: true,
51 allowUserSignup: true
52 },
53 acl : { disabled: true },
54 passport: {
55 // session is required for local strategy
56 enableSession: true,
57
58 accessTokenTime: 300000000,
59 cookieDomain: 'localhost:' + ( process.env.PORT || '3000' ),
60 cookieName: 'weoauth',
61 cookieSecure: false,
62 expiresTime: 900000, // time to expires token and session
63
64 strategies: {
65 // session
66 local: {
67 Strategy: require('passport-local').Strategy,
68 // url to image icon
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 // build the find user query
78 var query = { where: {} };
79 query.where[we.config.passport.strategies.local.usernameField] = email;
80 // find user in DB
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 // get the user password
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 // see https://github.com/andris9/nodemailer-smtp-transport for config options
106 email: {
107 // default mail options
108 mailOptions: {
109 // by default log emails in console
110 sendToConsole: true,
111 // default from and to
112 from: 'We.js project <contato@wejs.org>', // sender address
113 subject: 'A We.js project email', // Subject line
114 },
115 // connection configs
116 port: 25,
117 auth: {
118 user: '',
119 pass: ''
120 },
121 debug: true,
122 ignoreTLS: false,
123 name: null,
124 // optional params
125 // host: 'localhost',
126 // secure: 'true',
127 // localAddress: '',
128 // connectionTimeout: '',
129 // greetingTimeout: '',
130 // socketTimeout: '',
131
132 // authMethod: '',
133 // tls: ''
134 },
135 // external services API keys
136 apiKeys: {},
137 // node-i18n configs
138 i18n: {
139 // setup some locales - other locales default to en silently
140 locales:[],
141 // you may alter a site wide default locale
142 defaultLocale: 'en-us',
143 // sets a custom cookie name to parse locale settings from - defaults to NULL
144 cookie: 'weLocale',
145 // where to store json files - defaults to './locales' relative to modules directory
146 directory: projectPath + '/config/locales',
147 // whether to write new locale information to disk - defaults to true
148 updateFiles: false,
149 // what to use as the indentation unit - defaults to "\t"
150 indent: '\t',
151 // setting extension of json files - defaults to '.json'
152 // (you might want to set this to '.js' according to webtranslateit)
153 extension: '.json',
154 // setting prefix of json files name - default to none ''
155 // (in case you use different locale files naming scheme
156 // (webapp-en.json), rather then just en.json)
157 prefix: '',
158 // enable object notation
159 objectNotation: false
160 },
161 clientside: {
162 // client side logs
163 log: {},
164 // publivars
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 // // theme configs
176 themes: {
177 // list of all enabled themes how will be load in bootstrap
178 enabled: [],
179 // default app theme
180 app: null,
181 // default admin theme
182 admin: null
183 },
184 clientComponentTemplates: { 'components-core': true },
185 database: { resetAllData: false },
186 // services register
187 // { url: '', oauthCallback: '', name: ''}
188 services: {},
189
190 date: { defaultFormat: 'L LT' },
191 // cache configs
192 cache: {
193 // resource cache, Last-Modified cache
194 resourceCacheActions: 'findOne',
195 skipResourceCache: false,
196 //Cache-Control: public, max-age=[maxage]
197 maxage: 86400000 // one day
198 },
199 security: {
200 // see https://github.com/expressjs/cors#configuration-options for configuration options
201 // This may be override by every route configs
202 CORS: {
203 // block all CORS requests by default
204 origin: function(origin, cb){ cb(null, false) },
205 // default methods
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 // user pre-loader
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 * Convert body data fields to database data tipo
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};