0.3.14 • Published 3 months ago

mua-backend v0.3.14

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Mua-Backend Documentation and Integration Guide

The mua-backend is a full authentication and user management system designed to integrate seamlessly with the mua-frontend. It enables a Slack-like authentication experience, supporting multiple workspaces where users can log in with shared or separate credentials per workspace.

This backend simplifies the creation and management of accounts, users, and system administrators, providing ready-to-use routes.

Features

  • Slack-like Workspace Management: Users can log into separate workspaces, each with its own independent credentials.
  • Route Auto-Generation: Automatically generates routes for account, user, and system admin management.
  • Flexible API Integration: Built to work with an API server created using the Express Async API.

Getting Started

Installation

Install mua-backend via npm:

npm install mua-backend

Setup

To set up the mua-backend, you need to provide the following parameters:

  1. API Server
  1. Models
  • To ensure smooth integration and customization, it's essential that you pass the following models to the mua-backend. These models define the mandatory properties that are needed for managing accounts, users, and system administrators. You can extend these models by adding additional properties as needed.

  • AccountModel: The model for managing workspace accounts.

const AccountSchema = new mongoose.Schema({
  name: { type: String },
  urlFriendlyName: { type: String, unique: true },
  logo: { type: String },
  deleted: { type: Boolean }
}, { timestamps: true })

export default mongoose.model('Account', AccountSchema)
  • UserModel: The model for managing users.
const UserSchema = new mongoose.Schema({
  name: { type: String },
  email: { type: String, lowercase: true, required: true, match: /.+[\\@].+\..+/ },
  password: { type: String },
  googleProfileId: { type: String },
  microsoftProfileId: { type: String },
  githubProfileId: { type: String },
  role: { type: String, default: 'user', enum: ['user', 'admin'] },
  accountId: { type: Schema.Types.ObjectId, ref: 'Account', required: true },
  profilePicture: { type: String },
  verified: { type: Boolean, default: false },
  deleted: { type: Boolean }
}, { timestamps: true })

export default mongoose.model('User', UserSchema)
  • SystemAdminModel: The model for managing system administrators.
const SystemAdminSchema = new mongoose.Schema({
  name: { type: String },
  email: { type: String, lowercase: true, required: true, match: /.+[\\@].+\..+/, unique: true },
  password: { type: String },
  profilePicture: { type: String }
}, { timestamps: true });

export default mongoose.model('SystemAdmin', SystemAdminSchema)
  1. Integration Example
import MuaBackend from'mua-backend' 
import AccountModel from'./models/account' 
import UserModel from'./models/user'
import SystemAdminModel from'./models/systemAdmin' 

MuaBackend({  
  apiServer,  
  AccountModel,
  UserModel,
  SystemAdminModel,
  hooks: {} // optional  
})
  1. Add to .env file

You need to provide the following environment variables in a .env file for Mua to function properly.

NODE_ENV=development # Environment mode (e.g., development, production)
SECRETS=testsecret1 testsecret2 # Space-separated list of secrets used for token encryption

APP_URL=<app_url> # The base URL of your application

BLUEFOX_TRANSACTIONAL_EMAIL_API_URL=<emailfox_transactional_url> # URL for the Bluefox transactional email API
BLUEFOX_API_KEY=<bluefox_email_api_key> # API key for Bluefox email service

BLUEFOX_TEMPLATE_ID_ADMIN_VERIFY_EMAIL=<bluefox_template_id> # Bluefox Template ID for admin email verification
BLUEFOX_TEMPLATE_ID_ADMIN_FORGOT_PASSWORD=<bluefox_template_id> # Bluefox Template ID for admin forgot password
BLUEFOX_TEMPLATE_ID_ADMIN_INVITATION=<bluefox_template_id> # Bluefox Template ID for admin invitation
BLUEFOX_TEMPLATE_ID_ACCOUNT_FINALIZE_REGISTRATION=<bluefox_template_id> # Bluefox Template ID for account registration finalization
BLUEFOX_TEMPLATE_ID_ACCOUNT_FORGOT_PASSWORD=<bluefox_template_id> # Bluefox Template ID for account forgot password
BLUEFOX_TEMPLATE_ID_ACCOUNT_INVITATION=<bluefox_template_id> # Bluefox Template ID for account invitation
BLUEFOX_TEMPLATE_ID_ACCOUNT_LOGIN_SELECT=<bluefox_template_id> # Bluefox Template ID for account login selection
BLUEFOX_TEMPLATE_ID_ACCOUNT_VERIFY_EMAIL=<bluefox_template_id> # Bluefox Template ID for account email verification
BLUEFOX_TEMPLATE_ID_ACCOUNT_CREATE_PASSWORD=<bluefox_template_id> # Bluefox Template ID for account password creation

CDN_BASE_URL=<cdn_base_url> # Base URL for your CDN

AWS_BUCKET_PATH=<aws.bucket.path> # Path to the AWS bucket
AWS_BUCKET_NAME=<your_aws_bucket_name> # Name of the AWS S3 bucket
AWS_FOLDER_NAME=<your_aws_folder_name> # Folder name in the AWS S3 bucket
AWS_REGION=<your_aws_region> # AWS region for the S3 bucket
AWS_ACCESS_KEY_ID=<your_aws_access_key_id> # AWS access key ID
AWS_SECRET_ACCESS_KEY=<your_aws_secret_access_key> # AWS secret access key

ALPHA_MODE=false # Enable alpha mode (true/false)
MAX_FILE_SIZE=5242880 # Maximum file upload size in bytes (e.g., 5MB)

GOOGLE_CLIENT_ID=<your_google_client_id> # Google OAuth client ID
GOOGLE_CLIENT_SECRET=<your_google_client_secret> # Google OAuth client secret

MICROSOFT_CLIENT_ID=<your_microsoft_client_id> # Microsoft OAuth client ID
MICROSOFT_CLIENT_SECRET=<your_microsoft_client_secret> # Microsoft OAuth client secret

GITHUB_CLIENT_ID=<your_github_client_id> # GitHub OAuth client ID
GITHUB_CLIENT_SECRET=<your_github_client_secret> # GitHub OAuth client secret

Customization

The mua-backend allows users to customize behavior after specific actions, such as creating or deleting accounts. Pass a hooks object with functions to override default behaviors:

MuaBackend({
  apiServer,
  AccountModel,
  UserModel,
  SystemAdminModel,
  hooks: {
    deleteAccount: {
      post: (params) => {
        // Custom logic after deleting an account
      },
    },
    createAccount: {
      post: (params) => {
        // Custom logic after creating an account
      },
    },
    createNewUser: {
      post: (params) => {
        // Custom logic after creating a new user
      },
    },
    updateUserEmail: {
      post: (params) => {
        // Custom logic after updating a user's email
      },
    },
  },
})

Each hook accepts an object with a post method that executes custom logic after the associated action is completed.

API Routes

Developers using mua can rely on the seamless integration with the mua-frontend, so they don't need to concern themselves with these routes. The table below outlines the automatically generated routes and their purposes:

Accounts routes

RouteMethodDescription
/v1/accounts/createPOSTCreate an account.
/v1/accounts/POSTCreate a new account by admin.
/v1/accounts/:id/logoPOSTUpload account logo.
/v1/accounts/permission/:permissionForPOSTAssign permission to an account.
/v1/accounts/GETRetrieve all accounts.
/v1/accounts/:idGETRetrieve account by ID.
/v1/accounts/by-url-friendly-name/:urlFriendlyNameGETRetrieve account by URL-friendly name.
/v1/accounts/check-availabilityGETCheck if account is available pass urlFriendlyName in query.
/v1/accounts/:id/namePATCHUpdate account name.
/v1/accounts/:id/urlFriendlyNamePATCHUpdate account URL-friendly name.
/v1/accounts/:id/logoDELETEDelete account logo.
/v1/accounts/:idDELETEDelete an account.

System admins routes

RouteMethodDescription
/v1/system-admins/permission/:permissionForPOSTAssign permission to a system admin.
/v1/system-admins/:id/profile-picturePOSTUpload system admin profile picture.
/v1/system-admins/GETRetrieve all system admins.
/v1/system-admins/:idGETRetrieve system admin by ID.
/v1/system-admins/:id/access-tokenGETRetrieve access token for a system admin.
/v1/system-admins/:id/namePATCHUpdate system admin name.
/v1/system-admins/:id/passwordPATCHUpdate system admin password.
/v1/system-admins/:id/emailPATCHUpdate system admin email.
/v1/system-admins/:id/email-confirmPATCHConfirm system admin email.
/v1/system-admins/:id/profile-pictureDELETEDelete system admin profile picture.
/v1/system-admins/:idDELETEDelete a system admin.

Users routes

RouteMethodDescription
/v1/accounts/:accountId/usersPOSTCreate a new user in account.
/v1/accounts/:accountId/users/:id/profile-picturePOSTUpload user profile picture.
/v1/accounts/:accountId/usersGETRetrieve all users in account.
/v1/accounts/:accountId/users/:idGETRetrieve user by ID in account.
/v1/accounts/:accountId/users/:id/namePATCHUpdate user name in account.
/v1/accounts/:accountId/users/:id/passwordPATCHUpdate user password.
/v1/accounts/:accountId/users/:id/emailPATCHUpdate user email.
/v1/accounts/:accountId/users/:id/create-passwordPATCHCreate password for user.
/v1/accounts/:accountId/users/:id/email-confirmPATCHConfirm user email.
/v1/accounts/:accountId/users/:id/profile-pictureDELETEDelete user profile picture.
/v1/accounts/:accountId/users/:idDELETEDelete a user in account.

Auth routes

RouteMethodDescription
/v1/accounts/:id/loginPOSTLog in to an account.
/v1/accounts/:id/login/url-friendly-namePOSTLog in using URL-friendly name.
/v1/accounts/loginPOSTLog in to any account.
/v1/system-admins/loginPOSTLog in as system admin.
/v1/accounts/:accountId/users/:id/finalize-registrationPOSTFinalize user registration in account.
/v1/accounts/:accountId/users/:userId/resend-finalize-registrationPOSTResend finalize registration for a user.
/v1/accounts/:accountId/users/:id/access-tokenGETRetrieve user access token in account.

Provider Auth routes

RouteMethodDescription
/v1/accounts/:accountId/users/:id/link/provider/:providerPOSTLink user to a provider (e.g., Google).
/v1/accounts/create-account/provider/:providerPOSTCreate an account via third-party provider.
/v1/accounts/:id/login/provider/:providerPOSTLog in via third-party provider (e.g., Google).
/v1/accounts/provider/google/callbackGETGoogle OAuth callback.
/v1/accounts/provider/microsoft/callbackGETMicrosoft OAuth callback.
/v1/accounts/provider/github/callbackGETGitHub OAuth callback.
/v1/accounts/:accountId/users/:id/provider/:providerPATCHUpdate user provider link.

Forgot password routes

RouteMethodDescription
/v1/accounts/:id/forgot-password/sendPOSTSend password reset email for account.
/v1/system-admins/forgot-password/sendPOSTSend password reset email for system admin.
/v1/accounts/:id/forgot-password/resetPOSTReset account password.
/v1/system-admins/forgot-password/resetPOSTReset system admin password.

Invitation routes

RouteMethodDescription
/v1/accounts/:id/invitation/sendPOSTSend invitation for account.
/v1/system-admins/invitation/sendPOSTSend invitation for system admin.
/v1/accounts/:id/invitation/resendPOSTResend account invitation.
/v1/system-admins/invitation/resendPOSTResend system admin invitation.
/v1/accounts/:id/invitation/acceptPOSTAccept account invitation.
/v1/system-admins/invitation/acceptPOSTAccept system admin invitation.

Statistics routes

RouteMethodDescription
/v1/statistics/accounts/GETRetrieve statistics for accounts.
/v1/statistics/users/GETRetrieve statistics for users.
0.3.9

5 months ago

0.3.14

3 months ago

0.3.13

3 months ago

0.3.12

3 months ago

0.3.11

3 months ago

0.3.10

3 months ago

0.3.6

7 months ago

0.3.8

6 months ago

0.3.7

6 months ago

0.3.5

8 months ago

0.1.0

10 months ago

0.3.0

8 months ago

0.2.0

9 months ago

0.1.1

10 months ago

0.3.2

8 months ago

0.3.1

8 months ago

0.3.4

8 months ago

0.3.3

8 months ago

0.0.8

11 months ago

0.0.7

12 months ago

0.0.6

12 months ago

0.0.5

12 months ago

0.0.4

1 year ago

0.0.3

1 year ago