-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'issue-241' of https://github.com/gustavogularte/pet-dex…
…-frontend into issue-241
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}, | ||
}; |