Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixed response handling.
  • Loading branch information
DarkGhostHunter committed Jul 10, 2020
2 parents 1c7ea1c + 3af7158 commit 7ef7f8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,10 @@ Yes. Just be sure to disable the password column in the users table, the Passwor

* **Does this encodes/decode the strings automatically in the frontend?**

No, you must ensure to encode/decode to binary forms some strings in your frontend because the nature of WebAuthn. The included [WebAuthn Helper](#5-frontend-integration) does it automatically for you.
Yes, the included [WebAuthn Helper](#5-frontend-integration) does it automatically for you.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2020 Laravel LLC.
Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2020 Laravel LLC.
23 changes: 15 additions & 8 deletions resources/js/larapass.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,26 @@ class Larapass
/**
* Handles the response from the Server.
*
* Throws the response if is not OK (HTTP 2XX).
* Throws the entire response if is not OK (HTTP 2XX).
*
* @param response {Response}
* @returns Response
* @returns Promise<JSON|ReadableStream>
* @throws Response
*/
static #handleResponse(response)
{
if (response.ok) {
return response;
if (! response.ok) {
throw response;
}

throw response;
// Here we will do a small trick. Since most of the responses from the server
// are JSON, we will automatically parse the JSON body from the response. If
// it's not JSON, we will push the body verbatim and let the dev handle it.
return new Promise(resolve => {
response.json()
.then(json => resolve(json))
.catch(() => resolve(response.body))
})
}

/**
Expand All @@ -251,7 +258,7 @@ class Larapass
*
* @param data {{string}}
* @param headers {{string}}
* @returns {Promise<Response>}
* @returns Promise<any>
*/
async login(data = {}, headers = {})
{
Expand All @@ -272,7 +279,7 @@ class Larapass
*
* @param data {{string}}
* @param headers {{string}}
* @returns {Promise<Response>}
* @returns Promise<any>
*/
async register(data = {}, headers = {})
{
Expand All @@ -285,4 +292,4 @@ class Larapass
return await this.#fetch(publicKeyCredential, this.routes.register, headers)
.then(Larapass.#handleResponse);
}
}
}

0 comments on commit 7ef7f8e

Please sign in to comment.