chore: server code formatting
This commit is contained in:
parent
aedabcbdee
commit
68517170e2
234 changed files with 9824 additions and 6195 deletions
|
|
@ -7,7 +7,7 @@ const { AuthenticationError } = require('../errors')
|
|||
function authDirectiveTransformer(schema, directiveName = 'auth') {
|
||||
return mapSchema(schema, {
|
||||
// For object types
|
||||
[MapperKind.OBJECT_TYPE]: (objectType) => {
|
||||
[MapperKind.OBJECT_TYPE]: objectType => {
|
||||
const directive = getDirective(schema, objectType, directiveName)?.[0]
|
||||
if (directive) {
|
||||
const requiredAuthRole = directive.requires
|
||||
|
|
@ -15,7 +15,7 @@ function authDirectiveTransformer(schema, directiveName = 'auth') {
|
|||
}
|
||||
return objectType
|
||||
},
|
||||
|
||||
|
||||
// For field definitions
|
||||
[MapperKind.OBJECT_FIELD]: (fieldConfig, _fieldName, typeName) => {
|
||||
const directive = getDirective(schema, fieldConfig, directiveName)?.[0]
|
||||
|
|
@ -23,26 +23,30 @@ function authDirectiveTransformer(schema, directiveName = 'auth') {
|
|||
const requiredAuthRole = directive.requires
|
||||
fieldConfig._requiredAuthRole = requiredAuthRole
|
||||
}
|
||||
|
||||
|
||||
// Get the parent object type
|
||||
const objectType = schema.getType(typeName)
|
||||
|
||||
|
||||
// Apply auth check to the field's resolver
|
||||
const { resolve = defaultFieldResolver } = fieldConfig
|
||||
fieldConfig.resolve = function (root, args, context, info) {
|
||||
const requiredRoles = fieldConfig._requiredAuthRole || objectType._requiredAuthRole
|
||||
if (!requiredRoles) return resolve.apply(this, [root, args, context, info])
|
||||
|
||||
const requiredRoles =
|
||||
fieldConfig._requiredAuthRole || objectType._requiredAuthRole
|
||||
if (!requiredRoles)
|
||||
return resolve.apply(this, [root, args, context, info])
|
||||
|
||||
const user = context.req.session.user
|
||||
if (!user || !_.includes(_.upperCase(user.role), requiredRoles)) {
|
||||
throw new AuthenticationError('You do not have permission to access this resource!')
|
||||
throw new AuthenticationError(
|
||||
'You do not have permission to access this resource!',
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
return resolve.apply(this, [root, args, context, info])
|
||||
}
|
||||
|
||||
|
||||
return fieldConfig
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue