Skip to content

Commit

Permalink
Merge branch 'issue-241' of https://github.com/gustavogularte/pet-dex…
Browse files Browse the repository at this point in the history
…-frontend into issue-241
  • Loading branch information
gustavogularte committed Jul 18, 2024
2 parents 68c3e65 + 82cec30 commit 0aac5fa
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/services/login/loginService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function errorResponse(error) {
return {
success: false,
message: error.message,
};
}

function successResponse(data) {
return {
success: true,
token: data.token,
};
}

async function processLogin(email, password) {
const url = 'http://localhost:3000/login';
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
};

const response = await fetch(url, options);
if (!response.ok) throw new Error('Erro ao fazer o login');

const data = await response.json();
return successResponse(data);
}

export const LoginService = {
login: async (email, password) => {
try {
return await processLogin(email, password);
} catch (error) {
return errorResponse(error);
}
},
};

0 comments on commit 0aac5fa

Please sign in to comment.