172 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-11-17 11:17:20 +01:00
'use strict';
/**
* An asynchronous bootstrap function that runs before
* your application gets started.
*
* This gives you an opportunity to set up your data model,
* run jobs, or perform some special logic.
*/
const _ = require('lodash');
2017-12-04 16:43:24 +01:00
const uuid = require('uuid/v4');
2019-08-12 15:35:40 +02:00
module.exports = async () => {
2017-12-04 16:43:24 +01:00
if (!_.get(strapi.plugins['users-permissions'], 'config.jwtSecret')) {
const jwtSecret = uuid();
_.set(strapi.plugins['users-permissions'], 'config.jwtSecret', jwtSecret);
2017-12-12 15:13:27 +01:00
strapi.reload.isWatching = false;
2019-08-12 15:35:40 +02:00
await strapi.fs.writePluginFile(
'users-permissions',
'config/jwt.json',
JSON.stringify({ jwtSecret }, null, 2)
);
strapi.reload.isWatching = true;
2017-12-04 16:43:24 +01:00
}
const pluginStore = strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
});
2018-04-24 22:18:21 +08:00
const grantConfig = {
email: {
enabled: true,
icon: 'envelope',
},
discord: {
enabled: false,
2018-07-31 16:17:41 +02:00
icon: 'comments',
key: '',
secret: '',
callback: '/auth/discord/callback',
scope: ['identify', 'email'],
},
facebook: {
enabled: false,
icon: 'facebook-official',
key: '',
secret: '',
callback: '/auth/facebook/callback',
scope: ['email'],
},
google: {
enabled: false,
icon: 'google',
key: '',
secret: '',
callback: '/auth/google/callback',
scope: ['email'],
},
github: {
enabled: false,
icon: 'github',
key: '',
secret: '',
redirect_uri: '/auth/github/callback',
scope: ['user', 'user:email'],
},
microsoft: {
enabled: false,
icon: 'windows',
key: '',
secret: '',
callback: '/auth/microsoft/callback',
scope: ['user.read'],
},
twitter: {
enabled: false,
icon: 'twitter',
key: '',
2018-08-23 18:28:13 +02:00
secret: '',
callback: '/auth/twitter/callback',
},
2019-09-06 09:14:42 +02:00
instagram: {
enabled: false,
icon: 'instagram',
key: '',
secret: '',
callback: '/auth/instagram/callback',
},
};
const prevGrantConfig = (await pluginStore.get({ key: 'grant' })) || {};
// store grant auth config to db
// when plugin_users-permissions_grant is not existed in db
// or we have added/deleted provider here.
if (
!prevGrantConfig ||
!_.isEqual(_.keys(prevGrantConfig), _.keys(grantConfig))
) {
2018-04-24 22:18:21 +08:00
// merge with the previous provider config.
_.keys(grantConfig).forEach(key => {
2018-04-24 22:18:21 +08:00
if (key in prevGrantConfig) {
grantConfig[key] = _.merge(grantConfig[key], prevGrantConfig[key]);
}
});
await pluginStore.set({ key: 'grant', value: grantConfig });
2018-01-12 15:20:13 +01:00
}
if (!(await pluginStore.get({ key: 'email' }))) {
const value = {
reset_password: {
display: 'Email.template.reset_password',
icon: 'refresh',
options: {
from: {
name: 'Administration Panel',
email: 'no-reply@strapi.io',
},
response_email: '',
2018-04-16 09:48:43 +02:00
object: '­Reset password',
message: `<p>We heard that you lost your password. Sorry about that!</p>
<p>But dont worry! You can use the following link to reset your password:</p>
<p><%= URL %>?code=<%= TOKEN %></p>
<p>Thanks.</p>`,
},
2018-08-08 17:57:02 +02:00
},
email_confirmation: {
2018-08-08 17:57:02 +02:00
display: 'Email.template.email_confirmation',
icon: 'check-square-o',
options: {
from: {
name: 'Administration Panel',
email: 'no-reply@strapi.io',
2018-08-08 17:57:02 +02:00
},
response_email: '',
object: 'Account confirmation',
2018-08-27 08:11:23 -07:00
message: `<p>Thank you for registering!</p>
2018-08-08 17:57:02 +02:00
2018-08-23 12:00:46 +02:00
<p>You have to confirm your email address. Please click on the link below.</p>
2018-08-08 17:57:02 +02:00
<p><%= URL %>?confirmation=<%= CODE %></p>
<p>Thanks.</p>`,
},
},
};
2018-01-15 11:29:38 +01:00
await pluginStore.set({ key: 'email', value });
2018-01-15 11:29:38 +01:00
}
if (!(await pluginStore.get({ key: 'advanced' }))) {
const value = {
unique_email: true,
2018-03-12 15:56:25 +01:00
allow_register: true,
2018-08-08 17:57:02 +02:00
email_confirmation: false,
2019-08-12 15:35:40 +02:00
email_confirmation_redirection: `http://${strapi.config.currentEnvironment.server.host}:${strapi.config.currentEnvironment.server.port}/admin`,
2019-10-09 17:37:16 +02:00
email_reset_password: `http://${strapi.config.currentEnvironment.server.host}:${strapi.config.currentEnvironment.server.port}/admin`,
default_role: 'authenticated',
};
2018-01-15 15:01:21 +01:00
await pluginStore.set({ key: 'advanced', value });
2018-01-15 15:01:21 +01:00
}
2018-08-08 17:57:02 +02:00
2019-08-12 15:35:40 +02:00
return strapi.plugins[
'users-permissions'
].services.userspermissions.initialize();
2017-11-17 11:17:20 +01:00
};