1.0.7 • Published 10 months ago

jnext-library-ts v1.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

jnext-library-ts

An authentication library for Node.js simplifies the implementation of authentication-related functionalities in your Express applications. It provides tools for managing Sequelize models, creating Express routes, initializing email configurations, and Swagger documentation setup.

Installation

npm install jnext-library-ts

Usage

1. Setup server connection in your project

import dotenv from 'dotenv';
dotenv.config();
import express, { Express } from 'express';
import { Sequelize } from 'sequelize';
import * as myLib from 'jnext-library-ts';

// Create an Express application
const app: Express = express();

const sequelize = new Sequelize('dbName', 'root', '', {
    dialect: 'mysql',
    host: '127.0.0.1',
    logging: false
});

// Start the server on port 7000
app.listen(7000, () => {
    console.log(`Server is started on port:`, 7000);
});

2. Create Sequelize Models

Use the getModels function to retrieve predefined Sequelize models for users, user_meta, and user_roles. The user_roles model is automatically populated with data for the 'user' and 'admin' roles. Additionally, you can dynamically add fields to the user model using createModel().

import { Sequelize } from 'sequelize';
import * as myLib from 'jnext-library-ts';

const sequelize = new Sequelize('dbName', 'root', '', {
    dialect: 'mysql',
    host: '127.0.0.1',
    logging: false
});

myLib.getModels();
myLib.createModel('customUserModel', sequelize, {
    // Define additional fields here
    filedName: dataType // Datatype should be string. Ex. 'STRING' , 'INTEGER'
});

3. Create Express Routes

Use the createRoutes function to fetch API routes from the library for use in your Express application.

import express, { Express } from 'express';
import * as myLib from 'jnext-library-ts';

const router = myLib.createRoutes({ validator: false });

const app: Express = express();
app.use('/', router);

4. Set Environment variables in .env files

   PORT: 8000

   #JWT comfiguration details
   JWT_SECRET: jwtSecretkey
   JWT_EXPIRATION_TIME: Expiration time of jwt. Example - 1h

   #APIs prefix route to access swagger
   API_BASE_PREFIX: /

   #To manage delete APIs functionality. Hard delete or soft delete
   HARD_DELETE: false

   #To add/ manage custome templete for email Templetes in request data
   CUSTOM_TEMPLATE: true

   #To manage send mail using SMTP or sendmail()   
   SMTP=true

Example

import express, { Express } from 'express';
import Sequelize from 'sequelize';
import * as myLib from 'jnext-library-ts';

const router = myLib.createRoutes({ validator: false });
const sequelize = new Sequelize.Sequelize('authapis', 'root', '', {
    dialect: 'mysql',
    host: '127.0.0.1',
    logging: false
});

myLib.getModels();
myLib.createModel('customUserModel', sequelize, {
    // Define additional fields here
});

const app: Express = express();
app.use('/', router);

app.listen(8000, () => {
    console.log(`Server is started on`, 8000);
});

Additional Details

Types of validations

TypeDescription
string.baseSpecifies that the value must be a string.
number.baseSpecifies that the value must be a number.
boolean.baseSpecifies that the value must be a boolean.
object.baseSpecifies that the value must be an object.
array.baseSpecifies that the value must be an array.
date.baseSpecifies that the value must be a date.
alternativesSpecifies multiple valid alternatives for the value.
any.requiredSpecifies that the property is required.
any.optionalSpecifies that the property is optional.
any.forbiddenSpecifies that the property is forbidden.
any.allowSpecifies the allowed values for the property.
any.validSpecifies the valid values for the property.
any.invalidSpecifies the invalid values for the property.
any.defaultSpecifies the default value for the property.
string.emailSpecifies that the string must be a valid email.
string.minSpecifies the minimum length of the string.
string.maxSpecifies the maximum length of the string.
number.minSpecifies the minimum value for the number.
number.maxSpecifies the maximum value for the number.
date.minSpecifies the minimum date for the date.
date.maxSpecifies the maximum date for the date.
string.patternSpecifies a regular expression pattern for the string.
any.whenSpecifies conditional validation based on another property.
any.errorSpecifies custom error messages for the property.
any.labelSpecifies a custom label for the property in error messages.
any.messagesSpecifies custom validation error messages.

Acknowledgements