summaryrefslogtreecommitdiff
path: root/src/clients
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-05-11 19:28:47 -0600
committerHombreLaser <sebastian-440@live.com>2023-05-11 19:28:47 -0600
commit385606ee05a8ceb9073169639eb1a311f81cac10 (patch)
treeac8af50a15ce3e834009afef773988213c961cdb /src/clients
parentab540055c99074c1c67fd65b45d0afb785ca5a0b (diff)
Añade información a la vista de usuario
Diffstat (limited to 'src/clients')
-rw-r--r--src/clients/api_client.ts33
-rw-r--r--src/clients/loaders.ts15
2 files changed, 37 insertions, 11 deletions
diff --git a/src/clients/api_client.ts b/src/clients/api_client.ts
index 7d2cf34..fc2d361 100644
--- a/src/clients/api_client.ts
+++ b/src/clients/api_client.ts
@@ -1,11 +1,38 @@
-import axios from "axios";
+import axios, { AxiosResponse } from "axios";
+import Token from "../lib/token";
export class ApiClient {
readonly url = "http://localhost:3000/api";
+ token = new Token();
- async get(path: string, params?: URLSearchParams, headers?: object) {
+ async authenticatedGet(path: string) {
const request_url = `${ this.url }${ path }`;
- const response = await this.makeGetRequest(request_url, headers);
+ let request: any;
+ let options = {
+ headers: {
+ Authorization: this.token.get()
+ }
+ };
+
+ request = await this.makeGetRequest(request_url, options);
+
+ if(request.response) {
+ // Let's try with a refreshed token.
+ this.token.refresh()
+ options = {
+ headers: {
+ Authorization: this.token.getRefresh()
+ }
+ };
+ request = await this.makeGetRequest(request_url, options);
+ }
+
+ return request;
+ }
+
+ async get(path: string) {
+ const request_url = `${ this.url }${ path }`;
+ const response = await this.makeGetRequest(request_url);
return response;
}
diff --git a/src/clients/loaders.ts b/src/clients/loaders.ts
index 2af7770..d9972d5 100644
--- a/src/clients/loaders.ts
+++ b/src/clients/loaders.ts
@@ -12,17 +12,16 @@ export async function loader({ request }) {
export async function accountLoader() {
const client = new ApiClient();
- const token = new Token();
+
+ const account_response = await client.authenticatedGet("/account");
+ const addresses_response = await client.authenticatedGet("/account/addresses");
+ const cards_response = await client.authenticatedGet("/account/cards");
- if(!token.present())
+ // Authentication error handling.
+ if(account_response.response || addresses_response.response || cards_response.response)
return redirect("/products");
- const headers = {
- "Authentication": `Bearer ${ token.get() }`
- };
-
- const response = await client.get("/account", undefined, headers);
- return response;
+ return [account_response, addresses_response, cards_response];
}
export async function productLoader({ params }) {