1.2.30 • Published 6 months ago

is-auth-service v1.2.30

Weekly downloads
-
License
ISC
Repository
-
Last release
6 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

6 months ago

1.2.28

6 months ago

1.2.29

6 months ago

1.2.30

6 months ago

1.2.26

9 months ago

1.2.25

9 months ago

1.2.24

9 months ago

1.2.23

9 months ago

1.2.22

9 months ago

1.2.21

9 months ago

1.2.20

9 months ago

1.2.19

9 months ago

1.2.18

9 months ago

1.2.17

10 months ago

1.2.16

10 months ago

1.2.15

10 months ago

1.2.14

10 months ago

1.2.13

10 months ago

1.2.12

10 months ago

1.2.11

10 months ago

1.2.10

10 months ago

1.2.9

10 months ago

1.2.8

10 months ago

1.2.7

10 months ago

1.2.6

10 months ago

1.2.5

10 months ago

1.2.4

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.0

10 months ago

1.1.0

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago