-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(customer): add login and logout methods (WIP)
- Loading branch information
Maciej Kucmus
committed
Oct 15, 2019
1 parent
cb23c1a
commit 732a726
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/shopware-6-client/__tests__/services/customerService.spec.ts
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,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); | ||
} | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export const CATEGORY_ENDPOINT = "/category"; | ||
|
||
export const CUSTOMER_ENDPOINT = "/customer"; |
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 |
---|---|---|
@@ -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
61
packages/shopware-6-client/src/services/customerService.ts
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,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 | ||
}; |