import socket from "./httpDomainName"; /** * this is used to send the data collected from * the signup form to the backend * in turn it creates the user * returns a return status with true if created * false if there was an error if any kind */ export async function createUserNewAdmin(signUpObject = {}) { // returns a promise we use await to get the response body using JSON() const response = await fetch(`${socket}/open/signup`, { method: "POST", body: JSON.stringify(signUpObject), headers: { "content-type": "application/json", }, }); const resData = await response.json(); // console.log("response data"); // console.log(resData); return resData; } /** * this function is used to log-in * it uses the basic auth log-in technique * if successful a JWT token will be returned from the backend * use the JWT token for every subsequent request */ //user login data // export async function userLogin(username, password) { // //make an http get request for userLogin login // const loginResp = await fetch(`${socket}/open/login`, { // method: `GET`, // headers: basicAuthCredentialsBuilder(username, password), // credentials: "include", // <-- VERY IMPORTANT to get the JWT cookie from the backend // }); // const resData = await loginResp.json(); // console.log("response data"); // console.log(resData); // return loginResp; // } // //common basic auth header builder // function basicAuthCredentialsBuilder(username, password) { // //builds the appropriate format for basic auth // return { // Authorization: "Basic " + window.btoa(username + ":" + password), // }; // } export async function userLoginAndResp(username, password) { //make an http get request for userLogin login const auth = new authenticationBuilder(socket); return auth.userLogin(username, password); } /** * This class helps the user login */ class authenticationBuilder { #socketURL = ""; constructor(socketURL) { this.#socketURL = socketURL; } async userLogin(username, password) { //make an http get request for userLogin login const loginResp = await fetch(`${this.#socketURL}/open/login`, { method: `GET`, headers: this.#basicAuthCredentialsBuilder(username, password), credentials: "include", // <-- VERY IMPORTANT to get the JWT cookie from the backend }); const resData = await loginResp.json(); console.log("response data"); console.log(resData); return loginResp; } #basicAuthCredentialsBuilder(username, password) { //builds the appropriate format for basic auth return { Authorization: "Basic " + window.btoa(username + ":" + password), }; } }