Skip to content

Commit

Permalink
feat: login service (#284)
Browse files Browse the repository at this point in the history
* feat: login service

* refactor: import da url e remoção das functions

* refactor: mensagem de erro em inglês e desestruturação do token

* refactor: headers removed

* feat: login mock-api

* refactor: movido login para userService

* refactor: code style
  • Loading branch information
gustavogularte authored Jul 24, 2024
1 parent 1852a4e commit b61ba61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions db.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"login": [
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Ijc3OTFkZmMwLWY3YzEtNDAyMy05ZDUxLTFlYTQ3OWQ2ZjgyYiIsIm5hbWUiOiJqb2huZG9lQGV4YW1wbGUuY29tIiwiZW1haWwiOiJqb2huZG9lQGV4YW1wbGUuY29tIiwiZXhwIjoxNzIxODI4MDA3fQ.Agk6Yz2E6TpoqI6mVYeClKx1zOxSsp TyIhtJEvFvG-E"
}
],
"user": [
{
"id": "1",
Expand Down
22 changes: 22 additions & 0 deletions src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,26 @@ export const UserService = {
return `Ocorreu o seguinte erro: ${error}`;
}
},
login: async (email, password) => {
try {
const response = await fetch(`${url}/login`, {
method: 'POST',
body: JSON.stringify({ email, password }),
});

if (!response.ok) throw new Error('Failed to login');

const { token } = await response.json();
return {
success: true,
token,
};
} catch (error) {
console.error(`An error occurred: ${error}`);
return {
success: false,
message: error.message,
};
}
},
};

0 comments on commit b61ba61

Please sign in to comment.