@scayle/storefront-core 7.27.0 → 7.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,41 +3,28 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.updatePasswordByHash = exports.refreshAccessToken = exports.oauthRevokeToken = exports.oauthRegister = exports.oauthLogin = exports.oauthGuestLogin = exports.oauthForgetPassword = void 0;
7
- var _axios = _interopRequireDefault(require("axios"));
6
+ exports.updatePasswordByHash = exports.refreshAccessToken = exports.oauthRevokeToken = exports.oauthRegister = exports.oauthLogin = exports.oauthGuestLogin = exports.oauthForgetPassword = exports.convertErrorForRpcCall = void 0;
8
7
  var _jose = require("jose");
9
8
  var _constants = require("../../constants/index.cjs");
10
- var _rpc = require("../../utils/rpc.cjs");
11
- var _hash = require("../../utils/hash.cjs");
9
+ var _fetch = require("../../utils/fetch.cjs");
12
10
  var _user = require("../../utils/user.cjs");
13
11
  var _user2 = require("../../rpc/methods/user.cjs");
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
- class MissingCredentialsError extends Error {
16
- constructor() {
17
- super("[Token based Authentication] No credentials configured");
18
- this.name = "MissingCredentialsError";
12
+ var _oauth = require("../../api/oauth.cjs");
13
+ const convertErrorForRpcCall = (error, httpStatuses) => {
14
+ if (error instanceof _fetch.FetchError && httpStatuses.includes(error.response.status)) {
15
+ return error;
19
16
  }
20
- }
21
- function getOAuthSettings(context) {
17
+ };
18
+ exports.convertErrorForRpcCall = convertErrorForRpcCall;
19
+ function getOAuthClient(context) {
22
20
  const clientId = context.oauth?.clientId ?? process.env.OAUTH_CLIENT_ID;
23
21
  const clientSecret = context.oauth?.clientSecret ?? process.env.OAUTH_CLIENT_SECRET;
24
22
  const apiHost = context.oauth?.apiHost ?? process.env.OAUTH_API_HOST;
25
- if (!clientId || !clientSecret) {
26
- throw new MissingCredentialsError();
27
- }
28
- const BASIC_AUTH_HASH = (0, _hash.encodeBase64)(`${clientId}:${clientSecret}`);
29
- const axiosInstance = _axios.default.create({
30
- baseURL: `${apiHost}/v1`,
31
- headers: {
32
- Authorization: `Basic ${BASIC_AUTH_HASH}`
33
- }
34
- });
35
- return {
23
+ return new _oauth.OAuthClient({
36
24
  clientId,
37
25
  clientSecret,
38
- apiHost,
39
- axiosInstance
40
- };
26
+ apiHost
27
+ });
41
28
  }
42
29
  const saveUserOnSession = async (accessToken, context) => {
43
30
  const checkoutUrl = context.checkout.url;
@@ -51,31 +38,32 @@ const saveUserOnSession = async (accessToken, context) => {
51
38
  });
52
39
  context.updateUser(user);
53
40
  };
41
+ async function postLogin(context, tokens) {
42
+ context.updateTokens({
43
+ accessToken: tokens.access_token,
44
+ refreshToken: tokens.refresh_token
45
+ });
46
+ await saveUserOnSession(tokens.access_token, context);
47
+ const {
48
+ customerId
49
+ } = (0, _jose.decodeJwt)(tokens.access_token);
50
+ await Promise.all([(0, _user.mergeBaskets)(context.sessionId, await context.generateBasketKeyForUserId(customerId), _constants.DEFAULT_WITH_LISTING, context), (0, _user.mergeWishlists)(context.sessionId, await context.generateWishlistKeyForUserId(customerId), _constants.DEFAULT_WITH_LISTING, context)]);
51
+ await context.createUserBoundSession();
52
+ }
54
53
  const oauthLogin = async (login, context) => {
55
54
  const shopId = context.shopId;
56
- const {
57
- axiosInstance
58
- } = getOAuthSettings(context);
55
+ const client = getOAuthClient(context);
59
56
  if (!login.email || !login.password) {
60
57
  throw new Error("Login or password are missing, seems like validation has failed");
61
58
  }
62
59
  try {
63
- const response = await axiosInstance.post("/auth/login", {
60
+ const tokens = await client.login({
64
61
  ...login,
65
62
  shop_id: shopId
66
63
  });
67
- context.updateTokens({
68
- accessToken: response.data.access_token,
69
- refreshToken: response.data.refresh_token
70
- });
71
- await saveUserOnSession(response.data.access_token, context);
72
- const {
73
- customerId
74
- } = (0, _jose.decodeJwt)(response.data.access_token);
75
- await Promise.all([(0, _user.mergeBaskets)(context.sessionId, await context.generateBasketKeyForUserId(customerId), _constants.DEFAULT_WITH_LISTING, context), (0, _user.mergeWishlists)(context.sessionId, await context.generateWishlistKeyForUserId(customerId), _constants.DEFAULT_WITH_LISTING, context)]);
76
- await context.createUserBoundSession();
64
+ await postLogin(context, tokens);
77
65
  } catch (error) {
78
- const err = (0, _rpc.convertErrorForRpcCall)(error, [_constants.HttpStatusCode.INTERNAL_SERVER_ERROR, _constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.NOT_FOUND]);
66
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.INTERNAL_SERVER_ERROR, _constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.NOT_FOUND]);
79
67
  if (err) {
80
68
  throw err;
81
69
  }
@@ -84,26 +72,15 @@ const oauthLogin = async (login, context) => {
84
72
  exports.oauthLogin = oauthLogin;
85
73
  const oauthRegister = async (register, context) => {
86
74
  const shopId = context.shopId;
87
- const {
88
- axiosInstance
89
- } = getOAuthSettings(context);
75
+ const client = getOAuthClient(context);
90
76
  try {
91
- const response = await axiosInstance.post("/auth/register", {
77
+ const tokens = await client.register({
92
78
  ...register,
93
79
  shop_id: shopId
94
80
  });
95
- context.updateTokens({
96
- accessToken: response.data.access_token,
97
- refreshToken: response.data.refresh_token
98
- });
99
- await saveUserOnSession(response.data.access_token, context);
100
- const {
101
- customerId
102
- } = (0, _jose.decodeJwt)(response.data.access_token);
103
- await Promise.all([(0, _user.mergeBaskets)(context.sessionId, await context.generateBasketKeyForUserId(customerId), _constants.DEFAULT_WITH_LISTING, context), (0, _user.mergeWishlists)(context.sessionId, await context.generateWishlistKeyForUserId(customerId), _constants.DEFAULT_WITH_LISTING, context)]);
104
- await context.createUserBoundSession();
81
+ await postLogin(context, tokens);
105
82
  } catch (error) {
106
- const err = (0, _rpc.convertErrorForRpcCall)(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.CONFLICT]);
83
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.CONFLICT]);
107
84
  if (err) {
108
85
  throw err;
109
86
  }
@@ -112,26 +89,15 @@ const oauthRegister = async (register, context) => {
112
89
  exports.oauthRegister = oauthRegister;
113
90
  const oauthGuestLogin = async (guest, context) => {
114
91
  const shopId = context.shopId;
115
- const {
116
- axiosInstance
117
- } = getOAuthSettings(context);
92
+ const client = getOAuthClient(context);
118
93
  try {
119
- const response = await axiosInstance.post("/auth/login/guest", {
94
+ const tokens = client.guestLogin({
120
95
  ...guest,
121
96
  shop_id: shopId
122
97
  });
123
- context.updateTokens({
124
- accessToken: response.data.access_token,
125
- refreshToken: response.data.refresh_token
126
- });
127
- await saveUserOnSession(response.data.access_token, context);
128
- const {
129
- customerId
130
- } = (0, _jose.decodeJwt)(response.data.access_token);
131
- await Promise.all([(0, _user.mergeBaskets)(context.sessionId, await context.generateBasketKeyForUserId(customerId), _constants.DEFAULT_WITH_LISTING, context), (0, _user.mergeWishlists)(context.sessionId, await context.generateWishlistKeyForUserId(customerId), _constants.DEFAULT_WITH_LISTING, context)]);
132
- await context.createUserBoundSession();
98
+ await postLogin(context, tokens);
133
99
  } catch (error) {
134
- const err = (0, _rpc.convertErrorForRpcCall)(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.CONFLICT]);
100
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.CONFLICT]);
135
101
  if (err) {
136
102
  throw err;
137
103
  }
@@ -140,26 +106,24 @@ const oauthGuestLogin = async (guest, context) => {
140
106
  exports.oauthGuestLogin = oauthGuestLogin;
141
107
  const refreshAccessToken = async context => {
142
108
  const refreshToken = context.refreshToken;
143
- const {
144
- axiosInstance
145
- } = getOAuthSettings(context);
109
+ const client = getOAuthClient(context);
146
110
  if (!refreshToken) {
147
111
  throw new Error("No app refresh token provided");
148
112
  }
149
113
  try {
150
- const response = await axiosInstance.post("/oauth/token", {
114
+ const tokens = await client.refreshToken({
151
115
  grant_type: "refresh_token",
152
116
  refresh_token: refreshToken
153
117
  });
154
118
  context.updateTokens({
155
- accessToken: response.data?.access_token,
156
- refreshToken: response.data?.refresh_token
119
+ accessToken: tokens?.access_token,
120
+ refreshToken: tokens?.refresh_token
157
121
  });
158
122
  return {
159
123
  success: !!response.data
160
124
  };
161
125
  } catch (error) {
162
- const err = (0, _rpc.convertErrorForRpcCall)(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED]);
126
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED]);
163
127
  if (err) {
164
128
  throw err;
165
129
  }
@@ -171,22 +135,15 @@ const oauthRevokeToken = async context => {
171
135
  if (!accessToken) {
172
136
  throw new Error("No app oauth authentication credentials");
173
137
  }
174
- const decodedAccessToken = (0, _jose.decodeJwt)(accessToken);
175
- const {
176
- axiosInstance
177
- } = getOAuthSettings(context);
138
+ const client = getOAuthClient(context);
178
139
  await context.destroySession();
179
140
  try {
180
- await axiosInstance.delete(`/oauth/tokens/${decodedAccessToken.jti}`, {
181
- headers: {
182
- Authorization: `Bearer ${accessToken}`
183
- }
184
- });
141
+ await client.revokeToken(accessToken);
185
142
  return {
186
143
  result: true
187
144
  };
188
145
  } catch (error) {
189
- const err = (0, _rpc.convertErrorForRpcCall)(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.NOT_FOUND]);
146
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.NOT_FOUND]);
190
147
  if (err) {
191
148
  throw err;
192
149
  }
@@ -200,9 +157,7 @@ const oauthForgetPassword = async ({
200
157
  email
201
158
  }, context) => {
202
159
  const shopId = context.shopId;
203
- const {
204
- axiosInstance
205
- } = getOAuthSettings(context);
160
+ const client = getOAuthClient(context);
206
161
  try {
207
162
  const resetUrl = new URL(context.auth.resetPasswordUrl);
208
163
  if (!resetUrl.searchParams.has("hash")) {
@@ -210,7 +165,7 @@ const oauthForgetPassword = async ({
210
165
  } else {
211
166
  resetUrl.searchParams.set("hash", "{hash}");
212
167
  }
213
- await axiosInstance.post("/auth/password/send-reset-email", {
168
+ await client.sendPasswordResetEmail({
214
169
  email,
215
170
  reset_url: decodeURI(resetUrl.toString()),
216
171
  shop_id: shopId
@@ -219,7 +174,7 @@ const oauthForgetPassword = async ({
219
174
  success: true
220
175
  };
221
176
  } catch (error) {
222
- const err = (0, _rpc.convertErrorForRpcCall)(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.NOT_FOUND, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.FORBIDDEN]);
177
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.NOT_FOUND, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.FORBIDDEN]);
223
178
  if (err) {
224
179
  throw err;
225
180
  }
@@ -231,21 +186,19 @@ const oauthForgetPassword = async ({
231
186
  exports.oauthForgetPassword = oauthForgetPassword;
232
187
  const updatePasswordByHash = async (passwordHash, context) => {
233
188
  const shopId = context.shopId;
234
- const {
235
- axiosInstance
236
- } = getOAuthSettings(context);
189
+ const client = getOAuthClient(context);
237
190
  try {
238
- const response = await axiosInstance.put("/auth/password/update-by-hash", {
191
+ const tokens = await client.updatePasswordByHash({
239
192
  ...passwordHash,
240
193
  shop_id: shopId
241
194
  });
242
195
  context.updateTokens({
243
- accessToken: response.data.access_token,
244
- refreshToken: response.data.refresh_token
196
+ accessToken: tokens.access_token,
197
+ refreshToken: tokens.refresh_token
245
198
  });
246
199
  await context.createUserBoundSession();
247
200
  } catch (error) {
248
- const err = (0, _rpc.convertErrorForRpcCall)(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.NOT_ACCEPTABLE]);
201
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.NOT_ACCEPTABLE]);
249
202
  if (err) {
250
203
  throw err;
251
204
  }
@@ -1,7 +1,12 @@
1
- import { GuestRequest, LoginRequest, ParamRpcHandler, RegisterRequest, RpcHandler, SendResetPasswordEmailRequest, UpdatePasswordByHashRequest } from '../../types';
2
- export declare const oauthLogin: ParamRpcHandler<LoginRequest, undefined>;
3
- export declare const oauthRegister: ParamRpcHandler<RegisterRequest, undefined>;
4
- export declare const oauthGuestLogin: ParamRpcHandler<GuestRequest, undefined>;
1
+ import type { Optional } from 'utility-types';
2
+ import type { GuestRequest, LoginRequest, ParamRpcHandler, RegisterRequest, RpcHandler, SendResetPasswordEmailRequest, UpdatePasswordByHashRequest } from '../../types';
3
+ /**
4
+ * Ensure error is a fetch error with one of the status codes
5
+ */
6
+ export declare const convertErrorForRpcCall: (error: any, httpStatuses: Array<number>) => Error;
7
+ export declare const oauthLogin: ParamRpcHandler<Optional<LoginRequest, 'shop_id'>, undefined>;
8
+ export declare const oauthRegister: ParamRpcHandler<Optional<RegisterRequest, 'shop_id'>, undefined>;
9
+ export declare const oauthGuestLogin: ParamRpcHandler<Optional<GuestRequest, 'shop_id'>, undefined>;
5
10
  export declare const refreshAccessToken: RpcHandler<{
6
11
  success: boolean;
7
12
  }>;
@@ -1,36 +1,19 @@
1
- import axios from "axios";
2
1
  import { decodeJwt } from "jose";
3
2
  import { DEFAULT_WITH_LISTING, HttpStatusCode } from "../../constants/index.mjs";
4
- import { convertErrorForRpcCall } from "../../utils/rpc.mjs";
5
- import { encodeBase64 } from "../../utils/hash.mjs";
3
+ import { FetchError } from "../../utils/fetch.mjs";
6
4
  import { mergeBaskets, mergeWishlists } from "../../utils/user.mjs";
7
5
  import { fetchUser } from "../../rpc/methods/user.mjs";
8
- class MissingCredentialsError extends Error {
9
- constructor() {
10
- super("[Token based Authentication] No credentials configured");
11
- this.name = "MissingCredentialsError";
6
+ import { OAuthClient } from "../../api/oauth.mjs";
7
+ export const convertErrorForRpcCall = (error, httpStatuses) => {
8
+ if (error instanceof FetchError && httpStatuses.includes(error.response.status)) {
9
+ return error;
12
10
  }
13
- }
14
- function getOAuthSettings(context) {
11
+ };
12
+ function getOAuthClient(context) {
15
13
  const clientId = context.oauth?.clientId ?? process.env.OAUTH_CLIENT_ID;
16
14
  const clientSecret = context.oauth?.clientSecret ?? process.env.OAUTH_CLIENT_SECRET;
17
15
  const apiHost = context.oauth?.apiHost ?? process.env.OAUTH_API_HOST;
18
- if (!clientId || !clientSecret) {
19
- throw new MissingCredentialsError();
20
- }
21
- const BASIC_AUTH_HASH = encodeBase64(`${clientId}:${clientSecret}`);
22
- const axiosInstance = axios.create({
23
- baseURL: `${apiHost}/v1`,
24
- headers: {
25
- Authorization: `Basic ${BASIC_AUTH_HASH}`
26
- }
27
- });
28
- return {
29
- clientId,
30
- clientSecret,
31
- apiHost,
32
- axiosInstance
33
- };
16
+ return new OAuthClient({ clientId, clientSecret, apiHost });
34
17
  }
35
18
  const saveUserOnSession = async (accessToken, context) => {
36
19
  const checkoutUrl = context.checkout.url;
@@ -44,43 +27,40 @@ const saveUserOnSession = async (accessToken, context) => {
44
27
  );
45
28
  context.updateUser(user);
46
29
  };
30
+ async function postLogin(context, tokens) {
31
+ context.updateTokens({
32
+ accessToken: tokens.access_token,
33
+ refreshToken: tokens.refresh_token
34
+ });
35
+ await saveUserOnSession(tokens.access_token, context);
36
+ const { customerId } = decodeJwt(tokens.access_token);
37
+ await Promise.all([
38
+ mergeBaskets(
39
+ context.sessionId,
40
+ await context.generateBasketKeyForUserId(customerId),
41
+ DEFAULT_WITH_LISTING,
42
+ context
43
+ ),
44
+ mergeWishlists(
45
+ context.sessionId,
46
+ await context.generateWishlistKeyForUserId(customerId),
47
+ DEFAULT_WITH_LISTING,
48
+ context
49
+ )
50
+ ]);
51
+ await context.createUserBoundSession();
52
+ }
47
53
  export const oauthLogin = async (login, context) => {
48
54
  const shopId = context.shopId;
49
- const { axiosInstance } = getOAuthSettings(context);
55
+ const client = getOAuthClient(context);
50
56
  if (!login.email || !login.password) {
51
57
  throw new Error(
52
58
  "Login or password are missing, seems like validation has failed"
53
59
  );
54
60
  }
55
61
  try {
56
- const response = await axiosInstance.post(
57
- "/auth/login",
58
- {
59
- ...login,
60
- shop_id: shopId
61
- }
62
- );
63
- context.updateTokens({
64
- accessToken: response.data.access_token,
65
- refreshToken: response.data.refresh_token
66
- });
67
- await saveUserOnSession(response.data.access_token, context);
68
- const { customerId } = decodeJwt(response.data.access_token);
69
- await Promise.all([
70
- mergeBaskets(
71
- context.sessionId,
72
- await context.generateBasketKeyForUserId(customerId),
73
- DEFAULT_WITH_LISTING,
74
- context
75
- ),
76
- mergeWishlists(
77
- context.sessionId,
78
- await context.generateWishlistKeyForUserId(customerId),
79
- DEFAULT_WITH_LISTING,
80
- context
81
- )
82
- ]);
83
- await context.createUserBoundSession();
62
+ const tokens = await client.login({ ...login, shop_id: shopId });
63
+ await postLogin(context, tokens);
84
64
  } catch (error) {
85
65
  const err = convertErrorForRpcCall(error, [
86
66
  HttpStatusCode.INTERNAL_SERVER_ERROR,
@@ -95,36 +75,13 @@ export const oauthLogin = async (login, context) => {
95
75
  };
96
76
  export const oauthRegister = async (register, context) => {
97
77
  const shopId = context.shopId;
98
- const { axiosInstance } = getOAuthSettings(context);
78
+ const client = getOAuthClient(context);
99
79
  try {
100
- const response = await axiosInstance.post(
101
- "/auth/register",
102
- {
103
- ...register,
104
- shop_id: shopId
105
- }
106
- );
107
- context.updateTokens({
108
- accessToken: response.data.access_token,
109
- refreshToken: response.data.refresh_token
80
+ const tokens = await client.register({
81
+ ...register,
82
+ shop_id: shopId
110
83
  });
111
- await saveUserOnSession(response.data.access_token, context);
112
- const { customerId } = decodeJwt(response.data.access_token);
113
- await Promise.all([
114
- mergeBaskets(
115
- context.sessionId,
116
- await context.generateBasketKeyForUserId(customerId),
117
- DEFAULT_WITH_LISTING,
118
- context
119
- ),
120
- mergeWishlists(
121
- context.sessionId,
122
- await context.generateWishlistKeyForUserId(customerId),
123
- DEFAULT_WITH_LISTING,
124
- context
125
- )
126
- ]);
127
- await context.createUserBoundSession();
84
+ await postLogin(context, tokens);
128
85
  } catch (error) {
129
86
  const err = convertErrorForRpcCall(error, [
130
87
  HttpStatusCode.BAD_REQUEST,
@@ -138,36 +95,13 @@ export const oauthRegister = async (register, context) => {
138
95
  };
139
96
  export const oauthGuestLogin = async (guest, context) => {
140
97
  const shopId = context.shopId;
141
- const { axiosInstance } = getOAuthSettings(context);
98
+ const client = getOAuthClient(context);
142
99
  try {
143
- const response = await axiosInstance.post(
144
- "/auth/login/guest",
145
- {
146
- ...guest,
147
- shop_id: shopId
148
- }
149
- );
150
- context.updateTokens({
151
- accessToken: response.data.access_token,
152
- refreshToken: response.data.refresh_token
100
+ const tokens = client.guestLogin({
101
+ ...guest,
102
+ shop_id: shopId
153
103
  });
154
- await saveUserOnSession(response.data.access_token, context);
155
- const { customerId } = decodeJwt(response.data.access_token);
156
- await Promise.all([
157
- mergeBaskets(
158
- context.sessionId,
159
- await context.generateBasketKeyForUserId(customerId),
160
- DEFAULT_WITH_LISTING,
161
- context
162
- ),
163
- mergeWishlists(
164
- context.sessionId,
165
- await context.generateWishlistKeyForUserId(customerId),
166
- DEFAULT_WITH_LISTING,
167
- context
168
- )
169
- ]);
170
- await context.createUserBoundSession();
104
+ await postLogin(context, tokens);
171
105
  } catch (error) {
172
106
  const err = convertErrorForRpcCall(error, [
173
107
  HttpStatusCode.BAD_REQUEST,
@@ -181,21 +115,18 @@ export const oauthGuestLogin = async (guest, context) => {
181
115
  };
182
116
  export const refreshAccessToken = async (context) => {
183
117
  const refreshToken = context.refreshToken;
184
- const { axiosInstance } = getOAuthSettings(context);
118
+ const client = getOAuthClient(context);
185
119
  if (!refreshToken) {
186
120
  throw new Error("No app refresh token provided");
187
121
  }
188
122
  try {
189
- const response = await axiosInstance.post(
190
- "/oauth/token",
191
- {
192
- grant_type: "refresh_token",
193
- refresh_token: refreshToken
194
- }
195
- );
123
+ const tokens = await client.refreshToken({
124
+ grant_type: "refresh_token",
125
+ refresh_token: refreshToken
126
+ });
196
127
  context.updateTokens({
197
- accessToken: response.data?.access_token,
198
- refreshToken: response.data?.refresh_token
128
+ accessToken: tokens?.access_token,
129
+ refreshToken: tokens?.refresh_token
199
130
  });
200
131
  return { success: !!response.data };
201
132
  } catch (error) {
@@ -213,15 +144,10 @@ export const oauthRevokeToken = async (context) => {
213
144
  if (!accessToken) {
214
145
  throw new Error("No app oauth authentication credentials");
215
146
  }
216
- const decodedAccessToken = decodeJwt(accessToken);
217
- const { axiosInstance } = getOAuthSettings(context);
147
+ const client = getOAuthClient(context);
218
148
  await context.destroySession();
219
149
  try {
220
- await axiosInstance.delete(`/oauth/tokens/${decodedAccessToken.jti}`, {
221
- headers: {
222
- Authorization: `Bearer ${accessToken}`
223
- }
224
- });
150
+ await client.revokeToken(accessToken);
225
151
  return { result: true };
226
152
  } catch (error) {
227
153
  const err = convertErrorForRpcCall(error, [
@@ -237,7 +163,7 @@ export const oauthRevokeToken = async (context) => {
237
163
  };
238
164
  export const oauthForgetPassword = async ({ email }, context) => {
239
165
  const shopId = context.shopId;
240
- const { axiosInstance } = getOAuthSettings(context);
166
+ const client = getOAuthClient(context);
241
167
  try {
242
168
  const resetUrl = new URL(context.auth.resetPasswordUrl);
243
169
  if (!resetUrl.searchParams.has("hash")) {
@@ -245,7 +171,7 @@ export const oauthForgetPassword = async ({ email }, context) => {
245
171
  } else {
246
172
  resetUrl.searchParams.set("hash", "{hash}");
247
173
  }
248
- await axiosInstance.post("/auth/password/send-reset-email", {
174
+ await client.sendPasswordResetEmail({
249
175
  email,
250
176
  reset_url: decodeURI(resetUrl.toString()),
251
177
  shop_id: shopId
@@ -266,18 +192,15 @@ export const oauthForgetPassword = async ({ email }, context) => {
266
192
  };
267
193
  export const updatePasswordByHash = async (passwordHash, context) => {
268
194
  const shopId = context.shopId;
269
- const { axiosInstance } = getOAuthSettings(context);
195
+ const client = getOAuthClient(context);
270
196
  try {
271
- const response = await axiosInstance.put(
272
- "/auth/password/update-by-hash",
273
- {
274
- ...passwordHash,
275
- shop_id: shopId
276
- }
277
- );
197
+ const tokens = await client.updatePasswordByHash({
198
+ ...passwordHash,
199
+ shop_id: shopId
200
+ });
278
201
  context.updateTokens({
279
- accessToken: response.data.access_token,
280
- refreshToken: response.data.refresh_token
202
+ accessToken: tokens.access_token,
203
+ refreshToken: tokens.refresh_token
281
204
  });
282
205
  await context.createUserBoundSession();
283
206
  } catch (error) {
@@ -4,9 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.refreshUser = exports.getUser = exports.fetchUser = void 0;
7
- var _axios = _interopRequireDefault(require("axios"));
7
+ var _fetch = require("../../utils/fetch.cjs");
8
8
  var _httpStatus = require("../../constants/httpStatus.cjs");
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
9
  const getUser = exports.getUser = function getUser2(context) {
11
10
  return {
12
11
  user: context.user
@@ -21,20 +20,24 @@ const fetchUser = exports.fetchUser = async function fetchUser2(payload, options
21
20
  shopId,
22
21
  accessHeader
23
22
  } = options;
24
- const user = await _axios.default.get(`${checkoutUrl}/api/oauth/me`, {
23
+ const response = await fetch(`${checkoutUrl}/api/oauth/me`, {
25
24
  headers: {
26
25
  Authorization: `Bearer ${accessToken}`,
27
- "X-Shop-Id": shopId,
26
+ "X-Shop-Id": shopId.toString(),
28
27
  "Content-Type": "application/json",
29
28
  ...(accessHeader && {
30
29
  "X-Access-Header": accessHeader
31
30
  })
32
31
  }
33
32
  });
33
+ if (!response.ok) {
34
+ throw new _fetch.FetchError(response);
35
+ }
36
+ const user = await response.json();
34
37
  return {
35
- ...user.data,
38
+ ...user,
36
39
  authentication: {
37
- ...user.data?.authentication,
40
+ ...user?.authentication,
38
41
  storefrontAccessToken: accessToken
39
42
  },
40
43
  ...{
@@ -56,19 +59,22 @@ const refreshUser = exports.refreshUser = async function refreshUser2(context) {
56
59
  } = _httpStatus.HttpStatusCode;
57
60
  return status >= OK && status < MULTIPLE_CHOICES || status === FORBIDDEN;
58
61
  };
59
- const user = await _axios.default.get(`${checkout.url}/api/oauth/me`, {
62
+ const response = await fetch(`${checkout.url}/api/oauth/me`, {
60
63
  headers: {
61
64
  Authorization: `Bearer ${accessToken}`,
62
- "X-Shop-Id": shopId,
65
+ "X-Shop-Id": shopId.toString(),
63
66
  "Content-Type": "application/json"
64
- },
65
- validateStatus
67
+ }
66
68
  });
67
- if (user.data.id) {
69
+ if (!response.ok || !validateStatus(response.status)) {
70
+ throw new _fetch.FetchError(response);
71
+ }
72
+ const user = await response.json();
73
+ if (user.id) {
68
74
  context.updateUser({
69
- ...user.data,
75
+ ...user,
70
76
  authentication: {
71
- ...user.data?.authentication,
77
+ ...user?.authentication,
72
78
  storefrontAccessToken: accessToken
73
79
  },
74
80
  ...{
@@ -76,7 +82,7 @@ const refreshUser = exports.refreshUser = async function refreshUser2(context) {
76
82
  }
77
83
  });
78
84
  return {
79
- user: user.data
85
+ user
80
86
  };
81
87
  }
82
88
  await context.destroySession();