mbkauthe
Version:
MBKTech's reusable authentication system for Node.js applications.
276 lines (253 loc) • 8.99 kB
JavaScript
/**
* Centralized error messages and error codes for mbkauthe
* Provides consistent, user-friendly error messages across the application
*/
// Error codes for different scenarios
export const ErrorCodes = {
// Authentication errors (600-699)
INVALID_CREDENTIALS: 601,
USER_NOT_FOUND: 602,
INCORRECT_PASSWORD: 603,
ACCOUNT_INACTIVE: 604,
APP_NOT_AUTHORIZED: 605,
// 2FA errors (700-799)
TWO_FA_REQUIRED: 701,
TWO_FA_INVALID_TOKEN: 702,
TWO_FA_NOT_CONFIGURED: 703,
TWO_FA_EXPIRED: 704,
// Session errors (800-899)
SESSION_EXPIRED: 801,
SESSION_INVALID: 802,
SESSION_NOT_FOUND: 803,
// Authorization errors (900-999)
INSUFFICIENT_PERMISSIONS: 901,
ROLE_NOT_ALLOWED: 902,
// Input validation errors (1000-1099)
MISSING_REQUIRED_FIELD: 1001,
INVALID_USERNAME_FORMAT: 1002,
INVALID_PASSWORD_LENGTH: 1003,
INVALID_TOKEN_FORMAT: 1004,
INVALID_AUTH_TOKEN: 1005,
API_TOKEN_EXPIRED: 1006,
TOKEN_SCOPE_INSUFFICIENT: 1007,
// Rate limiting (1100-1199)
RATE_LIMIT_EXCEEDED: 1101,
// Server errors (1200-1299)
INTERNAL_SERVER_ERROR: 1201,
DATABASE_ERROR: 1202,
CONFIGURATION_ERROR: 1203,
// GitHub OAuth errors (1300-1399)
GITHUB_NOT_LINKED: 1301,
GITHUB_AUTH_FAILED: 1302,
OAUTH_STATE_MISMATCH: 1303,
};
// User-friendly error messages
export const ErrorMessages = {
// Authentication
[]: {
message: "Invalid username or password",
userMessage: "The username or password you entered is incorrect. Please try again.",
hint: "Check your spelling and make sure Caps Lock is off"
},
[]: {
message: "User account not found",
userMessage: "We couldn't find an account with that username.",
hint: "Please check the username and try again"
},
[]: {
message: "Incorrect password",
userMessage: "The password you entered is incorrect.",
hint: "Make sure you're using the correct password for this account"
},
[]: {
message: "Account is inactive",
userMessage: "Your account has been deactivated.",
hint: "Please contact your administrator to reactivate your account"
},
[]: {
message: "Not authorized for this application",
userMessage: "You don't have permission to access this application.",
hint: "This token may not be authorized for the requested app."
},
// 2FA
[]: {
message: "Two-factor authentication required",
userMessage: "Please enter your 6-digit authentication code.",
hint: "Check your authenticator app for the code"
},
[]: {
message: "Invalid 2FA code",
userMessage: "The authentication code you entered is incorrect.",
hint: "Make sure you're using the latest code from your authenticator app"
},
[]: {
message: "2FA not configured",
userMessage: "Two-factor authentication is not set up for your account.",
hint: "Contact your administrator to enable 2FA"
},
[]: {
message: "2FA code expired",
userMessage: "The authentication code has expired.",
hint: "Please use a fresh code from your authenticator app"
},
// Session
[]: {
message: "Session expired",
userMessage: "Your session has expired. Please log in again.",
hint: "This happens when you've been inactive for too long"
},
[]: {
message: "Invalid session",
userMessage: "Your session is no longer valid. Please log in again.",
hint: "This may happen if you logged in from another device"
},
[]: {
message: "Session not found",
userMessage: "Please log in to continue.",
hint: "You need to be logged in to access this page"
},
// Authorization
[]: {
message: "Insufficient permissions",
userMessage: "You don't have permission to perform this action.",
hint: "Contact your administrator if you need access"
},
[]: {
message: "Role not allowed",
userMessage: "Your account role doesn't have access to this feature.",
hint: "This feature requires a different permission level"
},
// Input Validation
[]: {
message: "Required field missing",
userMessage: "Please fill in all required fields.",
hint: "Username and password are required"
},
[]: {
message: "Invalid username format",
userMessage: "Please enter a valid username.",
hint: "Username must be 1-255 characters"
},
[]: {
message: "Invalid password length",
userMessage: "Password must be at least 8 characters long.",
hint: "Please use a password with 8 or more characters"
},
[]: {
message: "Invalid token format",
userMessage: "Please enter a valid 6-digit code.",
hint: "The code should be 6 numbers from your authenticator app"
},
[]: {
message: "Invalid API token",
userMessage: "The provided API token is invalid.",
hint: "Please check your token and try again"
},
[]: {
message: "API token expired",
userMessage: "The provided API token has expired.",
hint: "Please generate a new API token"
},
[]: {
message: "Token scope insufficient",
userMessage: "This API token doesn't have permission for this operation.",
hint: "Use a token with 'write' scope or create a new one with appropriate permissions"
},
// Rate Limiting
[]: {
message: "Too many requests",
userMessage: "Too many attempts. Please try again later.",
hint: "Wait a few minutes before trying again"
},
// Server Errors
[]: {
message: "Internal server error",
userMessage: "Something went wrong on our end.",
hint: "Please try again later or contact support if the problem persists"
},
[]: {
message: "Database error",
userMessage: "We're experiencing technical difficulties.",
hint: "Please try again in a few moments"
},
[]: {
message: "Configuration error",
userMessage: "The service is temporarily unavailable.",
hint: "Please contact your administrator"
},
// GitHub OAuth
[]: {
message: "GitHub account not linked",
userMessage: "Your GitHub account is not linked to any user account.",
hint: "Please link your GitHub account in your profile settings first"
},
[]: {
message: "GitHub authentication failed",
userMessage: "We couldn't authenticate you with GitHub.",
hint: "Please try again or use username/password login"
},
[]: {
message: "OAuth state mismatch",
userMessage: "Authentication verification failed.",
hint: "Please try logging in again"
},
};
/**
* Get error details by error code
* @param {number} errorCode - The error code
* @param {Object} customData - Optional custom data to merge with error
* @returns {Object} Error details with message, userMessage, and hint
*/
export function getErrorByCode(errorCode, customData = {}) {
const errorDetails = ErrorMessages[errorCode] || {
message: "An error occurred",
userMessage: "An unexpected error occurred. Please try again.",
hint: "Contact support if this problem continues"
};
return {
errorCode,
...errorDetails,
...customData
};
}
/**
* Create a standardized error response
* @param {number} statusCode - HTTP status code
* @param {number} errorCode - Application error code
* @param {Object} customData - Optional custom data
* @returns {Object} Standardized error response
*/
export function createErrorResponse(statusCode, errorCode, customData = {}) {
const error = getErrorByCode(errorCode, customData);
return {
success: false,
statusCode,
errorCode: error.errorCode,
message: error.userMessage || error.message,
hint: error.hint,
timestamp: new Date().toISOString(),
...customData
};
}
/**
* Log error with consistent format
* @param {string} context - Context where error occurred
* @param {number} errorCode - Error code
* @param {Object} additionalInfo - Additional info to log
*/
export function logError(context, errorCode, additionalInfo = {}) {
const error = getErrorByCode(errorCode);
console.error(`[mbkauthe] ${context}:`, {
errorCode,
message: error.message,
...additionalInfo,
timestamp: new Date().toISOString()
});
}
export default {
ErrorCodes,
ErrorMessages,
getErrorByCode,
createErrorResponse,
logError
};