@zestic/oauth-core
Version:
Framework-agnostic OAuth authentication library with support for multiple OAuth flows
144 lines • 4.91 kB
JavaScript
;
/**
* GraphQL Resolvers for OAuth Core
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.validationHelpers = exports.resolvers = void 0;
exports.createGraphQLContext = createGraphQLContext;
const RegistrationService_1 = require("../services/RegistrationService");
const MagicLinkService_1 = require("../services/MagicLinkService");
const ErrorHandler_1 = require("../utils/ErrorHandler");
/**
* GraphQL resolvers
*/
exports.resolvers = {
Query: {
_empty: () => 'OAuth Core GraphQL API'
},
Mutation: {
/**
* Register a new user
*/
register: async (_parent, { input }, context) => {
try {
const registrationService = new RegistrationService_1.RegistrationService(context.adapters);
return await registrationService.register(input);
}
catch (error) {
// Handle OAuth errors gracefully
if (ErrorHandler_1.ErrorHandler.isOAuthError(error)) {
return {
success: false,
message: error.message,
code: error.code
};
}
// Handle unexpected errors
return {
success: false,
message: 'An unexpected error occurred during registration',
code: 'INTERNAL_ERROR'
};
}
},
/**
* Send a magic link to the specified email
*/
sendMagicLink: async (_parent, { input }, context) => {
try {
const magicLinkService = new MagicLinkService_1.MagicLinkService(context.adapters, context.magicLinkConfig);
return await magicLinkService.sendMagicLink(input);
}
catch (error) {
// Handle OAuth errors gracefully
if (ErrorHandler_1.ErrorHandler.isOAuthError(error)) {
return {
success: false,
message: error.message,
code: error.code
};
}
// Handle unexpected errors
return {
success: false,
message: 'An unexpected error occurred while sending magic link',
code: 'INTERNAL_ERROR'
};
}
}
},
// Custom scalar resolver for JSON
JSON: {
serialize: (value) => value,
parseValue: (value) => value,
parseLiteral: (ast) => {
switch (ast.kind) {
case 'StringValue':
case 'BooleanValue':
return ast.value;
case 'IntValue':
case 'FloatValue':
return ast.value ? parseFloat(String(ast.value)) : 0;
case 'ObjectValue':
return ast.fields?.reduce((obj, field) => {
obj[field.name.value] = exports.resolvers.JSON.parseLiteral(field.value);
return obj;
}, {}) ?? {};
case 'ListValue':
return ast.values?.map(exports.resolvers.JSON.parseLiteral) ?? [];
case 'NullValue':
return null;
default:
throw new Error(`Unexpected kind in JSON literal: ${ast.kind}`);
}
}
}
};
/**
* Helper function to create GraphQL context
*/
function createGraphQLContext(adapters, magicLinkConfig) {
return {
adapters,
magicLinkConfig
};
}
/**
* Validation helpers for resolvers
*/
exports.validationHelpers = {
/**
* Validate that required context is present
*/
validateContext(context) {
if (!context.adapters) {
throw new Error('OAuth adapters not provided in GraphQL context');
}
if (!context.adapters.user) {
throw new Error('User adapter not provided in GraphQL context');
}
if (!context.adapters.graphql) {
throw new Error('GraphQL adapter not provided in GraphQL context');
}
if (!context.magicLinkConfig) {
throw new Error('Magic link configuration not provided in GraphQL context');
}
},
/**
* Sanitize error messages for client consumption
*/
sanitizeError(error) {
if (ErrorHandler_1.ErrorHandler.isOAuthError(error)) {
return {
message: error.message,
code: error.code
};
}
// Don't expose internal error details to clients
return {
message: 'An internal error occurred',
code: 'INTERNAL_ERROR'
};
}
};
//# sourceMappingURL=resolvers.js.map