VRATIX Logo

βœ…

Automatic Reuse Detection

NEW

βœ…

Username and Password Authentication

βœ…

JWT Access Tokens

βœ…

Refresh Tokens

βœ…

Reply Attack Protection

πŸ—οΈ

HTTP Cookie Sessions

About

The Auth Basic Module provides essential authentication functionality for backend services. It supports quick setup for user signup and login with username and password, along with session management. As a core module, it enables secure access to routes and serves as the foundation for many other API modules.

Installation

To add the Auth Basic Module to your project, run:

npx vratix add auth-basic

.env

Add the following environment variables to your .env file:

  • JWT_SECRET_KEY: Secret key used for signing and verifying JWTs
    • Default: None (required)
    • Example: JWT_SECRET_KEY=your-secret-key
  • JWT_ISSUER: Issuer identifier for JWTs to ensure they’re issued by your backend
    • Default: None (required)
    • Example: JWT_ISSUER=com.yourdomain

Usage

Import the router from @/routes/auth.js in your main entry point file (e.g., server.ts):

import { router as authRouter } from "@/routes/auth.js";
 
app.use("/api/auth", authRouter);  

Middleware

This module provides a protectedRoute middleware to secure endpoints, requiring a valid JWT access token for access.

import { protectedRoute } from "@/middleware/jwt";
 
router.post("/upload/:fileName", protectedRoute, async (req, res, next) => {
  ...
});

Endpoints

The Auth Basic Module exposes the following endpoints:

MethodEndpointDescription
POST/signupCreates a new user account and returns session tokens
POST/loginAuthenticates the user and returns session tokens
POST/refresh-tokenIssues a new JWT access token and rotates the refresh token

Errors

Below are common errors with solutions for this module:

Error CodeNameSolution
409UsernameNotAvailableEnsure username is unique
409InvalidLoginCredentialsVerify the credentials are correct
403ForbiddenErrorUser attempted to access a protected route with an invalid token
500JWTEnvVariableMissingVerify .env file configuration

Examples

To explore sample requests and responses, download our Postman collection: