op-db logo

OP-DB

docsapigithub

Introduction -

Note : This setup strictly uses cloudinary and mongodb. Both are trustworthy, secure and have a free teir. You can opt not to use cloudinary but you might face some errors with some endpoints.

After the npm install you get a op-db in the node modules which consists of all the code.

Basically what you get is a very simple and well planned backend code written in express js , mongoose and using cloudinary and mongodb. Perfect for your backend needs.

For detailed reference see the installation section for installation and api section for all available api details and endpoints !

Let's start by adding basic signup and login/logout functionality.

With just a few steps, you can set up authentication for your project!

  1. From the frontend, make a POST request to http://localhost:8000/api/user/register.

    Replace localhost:8000 with your server URL.

  2. From the frontend, make a POST request to http://localhost:8000/api/user/login.

    Replace localhost:8000 with your server URL.

  3. From the frontend, make a POST request to http://localhost:8000/api/user/logout.

    Replace localhost:8000 with your server URL.

These endpoints, once connected to the frontend, will provide the authentication feature.

Register/Signup user request !

async function registerUser() {
  const url = 'http://localhost:8000/api/user/register';
  const userData = {
    username: 'exampleUsername',
    fullname: 'Example Fullname',
    email: 'example@example.com',
    password: 'examplePassword',
    project: 'exampleProject',
  };
  try {
      const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(userData),
      credentials: 'include', // This ensures cookies are included in the request
    });
    if (!response.success) {
      throw new Error(`Error: ${response.message}`);
    }
    const data = await response.json();
    console.log('Success:', data);
  } catch (error) {
    console.error('Error:', error);
  }
}
registerUser();

Login user request !

async function loginUser() {
  const url = 'http://localhost:8000/api/user/login';
  const userData = {
    username: 'exampleUsername',
    password: 'examplePassword',
  };
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(userData),
      credentials: 'include', // This ensures cookies are included in the request
    });
    if (!response.success) {
      throw new Error(`Error: ${response.message}`);
    }
    const data = await response.json();
    console.log('Success:', data);
  } catch (error) {
    console.error('Error:', error);
  }
}
// Call the function to test
loginUser();

Logout user request !

async function logoutUser() {
  const url = 'http://localhost:8000/api/user/logout';
  try {
    const response = await fetch(url, {
      method: 'POST',
      credentials: 'include', // This ensures cookies are included in the request
    });
    if (!response.success) {
      throw new Error(`Error: ${response.message}`);
    }
    const data = await response.json();
    console.log('Success:', data);
  } catch (error) {
    console.error('Error:', error);
  }
}
// Call the function to test
logoutUser();

Similarly -

You can refer to the api section for all the api options and endpoints and setup all your application !

Your database is setup now connecting the apis will take any of your application a step ahead. An make them complete !