2018-01-12 15:20:13 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
2022-08-09 12:03:51 +02:00
|
|
|
* Module dependencies
|
2018-01-12 15:20:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
2021-11-29 10:41:26 +01:00
|
|
|
const urlJoin = require('url-join');
|
2018-01-12 15:20:13 +01:00
|
|
|
|
2022-06-01 16:22:19 +02:00
|
|
|
const { getService } = require('../utils');
|
2018-01-12 15:20:13 +01:00
|
|
|
|
2021-08-20 15:23:02 +02:00
|
|
|
module.exports = ({ strapi }) => {
|
|
|
|
/**
|
|
|
|
* Helper to get profiles
|
|
|
|
*
|
|
|
|
* @param {String} provider
|
|
|
|
*/
|
|
|
|
|
2022-02-23 22:51:59 +08:00
|
|
|
const getProfile = async (provider, query) => {
|
2022-08-08 23:33:39 +02:00
|
|
|
const accessToken = query.access_token || query.code || query.oauth_token;
|
2021-08-20 15:23:02 +02:00
|
|
|
|
2021-11-29 16:05:45 +01:00
|
|
|
const providers = await strapi
|
2021-09-13 12:03:12 +02:00
|
|
|
.store({ type: 'plugin', name: 'users-permissions', key: 'grant' })
|
2021-08-20 15:23:02 +02:00
|
|
|
.get();
|
|
|
|
|
2022-06-01 16:22:19 +02:00
|
|
|
return getService('providers-registry').run({
|
|
|
|
provider,
|
|
|
|
query,
|
2022-08-08 23:33:39 +02:00
|
|
|
accessToken,
|
2022-06-01 16:22:19 +02:00
|
|
|
providers,
|
|
|
|
});
|
2021-08-20 15:23:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect thanks to a third-party provider.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {String} provider
|
2022-08-08 23:33:39 +02:00
|
|
|
* @param {String} accessToken
|
2021-08-20 15:23:02 +02:00
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
|
2022-05-31 14:06:58 +02:00
|
|
|
const connect = async (provider, query) => {
|
2022-08-08 23:33:39 +02:00
|
|
|
const accessToken = query.access_token || query.code || query.oauth_token;
|
2021-08-20 15:23:02 +02:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
if (!accessToken) {
|
2022-05-31 14:06:58 +02:00
|
|
|
throw new Error('No access_token.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the profile.
|
|
|
|
const profile = await getProfile(provider, query);
|
|
|
|
|
|
|
|
const email = _.toLower(profile.email);
|
|
|
|
|
|
|
|
// We need at least the mail.
|
|
|
|
if (!email) {
|
|
|
|
throw new Error('Email was not available.');
|
|
|
|
}
|
|
|
|
|
2024-03-13 15:40:30 +01:00
|
|
|
const users = await strapi.db.query('plugin::users-permissions.user').findMany({
|
2022-05-31 14:06:58 +02:00
|
|
|
where: { email },
|
2021-08-20 15:23:02 +02:00
|
|
|
});
|
2022-05-31 14:06:58 +02:00
|
|
|
|
|
|
|
const advancedSettings = await strapi
|
|
|
|
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
|
|
|
|
.get();
|
|
|
|
|
|
|
|
const user = _.find(users, { provider });
|
|
|
|
|
|
|
|
if (_.isEmpty(user) && !advancedSettings.allow_register) {
|
|
|
|
throw new Error('Register action is actually not available.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isEmpty(user)) {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2022-11-18 11:59:11 +08:00
|
|
|
if (users.length && advancedSettings.unique_email) {
|
2022-05-31 14:06:58 +02:00
|
|
|
throw new Error('Email is already taken.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve default role.
|
2024-03-13 15:40:30 +01:00
|
|
|
const defaultRole = await strapi.db
|
2022-05-31 14:06:58 +02:00
|
|
|
.query('plugin::users-permissions.role')
|
|
|
|
.findOne({ where: { type: advancedSettings.default_role } });
|
|
|
|
|
|
|
|
// Create the new user.
|
|
|
|
const newUser = {
|
|
|
|
...profile,
|
|
|
|
email, // overwrite with lowercased email
|
|
|
|
provider,
|
|
|
|
role: defaultRole.id,
|
|
|
|
confirmed: true,
|
|
|
|
};
|
|
|
|
|
2024-03-13 15:40:30 +01:00
|
|
|
const createdUser = await strapi.db
|
2022-05-31 14:06:58 +02:00
|
|
|
.query('plugin::users-permissions.user')
|
|
|
|
.create({ data: newUser });
|
|
|
|
|
|
|
|
return createdUser;
|
2021-08-20 15:23:02 +02:00
|
|
|
};
|
|
|
|
|
2021-10-26 16:51:29 +02:00
|
|
|
const buildRedirectUri = (provider = '') => {
|
|
|
|
const apiPrefix = strapi.config.get('api.rest.prefix');
|
2024-01-19 10:09:53 +01:00
|
|
|
return urlJoin(
|
|
|
|
strapi.config.get('server.absoluteUrl'),
|
|
|
|
apiPrefix,
|
|
|
|
'connect',
|
|
|
|
provider,
|
|
|
|
'callback'
|
|
|
|
);
|
2021-10-26 16:51:29 +02:00
|
|
|
};
|
2021-08-20 15:23:02 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
connect,
|
|
|
|
buildRedirectUri,
|
|
|
|
};
|
|
|
|
};
|