113 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-01-12 15:20:13 +01:00
'use strict';
/**
* Module dependencies.
*/
// 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
2021-04-29 13:51:12 +02:00
const { getAbsoluteServerUrl } = require('@strapi/utils');
2018-01-12 15:20:13 +01:00
2021-08-20 15:23:02 +02:00
module.exports = ({ strapi }) => {
2021-09-03 11:11:37 +02:00
// lazy load heavy dependencies
const providerRequest = require('./providers-list');
2021-09-03 11:11:37 +02:00
2021-08-20 15:23:02 +02:00
/**
* Helper to get profiles
*
* @param {String} provider
*/
2022-02-23 22:51:59 +08:00
const getProfile = async (provider, query) => {
2021-08-20 15:23:02 +02:00
const access_token = query.access_token || query.code || query.oauth_token;
const providers = await strapi
.store({ type: 'plugin', name: 'users-permissions', key: 'grant' })
2021-08-20 15:23:02 +02:00
.get();
2022-02-23 22:51:59 +08:00
return providerRequest({ provider, query, access_token, providers });
2021-08-20 15:23:02 +02:00
};
/**
* Connect thanks to a third-party provider.
*
*
* @param {String} provider
* @param {String} access_token
*
* @return {*}
*/
2022-05-31 14:06:58 +02:00
const connect = async (provider, query) => {
2021-08-20 15:23:02 +02:00
const access_token = query.access_token || query.code || query.oauth_token;
2022-05-31 14:06:58 +02:00
if (!access_token) {
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.');
}
const users = await strapi.query('plugin::users-permissions.user').findMany({
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;
}
if (users.length > 1 && advancedSettings.unique_email) {
throw new Error('Email is already taken.');
}
// Retrieve default role.
const defaultRole = await strapi
.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,
};
const createdUser = await strapi
.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');
2021-11-29 10:41:26 +01:00
return urlJoin(getAbsoluteServerUrl(strapi.config), apiPrefix, 'connect', provider, 'callback');
2021-10-26 16:51:29 +02:00
};
2021-08-20 15:23:02 +02:00
return {
connect,
buildRedirectUri,
};
};