adds secondary auth collection Customers, modifies GraphQL policies to suit multiple auth collections
This commit is contained in:
@@ -1,13 +1,29 @@
|
||||
const { policies } = require('../../operations');
|
||||
const formatName = require('../../../graphql/utilities/formatName');
|
||||
|
||||
const policyResolver = (config, collection) => async (_, __, context) => {
|
||||
const formatConfigNames = (results, configs) => {
|
||||
const formattedResults = { ...results };
|
||||
|
||||
configs.forEach(({ slug }) => {
|
||||
const result = { ...(formattedResults[slug] || {}) };
|
||||
delete formattedResults[slug];
|
||||
formattedResults[formatName(slug)] = result;
|
||||
});
|
||||
|
||||
return formattedResults;
|
||||
};
|
||||
|
||||
const policyResolver = config => async (_, __, context) => {
|
||||
const options = {
|
||||
config,
|
||||
collection,
|
||||
req: context,
|
||||
};
|
||||
|
||||
const policyResults = await policies(options);
|
||||
let policyResults = await policies(options);
|
||||
|
||||
policyResults = formatConfigNames(policyResults, config.collections);
|
||||
policyResults = formatConfigNames(policyResults, config.globals);
|
||||
|
||||
return policyResults;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,15 +4,14 @@ const policies = async (args) => {
|
||||
try {
|
||||
const {
|
||||
config,
|
||||
collection: {
|
||||
config: collectionConfig,
|
||||
},
|
||||
req,
|
||||
req: { user },
|
||||
} = args;
|
||||
|
||||
const isLoggedIn = !!(user);
|
||||
|
||||
const collectionConfig = (user && user.collection) ? config.collections.find(collection => collection.slug === user.collection) : null;
|
||||
|
||||
const returnPolicyResults = (entity, operations) => {
|
||||
const results = {};
|
||||
|
||||
@@ -36,9 +35,13 @@ const policies = async (args) => {
|
||||
return results;
|
||||
};
|
||||
|
||||
const policyResults = {
|
||||
canAccessAdmin: collectionConfig.policies.admin ? collectionConfig.policies.admin(args) : isLoggedIn,
|
||||
};
|
||||
const policyResults = {};
|
||||
|
||||
if (collectionConfig) {
|
||||
policyResults.canAccessAdmin = collectionConfig.policies.admin ? collectionConfig.policies.admin(args) : isLoggedIn;
|
||||
} else {
|
||||
policyResults.canAccessAdmin = false;
|
||||
}
|
||||
|
||||
config.collections.forEach((collection) => {
|
||||
policyResults[collection.slug] = returnPolicyResults(collection, allOperations);
|
||||
|
||||
@@ -7,7 +7,6 @@ const policiesHandler = config => async (req, res) => {
|
||||
const policyResults = await policies({
|
||||
req,
|
||||
config,
|
||||
collection: req.collection,
|
||||
});
|
||||
|
||||
return res.status(httpStatus.OK)
|
||||
|
||||
@@ -11,7 +11,6 @@ const {
|
||||
forgotPassword,
|
||||
resetPassword,
|
||||
update,
|
||||
policies,
|
||||
} = require('./requestHandlers');
|
||||
|
||||
const {
|
||||
@@ -44,10 +43,6 @@ const authRoutes = (collection, config, sendEmail) => {
|
||||
.route(`/${slug}/me`)
|
||||
.get(me);
|
||||
|
||||
router
|
||||
.route(`/${slug}/policies`)
|
||||
.get(policies(config));
|
||||
|
||||
router
|
||||
.route(`/${slug}/first-register`)
|
||||
.post(registerFirstUser(config));
|
||||
|
||||
@@ -12,7 +12,7 @@ const {
|
||||
} = require('./resolvers');
|
||||
|
||||
const {
|
||||
login, me, init, refresh, register, forgotPassword, resetPassword, policies,
|
||||
login, me, init, refresh, register, forgotPassword, resetPassword,
|
||||
} = require('../../auth/graphql/resolvers');
|
||||
|
||||
const buildPaginatedListType = require('../../graphql/schema/buildPaginatedListType');
|
||||
@@ -129,21 +129,21 @@ function registerCollections() {
|
||||
|
||||
if (collection.config.auth) {
|
||||
collection.graphQL.jwt = this.buildObjectType(
|
||||
'JWT',
|
||||
formatName(`${slug}JWT`),
|
||||
collection.config.fields.filter(field => field.saveToJWT).concat([
|
||||
{
|
||||
name: 'email',
|
||||
type: 'email',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'collection',
|
||||
type: 'text',
|
||||
required: true,
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
this.Query.fields[`policies${singularLabel}`] = {
|
||||
type: this.buildPoliciesType(),
|
||||
resolve: policies(this.config, collection),
|
||||
};
|
||||
|
||||
this.Query.fields[`me${singularLabel}`] = {
|
||||
type: collection.graphQL.jwt,
|
||||
resolve: me,
|
||||
|
||||
@@ -2,6 +2,8 @@ const { MissingFieldType } = require('../errors');
|
||||
const validations = require('./validations');
|
||||
|
||||
const sanitizeFields = (fields) => {
|
||||
if (!fields) return [];
|
||||
|
||||
return fields.map((unsanitizedField) => {
|
||||
const field = { ...unsanitizedField };
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ const initCollections = require('../collections/graphql/init');
|
||||
const initGlobals = require('../globals/graphql/init');
|
||||
const buildWhereInputType = require('./schema/buildWhereInputType');
|
||||
const errorHandler = require('./errorHandler');
|
||||
const { policies } = require('../auth/graphql/resolvers');
|
||||
|
||||
class GraphQL {
|
||||
constructor(init) {
|
||||
@@ -49,6 +50,11 @@ class GraphQL {
|
||||
this.initCollections();
|
||||
this.initGlobals();
|
||||
|
||||
this.Query.fields.Policies = {
|
||||
type: this.buildPoliciesType(),
|
||||
resolve: policies(this.config),
|
||||
};
|
||||
|
||||
this.Query = {
|
||||
...this.Query,
|
||||
...(this.config.queries),
|
||||
|
||||
@@ -17,6 +17,7 @@ const sanitizeConfig = require('./utilities/sanitizeConfig');
|
||||
const buildEmail = require('./email/build');
|
||||
const identifyAPI = require('./express/middleware/identifyAPI');
|
||||
const errorHandler = require('./express/middleware/errorHandler');
|
||||
const { policies } = require('./auth/requestHandlers');
|
||||
|
||||
class Payload {
|
||||
constructor(options) {
|
||||
@@ -57,6 +58,9 @@ class Payload {
|
||||
this.express.use(initWebpack(this.config));
|
||||
}
|
||||
|
||||
// Init policies route
|
||||
this.router.get('/policies', policies(this.config));
|
||||
|
||||
// Init GraphQL
|
||||
this.router.use(
|
||||
this.config.routes.graphQL,
|
||||
|
||||
Reference in New Issue
Block a user