Skip to content

A secure authentication API that uses JWT (JSON Web Tokens) for user authentication and CSRF protection to safeguard against malicious attacks. It employs cookie-based authentication for session management, providing a robust and secure way to handle user login and session persistence.

License

Notifications You must be signed in to change notification settings

ShaNaim/auth-flow-api

Repository files navigation

🔐AuthFlow API

The auth-flow-api is a TypeScript-based Express application designed for building an Authentication. It uses modern JavaScript and TypeScript

features, with development tools for linting, formatting, and maintaining code quality.

Prerequisites 🧾

  • Node.js: v20.16.0

  • npm: >=10.8.2

  • Ensure that Node.js and npm versions match the requirements specified in the engines section. Use tools like nvm (Node Version Manager) for

managing Node.js versions.

Installation 📦

  1. Clone the repository:
git  clone https://github.com/ShaNaim/auth-flow.git
cd  auth-flow
  1. Install dependencies: Make sure to install both the development and production dependencies:
npm  install
  1. Environment Setup:
  • Create a .env file in the root of your project with the necessary environment variables. Refer to .env.example if available.

  • Use the envalid package to ensure that your environment variables are properly validated.

  1. Database Setup:

    • Install PostgreSQL: If PostgreSQL is not installed on your machine, follow the instructions for your operating system:

      • On macOS:
        brew install postgresql
      • On Ubuntu:
        sudo apt update
        sudo apt install postgresql postgresql-contrib
    • Configure PostgreSQL: After installation, start PostgreSQL and create a new database:

      # Start PostgreSQL service
      sudo service postgresql start
      # Open PostgreSQL prompt
      sudo -u postgres psql
      # Create a new database for AuthFlow
      CREATE DATABASE AuthFlow;
    • Configure Prisma:

      • Set DATABASE_URL in the .env file: Replace the DATABASE_URL in your .env with the connection URL for PostgreSQL. For example: env DATABASE_URL="postgresql://user:password@localhost:5432/auth-flow"
      • Run Prisma Migrations: After configuring the database, run Prisma migrations to create the necessary tables:
        npx prisma migrate dev --name init
        This will apply the initial database schema based on your schema.prisma.
  2. Build the project: Compile the TypeScript files into JavaScript:

    npm  run  build
  3. Start the server: After building the project, start the server using:

    npm  start
  4. Run in Development Mode: To run the server in development mode with automatic reloads using nodemon and ts-node, use:

    npm  run  dev

Usage 🛫

  • Development Server: Running npm run dev will start the server with hot reloading, making it easier for development.

  • Production Build: To create a production build, use npm run build and then npm start to run the compiled JavaScript files.

  • Linting: Check code quality using ESLint:

    npm  run  lint
  • Code Formatting: Format the codebase with Prettier:

    npm  run  format
  • Clean Build Artifacts: To remove the dist directory (where the compiled JavaScript files are stored):

    npm  run  clean

Project Structure 🏗️

  • src/: Main source folder containing the TypeScript files.

  • dist/: Compiled JavaScript files are output here after running npm run build.

  • @config, @middlewares, @modules, @utils, etc.: These are module aliases to simplify imports in your TypeScript code.

Contributing 🤝

We welcome contributions! To contribute, follow these steps:

  1. Fork the repository: Click the "Fork" button at the top of the GitHub repository.

  2. Clone your fork:

    git  clone  https://github.com/ShaNaim/auth-flow.git
    cd  auth-flow
  3. Create a new branch: Make sure to give your branch a descriptive name:

    git  checkout  -b  feature/your-feature-name
  4. Make your changes.

  5. Run tests and lint: Make sure your code passes linting and any tests before submitting a pull request:

    npm  run  lint
    npm  test
  6. Commit and push your changes:

    git  add  .
    git  commit  -m  "Add a description of your changes"
    git  push  origin  feature/your-feature-name
  7. Create a pull request: Go to your forked repository on GitHub, and click the "New pull request" button.

Additional Information

  • Lint-Staged: This project uses lint-staged to automatically run eslint and prettier on staged files before committing, ensuring consistent code quality.

  • Module Aliases: Use the aliases defined in _moduleAliases to simplify imports. For example:

import { someUtil } from '@utils/someUtil';
  • RSA Token Pair Generator: rsa-token-pair-generator is an RSA token generator required for JWT authentication. This package follows the structure necessary for this project by default. npm version

Troubleshooting

  • If you encounter issues with the Node version, ensure that you are using atleas v20.16.0. Use nvm to manage Node versions:
nvm  install  20.16.0
nvm  use  20.16.0
  • Make sure that .env is properly configured with the required environment variables, as missing variables can cause the server to fail on startup.

This guide covers the essential steps to get started with auth-flow-api, as well as details on contributing and maintaining code quality. Happy

coding!

License 📄

MIT License - See LICENSE for full text.


About

A secure authentication API that uses JWT (JSON Web Tokens) for user authentication and CSRF protection to safeguard against malicious attacks. It employs cookie-based authentication for session management, providing a robust and secure way to handle user login and session persistence.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published