diff --git a/packages/shopware-6-client/__tests__/services/customerService.spec.ts b/packages/shopware-6-client/__tests__/services/customerService.spec.ts new file mode 100644 index 000000000..9ad3da65b --- /dev/null +++ b/packages/shopware-6-client/__tests__/services/customerService.spec.ts @@ -0,0 +1,45 @@ +import { CustomerService } from "../../src/index"; + +const loginCredentials = { + username: "mkucmus@divante.com", + password: "test123456" +}; + +describe("CustomerService", () => { + describe("login", () => { + it("should return context token", async () => { + try { + const result = await CustomerService.login(loginCredentials); + expect(result).toHaveProperty("sw-context-token"); + } catch (e) { + console.error("Connection problem", e); + } + }); + it("should not return context token when the credentials are wrong", async () => { + try { + const result = await CustomerService.login({ + username: loginCredentials.username, + password: "wrong-credentials" + }); + expect(result).toHaveProperty("sw-context-token"); + } catch (e) { + e.response && expect(e.response.status).toEqual(401); + } + }); + }); + describe("logout", () => { + it("should return no data and throw no exceotion (204)", async () => { + try { + const contextTokenResult = await CustomerService.login( + loginCredentials + ); + const result = await CustomerService.logout( + contextTokenResult["sw-context-token"] + ); + expect(result).toBeFalsy(); + } catch (e) { + console.error("Connection problem", e); + } + }); + }); +}); diff --git a/packages/shopware-6-client/src/endpoints.ts b/packages/shopware-6-client/src/endpoints.ts index 21a73b141..e04efbe63 100644 --- a/packages/shopware-6-client/src/endpoints.ts +++ b/packages/shopware-6-client/src/endpoints.ts @@ -1 +1,3 @@ export const CATEGORY_ENDPOINT = "/category"; + +export const CUSTOMER_ENDPOINT = "/customer"; diff --git a/packages/shopware-6-client/src/index.ts b/packages/shopware-6-client/src/index.ts index a0e836563..808025ed5 100644 --- a/packages/shopware-6-client/src/index.ts +++ b/packages/shopware-6-client/src/index.ts @@ -1,2 +1,3 @@ export { setup, config } from "./settings"; export { Category, CategoryService } from "./categoryService"; +export { CustomerService } from "./services/customerService"; diff --git a/packages/shopware-6-client/src/services/customerService.ts b/packages/shopware-6-client/src/services/customerService.ts new file mode 100644 index 000000000..eeeb6c9fc --- /dev/null +++ b/packages/shopware-6-client/src/services/customerService.ts @@ -0,0 +1,61 @@ +import axios from "axios"; +import { config } from "../settings"; +import { CUSTOMER_ENDPOINT } from "../endpoints"; + +interface CustomerLoginParam { + username: string; + password: string; +} + +interface CustomerLoginResponse { + "sw-context-token": string; +} + +/** + * Usage example: + * ```ts + * import { CustomerService } from '@shopware-pwa/shopware-6-client' + * ``` + */ +export interface CustomerService { + login: (params: CustomerLoginParam) => Promise; + logout: (contextToken?: string) => Promise; +} + +/** + * @description Get the context token for current user + */ +const login = async function( + params: CustomerLoginParam +): Promise { + const resp = await axios.post( + `${config.endpoint}${CUSTOMER_ENDPOINT}/login`, + params + ); + return resp.data; +}; + +/** + * @description End up the session + */ +const logout = async function(contextToken?: string): Promise { + const resp = await axios.post( + `${config.endpoint}${CUSTOMER_ENDPOINT}/logout`, + null, + { + headers: { + /** TODO: move into different layer if created */ + "sw-context-token": contextToken + } + } + ); + return resp.data; +}; + +/** + * @description Expose public methods of the service + */ +export const CustomerService: CustomerService = { + login, + logout +};