September 28, 2022
·
1125 commits
to main
since this release
@envelop/generic-auth@4.6.0
Minor Changes
-
#1528
50f214a2Thanks @{! - give access to execute args invalidateUserfunction.This is useful in conjunction with the
fieldAuthExtensionparameter to achieve custom per field validation:import { ValidateUserFn } from '@envelop/generic-auth' const validateUser: ValidateUserFn<UserType> = async ({ user, executionArgs, fieldAuthExtension }) => { if (!user) { throw new Error(`Unauthenticated!`) } // You have access to the object define in the resolver tree, allowing to define any custom logic you want. const validate = fieldAuthExtension?.validate if (validate) { await validate({ user, variables: executionArgs.variableValues, context: executionArgs.contextValue }) } } const resolvers = { Query: { resolve: (_, { userId }) => getUser(userId), extensions: { auth: { validate: ({ user, variables, context }) => { // We can now have access to the operation and variables to decide if the user can execute the query if (user.id !== variables.userId) { throw new Error(`Unauthorized`) } } } } } } }