2.4.2 • Published 10 months ago

login-auth-services v2.4.2

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

Login Auth Services

npm license

Login Auth Services is a robust authentication package supporting multiple OAuth providers (Google, GitHub, Microsoft, and Okta) with multi-factor authentication (OTP). It also includes a sync-db command to synchronize your database schema using TypeORM.

šŸš€ Features

  • šŸ”’ OAuth Authentication for:
    • Google
    • GitHub
    • Microsoft
    • Okta
  • šŸ”‘ Multi-Factor Authentication (MFA) using One-Time Password (OTP)
  • šŸ›  Database Agnostic - Works with any database supported by TypeORM
  • šŸ”„ Database Synchronization via sync-db command
  • šŸ” JWT-based Authentication
  • šŸ“§ Email verification & password reset using Nodemailer
  • šŸ“¦ Easily Integrates with Express.js

šŸ“¦ Installation

Install via npm:

npm install login-auth-services

or using yarn:

yarn add login-auth-services

šŸ“Œ TypeORM & Database Setup

This package uses TypeORM for database interactions. You can use any relational database (MySQL, PostgreSQL, SQLite, etc.).

1ļøāƒ£ Configure database.config.ts

Create a database.config.ts file to configure TypeORM's DataSource:

import { DataSource } from "typeorm";
import dotenv from "dotenv";

dotenv.config();

export const AppDataSource = new DataSource({
  type: process.env.DATABASE_TYPE as any,
  host: process.env.DATABASE_HOST,
  port: Number(process.env.DATABASE_PORT),
  username: process.env.DATABASE_USERNAME,
  password: process.env.DATABASE_PASSWORD,
  database: process.env.DATABASE_NAME,
  synchronize: false, // Use sync-db instead
  logging: false,
  entities: ["src/entity/*.ts"],
});

AppDataSource.initialize()
  .then(() => console.log("Database connected"))
  .catch((err) => console.error("Database connection failed", err));

2ļøāƒ£ Define Entities

Entities define the database structure. Example User entity:

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  email: string;

  @Column()
  password: string;
}

šŸ”„ Sync Database

This package includes a sync-db command to synchronize your database schema with TypeORM entities.

Running sync-db

Run the following command to apply migrations and synchronize the database:

npx sync-db

This will automatically update the database schema based on TypeORM entities. Ensure synchronize is disabled in database.config.ts to avoid conflicts.


šŸ”‘ Authentication Usage

Google OAuth Login

import { GoogleAuthService } from "login-auth-services";

const googleAuth = new GoogleAuthService({
  clientId: "your-client-id",
  clientSecret: "your-client-secret",
  redirectUri: "your-redirect-uri",
});

app.get("/auth/google", async (req, res) => {
  const url = googleAuth.getAuthUrl();
  res.redirect(url);
});

app.get("/auth/google/callback", async (req, res) => {
  const token = await googleAuth.getAccessToken(req.query.code);
  res.json(token);
});

GitHub OAuth Login

import { GitHubAuthService } from "login-auth-services";

const githubAuth = new GitHubAuthService({
  clientId: "your-client-id",
  clientSecret: "your-client-secret",
  redirectUri: "your-redirect-uri",
});

app.get("/auth/github", async (req, res) => {
  const url = githubAuth.getAuthUrl();
  res.redirect(url);
});

app.get("/auth/github/callback", async (req, res) => {
  const token = await githubAuth.getAccessToken(req.query.code);
  res.json(token);
});

šŸ“§ Email Verification & Password Reset

Send Verification Email

import { sendVerificationEmail } from "login-auth-services";

await sendVerificationEmail(user.email, user.verificationCode);

šŸ“Œ Custom Login Authentication

Register User

import { registerUser } from "login-auth-services";

const user = await registerUser({
  email: "user@example.com",
  password: "securepassword",
});

Login User

import { loginUser } from "login-auth-services";

const token = await loginUser({
  email: "user@example.com",
  password: "securepassword",
});

Verify OTP for Multi-Factor Authentication

import { verifyOTP } from "login-auth-services";

const isValid = await verifyOTP({
  email: "user@example.com",
  otp: "123456",
});

šŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.


šŸ™Œ Contributions

Contributions, issues, and feature requests are welcome! Feel free to fork, submit PRs, or open issues on GitHub.


šŸ“ Author

Harshil Patel
šŸ“§ Email: harshilpatel29072003@gmail.com


This README includes TypeORM configuration, database synchronization with sync-db, and entity setup.

2.4.2

10 months ago

2.4.1

10 months ago

2.4.0

10 months ago

2.3.8

10 months ago

2.3.7

10 months ago

2.3.6

10 months ago

2.3.5

10 months ago

2.3.4

10 months ago

2.3.3

10 months ago

2.3.2

10 months ago

2.3.1

10 months ago

2.3.0

10 months ago

2.2.5

10 months ago

2.2.4

10 months ago

2.2.3

10 months ago

2.2.2

10 months ago

2.2.1

10 months ago

2.1.0

10 months ago

1.3.6

11 months ago

1.3.5

11 months ago

1.3.4

11 months ago

1.3.3

11 months ago

1.3.2

11 months ago

1.3.1

11 months ago

1.3.0

11 months ago

1.2.3

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago

1.2.0

11 months ago

1.1.2

11 months ago

1.1.0

11 months ago

1.0.2

11 months ago

1.0.0

11 months ago