1.2.30 • Published 5 months ago

is-auth-service v1.2.30

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

Auth Service Package

The is-auth-service package provides a complete authentication and authorization solution for Node.js applications. It includes JWT middleware, Passport-based local and Google authentication, and a built-in User database with basic CRUD operations.

Features

  • JWT Middleware: Easily protect your routes with JSON Web Token (JWT) middleware to secure API endpoints. Supports token generation, verification, and decoding for seamless user sessions.
  • Passport Authentication:
    • Local Authentication: Enables secure email and password login, with customizable login and error handling.
    • Google OAuth: Allows users to log in with their Google account, simplifying user onboarding with OAuth 2.0.
  • User Database with CRUD: Provides a basic User model with Create, Read, Update, and Delete (CRUD) operations, making user management straightforward and scalable.

Installation

  1. Install
npm install is-auth-service

Usage

  1. Server Setup
  • app.js
import { authRouter, AuthConfig, passportInit } from 'is-auth-service';
import express from 'express';

const app = express();

app.use(
  session({
    secret: 'YOUR_SESSION_SECRET',
    resave: false,
    saveUninitialized: true,
    cookie: { secure: false },
  })
);

// Initialize AuthConfig
AuthConfig.getInstance(jwtSecret, nodeEnv, redirectUrl);

// Google and Local
passportInit(app, {
  googleClientId,
  googleSecret,
  callbackUrl,
});

// Or Local Only
passportInit(app);

// Register routers
app.use(authRouter);
  1. Use middleware in router
  • protected router
import express from 'express';
import { authenticateJWT } from 'is-auth-service';
import { addFunction } from './controllers/YOUR_ROUTER';

const router = express.Router();

router.post(authenticateJWT, addFunction);
  1. Avaiable the routes

Client Example

// GET Google login client
window.location.href = `${process.env.REACT_APP_AUTH_API_URL}/google`;

// Local login
api.post('/local/login', { username: 'your_email', password: 'your_password' });

// Local signup
api.post('/local/signup', {
  email: 'your_email',
  password: 'your_password',
  username: 'your_username',
});

// Logout
api.post('/logout');

// Get user
api.get('/user/1');

// Update user
api.put('/user/1', { username: 'new_user_name' });

// Add user
api.post('/user', {
  username: 'my_username',
  password: 'my-pw',
  email: 'my_email@mail.com',
});

// Delete user
api.delete('/user/1');

Postman example

curl --location '{{SERVER_URL}}generate-token' \
--header 'Content-Type: application/json' \
--data '{
    "userId": "1"
}'
  1. Required Frontend pages
1.2.27

5 months ago

1.2.28

5 months ago

1.2.29

5 months ago

1.2.30

5 months ago

1.2.26

8 months ago

1.2.25

8 months ago

1.2.24

8 months ago

1.2.23

8 months ago

1.2.22

8 months ago

1.2.21

8 months ago

1.2.20

8 months ago

1.2.19

8 months ago

1.2.18

8 months ago

1.2.17

9 months ago

1.2.16

9 months ago

1.2.15

9 months ago

1.2.14

9 months ago

1.2.13

9 months ago

1.2.12

9 months ago

1.2.11

9 months ago

1.2.10

9 months ago

1.2.9

9 months ago

1.2.8

9 months ago

1.2.7

9 months ago

1.2.6

9 months ago

1.2.5

9 months ago

1.2.4

9 months ago

1.2.3

9 months ago

1.2.2

9 months ago

1.2.1

9 months ago

1.2.0

9 months ago

1.1.0

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago