1.0.0 • Published 6 months ago

auth-service-manager v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

Auth Service Package

This is a lightweight and configurable authentication service package for Node.js and TypeScript applications. It simplifies user authentication by providing pre-built signup and login functionality with support for JWT-based authentication, password hashing, and validation.


Features

  • JWT Authentication: Secure token-based authentication.
  • Password Hashing: Uses SHA-512 for secure password storage.
  • Validation: Built-in email and password validation.
  • Customizable: Plug in your own user and auth models.
  • TypeScript Support: Fully typed for type safety and better development experience.

Installation

Install the package using npm:

npm install auth-service-manager

Usage

1. Set Up Models

Create your User and Auth models using a database like MongoDB with Mongoose:

import mongoose from "mongoose";

const UserSchema = new mongoose.Schema({
  email: { type: String, required: true },
  name: { type: String, required: true },
});

const AuthSchema = new mongoose.Schema({
  userId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
  password: { type: String, required: true },
  secureKey: { type: String, required: true },
});

export const User = mongoose.model("User", UserSchema);
export const Auth = mongoose.model("Auth", AuthSchema);

2. Initialize AuthService

Import and initialize the AuthService class with your configuration:

import express from "express";
import { AuthService } from "auth-service-manager";
import { User, Auth } from "./models";

const app = express();
app.use(express.json());

const authService = new AuthService({
  jwtSecret: process.env.JWT_SECRET as string, // Set this in your environment variables
  userModel: User,
  authModel: Auth,
});

// Routes
app.post("/signup", authService.signup());
app.post("/login", authService.login());

app.listen(3000, () => {
  console.log("Server is running on http://localhost:3000");
});

3. Environment Variables

Set the JWT_SECRET in your environment file:

JWT_SECRET=your_jwt_secret_key

4. Test the Endpoints

Use tools like Postman to test the /signup and /login endpoints:

Signup Request

POST /signup
{
  "email": "user@example.com",
  "name": "John Doe",
  "password": "Password@123"
}

Login Request

POST /login
{
  "email": "user@example.com",
  "password": "Password@123"
}

Validation Rules

Email Validation

  • Must have a valid email format.
  • Cannot start with a digit.

Password Validation

  • Minimum 8 characters.
  • Must include at least one uppercase letter, one lowercase letter, one number, and one special character.

Utilities

hashPassword

A utility function for hashing passwords.

import { hashPassword } from "auth-service-manager";

const secureKey = "your_secure_key";
const hashedPassword = hashPassword("Password@123", secureKey);

Development

Prerequisites

  • Node.js v14 or higher
  • TypeScript

Run the Project

  1. Start the development server:
    npm start
  2. The project will run on http://localhost:3000.