-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path05-slas-helper.ts
More file actions
97 lines (87 loc) · 3.1 KB
/
05-slas-helper.ts
File metadata and controls
97 lines (87 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/**
* Examples on how to use the SLAS helpers using a public client:
* - Get authorization token for a guest user
* - Get authorization token for a registered user
* - Get a new authorization token using refresh token
* - Logout
* Usage: ts-node examples/05-slas-helper.ts
* For more information, see (Shopper Login and API Access Service)[https://developer.salesforce.com/docs/commerce/commerce-api/guide/slas-public-client.html].
*/
import * as CommerceSdk from "commerce-sdk";
const { slasHelpers, Customer } = CommerceSdk;
// demo client credentials, if you have access to your own please replace them below.
const CLIENT_ID = "1d763261-6522-4913-9d52-5d947d3b94c4";
const ORG_ID = "f_ecom_zzte_053";
const SHORT_CODE = "kv7kzm78";
const SITE_ID = "RefArch";
// EDIT HERE: Fill in with shopper credentials. Examples on how to register a shopper can be found in 04-register-shopper.ts
const shopper = {
username: "<insert_username>",
password: "<insert_password>", // do not store password as plaintext. Store it in a secure location.
};
// must be registered in SLAS. On server, redirectURI is never called
const redirectURI = "http://localhost:3000/callback";
// client configuration parameters
const clientConfig = {
parameters: {
clientId: CLIENT_ID,
organizationId: ORG_ID,
shortCode: SHORT_CODE,
siteId: SITE_ID,
},
};
const slasClient = new Customer.ShopperLogin(clientConfig);
// GUEST LOGIN
const guestTokenResponse = await slasHelpers
.loginGuestUser(slasClient, { redirectURI })
.then((guestTokenResponse) => {
console.log("Guest Token Response: ", guestTokenResponse);
return guestTokenResponse;
})
.catch((error) =>
console.log("Error fetching token for guest login: ", error)
);
// REGISTERED B2C USER LOGIN
slasHelpers
.loginRegisteredUserB2C(
slasClient,
{ username: shopper.username, password: shopper.password },
{ redirectURI }
)
.then((registeredUserTokenResponse) => {
console.log(
"Registered User Token Response: ",
registeredUserTokenResponse
);
return registeredUserTokenResponse;
})
.catch((error) =>
console.log("Error fetching token for registered user login: ", error)
);
// REFRESH TOKEN
const refreshTokenResponse = await slasHelpers
.refreshAccessToken(slasClient, {
refreshToken: guestTokenResponse.refresh_token,
})
.then((refreshTokenResponse) => {
console.log("Refresh Token Response: ", refreshTokenResponse);
return refreshTokenResponse;
})
.catch((error) => console.log("Error refreshing token: ", error));
// LOGOUT
slasHelpers
.logout(slasClient, {
accessToken: refreshTokenResponse.access_token,
refreshToken: refreshTokenResponse.refresh_token,
})
.then((logoutTokenResponse) => {
console.log("Logout Token Response: ", logoutTokenResponse);
return logoutTokenResponse;
})
.catch((error) => console.log("Error with logout: ", error));