1.0.5 • Published 12 months ago

auth-jwt-mw v1.0.5

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
12 months ago

Middleware Auth Module

A middleware module for handling authentication using JWT tokens. This module includes functionalities for user registration, login, logout, and middleware for protecting routes with JWT token validation.

Features

  • User registration
  • User login with JWT token issuance
  • User logout
  • Middleware for protecting routes with JWT token validation

Installation

To install the module, run:

npm install auth-jwt-mw

Setup

  1. Create a .env file in the root of your project and add the following environment variable:

    VITE_API_URL=http://your-api-url.com/api/auth
  2. Load the environment variables in your main application file (e.g., server.ts):

    import { config as dotenvConfig } from 'dotenv';
    
    dotenvConfig();

Usage

Middleware Setup

Import and use the middleware in your Express application:

import express from 'express';
import { authGuard, register, login } from 'auth-jwt-mw';

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

app.post('/register', async (req, res) => {
  try {
    const response = await register(req.body);
    res.status(200).send(response);
  } catch (error) {
    res.status(400).send(error.message);
  }
});

app.post('/login', async (req, res) => {
  try {
    const response = await login(req.body);
    res.status(200).send(response);
  } catch (error) {
    res.status(400).send(error.message);
  }
});

app.post('/protected', authGuard, (req, res) => {
  res.status(200).send('This is a protected route');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

Configuration

The configuration for the module is handled via environment variables. Ensure you have a .env file with the necessary variables:

VITE_API_URL=http://your-api-url.com/api/auth

Testing

To run tests, make sure you have Jest installed and configured. You can run tests with:

npm test

License

This project is licensed under the MIT License.

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago