Skip to content

Commit

Permalink
feat(customer): add login and logout methods (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Kucmus committed Oct 15, 2019
1 parent cb23c1a commit 732a726
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
});
2 changes: 2 additions & 0 deletions packages/shopware-6-client/src/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const CATEGORY_ENDPOINT = "/category";

export const CUSTOMER_ENDPOINT = "/customer";
1 change: 1 addition & 0 deletions packages/shopware-6-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { setup, config } from "./settings";
export { Category, CategoryService } from "./categoryService";
export { CustomerService } from "./services/customerService";
61 changes: 61 additions & 0 deletions packages/shopware-6-client/src/services/customerService.ts
Original file line number Diff line number Diff line change
@@ -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<CustomerLoginResponse>;
logout: (contextToken?: string) => Promise<null>;
}

/**
* @description Get the context token for current user
*/
const login = async function(
params: CustomerLoginParam
): Promise<CustomerLoginResponse> {
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<null> {
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
};

0 comments on commit 732a726

Please sign in to comment.