1.0.1 • Published 10 months ago

oneclickauth-backend v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

OneClickAuth-backend

OneClickAuth-backend is a plug-and-play backend solution for authentication and user management in your React applications. It integrates with MongoDB and provides seamless JWT-based authentication with minimal setup.

Features

  • Easy setup for authentication
  • JWT-based user management
  • MongoDB integration
  • Simple environment variable configuration

Installation

To install OneClickAuth into your React project, run the following command:

npm install oneclickauth-backend

Basic Setup

1. Create index.js:

In your project’s root directory, create a file named index.js and paste the code below:

import { createServer } from "oneclickauth-backend";
import dotenv from "dotenv";

dotenv.config();

// MongoDB connection URI from environment variables
const mongoUri = process.env.MONGODB_URI;

// Port for the backend server (default: 3000)
const port = 3000; // Customize the port if needed

// Start the server
createServer(mongoUri, port);

2. Environment Variables:

Create a .env file in the root directory of your project and add the following variables:

MONGODB_URI = Your_MongoDB_Connection_URI
JWT_SECRET = Your_Random_Secret_String
  • MONGODB_URI: Your MongoDB connection string.
  • JWT_SECRET: A random string used to sign and verify JWT tokens. Make sure to use a strong, unpredictable secret.

3. Run the Backend:

Once you've set up the index.js file and the .env configuration, start the backend by running the following command in the terminal:

node index.js

The server will start on the specified port (default: 3000) and connect to MongoDB using the provided URI.

API Routes

  • POST /backend/auth/signup: Register a new user.
  • POST /backend/auth/signin: Log in with email and password.
  • GET /backend/auth/signout: Log out the user.
  • POST /backend/user/update/${currentUser._id}: Update the User.
  • DELETE /backend/user/delete/${currentUser._id}: Delete the User.

Handling CORS or Proxy Issues

If you encounter CORS or proxy-reated issues while accessing the backend from the frontend, follow the steps below:

For React Apps

Add the following proxy configuration in your package.json:

"proxy": "http://localhost:3000" // Customize the port if needed

For Vite Apps

Add the following configuration to your vite.config.js:

// vite.config.js
export default {
  server: {
    proxy: {
      // Proxy API requests starting with /backend to the Express server
      '/backend': {
        target: 'http://localhost:3000', // Customize the port if needed
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/backend/, '/backend'),
      },
    },
  },
};

License

MIT License

1.0.1

10 months ago

1.0.0

10 months ago