You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
905 B

const fecher = async (url: string, token: string) => {
return await fetch(url, {
method: 'POST',
mode: 'cors',
headers: {
'Authorization' :'Basic '+token
},
});
}
export const postBasicAuth = async (url: string, username:string, password: string) => {
//url= process.env.API_URL+url;
const token = Buffer.from(username + ':' + password).toString('base64');
let data;
let error;
try{
const response = await fecher(url, token)
if (response.ok){
const token = response.headers.get('Authorization') ;
if (token){
data = token.replaceAll("Basic ","")
}
}else {
const trace = await response.json();
error = "Servidor: "+ trace.message;
}
}catch (trace){
error = "Cliente: "+trace;
}
return {
data,
error
}
}