@scayle/storefront-core 7.38.0 → 7.38.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 7.38.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fix potential empty user after login
8
+
3
9
  ## 7.38.0
4
10
 
5
11
  ### Minor Changes
@@ -50,16 +50,16 @@ class CustomerAPIClient {
50
50
  * When not logged in will return undefined.
51
51
  * @see https://scayle.dev/api/oauth/latest/fetch-authorized-payload
52
52
  */
53
- async getMe(shopId) {
53
+ async getMe(shopId, accessToken) {
54
54
  const response = await fetch(`${this.baseURL}/api/oauth/me`, {
55
55
  headers: {
56
56
  ...this.headers,
57
+ ...(accessToken ? {
58
+ Authorization: `Bearer ${accessToken}`
59
+ } : {}),
57
60
  "X-Shop-Id": shopId.toString()
58
61
  }
59
62
  });
60
- if (response.status === _httpStatus.HttpStatusCode.FORBIDDEN) {
61
- return void 0;
62
- }
63
63
  return this.handleResponse(response);
64
64
  }
65
65
  /**
@@ -49,7 +49,7 @@ export declare class CustomerAPIClient {
49
49
  * When not logged in will return undefined.
50
50
  * @see https://scayle.dev/api/oauth/latest/fetch-authorized-payload
51
51
  */
52
- getMe(shopId: number): Promise<ShopUser | undefined>;
52
+ getMe(shopId: number, accessToken?: string): Promise<ShopUser>;
53
53
  /**
54
54
  * Fetch a customer's order
55
55
  * @see https://scayle.dev/api/oauth/latest/fetch-order-by-id
@@ -44,16 +44,14 @@ export class CustomerAPIClient {
44
44
  * When not logged in will return undefined.
45
45
  * @see https://scayle.dev/api/oauth/latest/fetch-authorized-payload
46
46
  */
47
- async getMe(shopId) {
47
+ async getMe(shopId, accessToken) {
48
48
  const response = await fetch(`${this.baseURL}/api/oauth/me`, {
49
49
  headers: {
50
50
  ...this.headers,
51
+ ...accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
51
52
  "X-Shop-Id": shopId.toString()
52
53
  }
53
54
  });
54
- if (response.status === HttpStatusCode.FORBIDDEN) {
55
- return void 0;
56
- }
57
55
  return this.handleResponse(response);
58
56
  }
59
57
  /**
@@ -17,19 +17,16 @@ const convertErrorForRpcCall = (error, httpStatuses) => {
17
17
  }
18
18
  };
19
19
  exports.convertErrorForRpcCall = convertErrorForRpcCall;
20
- const saveUserOnSession = async (accessToken, context) => {
20
+ async function postLogin(context, tokens) {
21
21
  const user = await (0, _user2.fetchUser)({
22
- accessToken,
22
+ accessToken: tokens.access_token,
23
23
  callback: ""
24
24
  }, context);
25
- context.updateUser(user);
26
- };
27
- async function postLogin(context, tokens) {
28
25
  context.updateTokens({
29
26
  accessToken: tokens.access_token,
30
27
  refreshToken: tokens.refresh_token
31
28
  });
32
- await saveUserOnSession(tokens.access_token, context);
29
+ context.updateUser(user);
33
30
  const {
34
31
  customerId
35
32
  } = (0, _jose.decodeJwt)(tokens.access_token);
@@ -50,7 +47,7 @@ const oauthLogin = async (login, context) => {
50
47
  });
51
48
  await postLogin(context, tokens);
52
49
  } catch (error) {
53
- const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.INTERNAL_SERVER_ERROR, _constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.NOT_FOUND]);
50
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.INTERNAL_SERVER_ERROR, _constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.NOT_FOUND, _constants.HttpStatusCode.FORBIDDEN]);
54
51
  if (err) {
55
52
  throw err;
56
53
  }
@@ -68,7 +65,7 @@ const oauthRegister = async (register, context) => {
68
65
  });
69
66
  await postLogin(context, tokens);
70
67
  } catch (error) {
71
- const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.CONFLICT]);
68
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.CONFLICT, _constants.HttpStatusCode.FORBIDDEN]);
72
69
  if (err) {
73
70
  throw err;
74
71
  }
@@ -86,7 +83,7 @@ const oauthGuestLogin = async (guest, context) => {
86
83
  });
87
84
  await postLogin(context, tokens);
88
85
  } catch (error) {
89
- const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.CONFLICT]);
86
+ const err = convertErrorForRpcCall(error, [_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusCode.CONFLICT, _constants.HttpStatusCode.FORBIDDEN]);
90
87
  if (err) {
91
88
  throw err;
92
89
  }
@@ -10,16 +10,16 @@ export const convertErrorForRpcCall = (error, httpStatuses) => {
10
10
  return error;
11
11
  }
12
12
  };
13
- const saveUserOnSession = async (accessToken, context) => {
14
- const user = await fetchUser({ accessToken, callback: "" }, context);
15
- context.updateUser(user);
16
- };
17
13
  async function postLogin(context, tokens) {
14
+ const user = await fetchUser(
15
+ { accessToken: tokens.access_token, callback: "" },
16
+ context
17
+ );
18
18
  context.updateTokens({
19
19
  accessToken: tokens.access_token,
20
20
  refreshToken: tokens.refresh_token
21
21
  });
22
- await saveUserOnSession(tokens.access_token, context);
22
+ context.updateUser(user);
23
23
  const { customerId } = decodeJwt(tokens.access_token);
24
24
  await Promise.all([
25
25
  mergeBaskets(
@@ -54,7 +54,8 @@ export const oauthLogin = async (login, context) => {
54
54
  HttpStatusCode.INTERNAL_SERVER_ERROR,
55
55
  HttpStatusCode.BAD_REQUEST,
56
56
  HttpStatusCode.UNAUTHORIZED,
57
- HttpStatusCode.NOT_FOUND
57
+ HttpStatusCode.NOT_FOUND,
58
+ HttpStatusCode.FORBIDDEN
58
59
  ]);
59
60
  if (err) {
60
61
  throw err;
@@ -75,7 +76,8 @@ export const oauthRegister = async (register, context) => {
75
76
  const err = convertErrorForRpcCall(error, [
76
77
  HttpStatusCode.BAD_REQUEST,
77
78
  HttpStatusCode.UNAUTHORIZED,
78
- HttpStatusCode.CONFLICT
79
+ HttpStatusCode.CONFLICT,
80
+ HttpStatusCode.FORBIDDEN
79
81
  ]);
80
82
  if (err) {
81
83
  throw err;
@@ -96,7 +98,8 @@ export const oauthGuestLogin = async (guest, context) => {
96
98
  const err = convertErrorForRpcCall(error, [
97
99
  HttpStatusCode.BAD_REQUEST,
98
100
  HttpStatusCode.UNAUTHORIZED,
99
- HttpStatusCode.CONFLICT
101
+ HttpStatusCode.CONFLICT,
102
+ HttpStatusCode.FORBIDDEN
100
103
  ]);
101
104
  if (err) {
102
105
  throw err;
@@ -19,17 +19,12 @@ const fetchUser = exports.fetchUser = async function fetchUser2(payload, context
19
19
  shopId
20
20
  } = context;
21
21
  const client = new _customer.CustomerAPIClient(context);
22
- const user = await client.getMe(shopId);
23
- return {
24
- ...user,
25
- authentication: {
26
- ...user?.authentication,
27
- storefrontAccessToken: accessToken
28
- },
29
- ...{
30
- loginShopId: shopId
31
- }
32
- };
22
+ const user = await client.getMe(shopId, accessToken);
23
+ if (user.authentication) {
24
+ user.authentication.storefrontAccessToken = accessToken;
25
+ }
26
+ user.loginShopId = shopId;
27
+ return user;
33
28
  };
34
29
  const refreshUser = exports.refreshUser = async function refreshUser2(context) {
35
30
  (0, _types.assertSession)(context);
@@ -38,8 +33,8 @@ const refreshUser = exports.refreshUser = async function refreshUser2(context) {
38
33
  shopId
39
34
  } = context;
40
35
  const client = new _customer.CustomerAPIClient(context);
41
- const user = await client.getMe(shopId);
42
- if (user?.id) {
36
+ try {
37
+ const user = await client.getMe(shopId);
43
38
  context.updateUser({
44
39
  ...user,
45
40
  authentication: user.authentication ? {
@@ -53,9 +48,10 @@ const refreshUser = exports.refreshUser = async function refreshUser2(context) {
53
48
  return {
54
49
  user
55
50
  };
51
+ } catch {
52
+ await context.destroySession();
53
+ return {
54
+ user: void 0
55
+ };
56
56
  }
57
- await context.destroySession();
58
- return {
59
- user: void 0
60
- };
61
57
  };
@@ -9,22 +9,19 @@ const fetchUser = async function fetchUser2(payload, context) {
9
9
  const { accessToken } = payload;
10
10
  const { shopId } = context;
11
11
  const client = new CustomerAPIClient(context);
12
- const user = await client.getMe(shopId);
13
- return {
14
- ...user,
15
- authentication: {
16
- ...user?.authentication,
17
- storefrontAccessToken: accessToken
18
- },
19
- ...{ loginShopId: shopId }
20
- };
12
+ const user = await client.getMe(shopId, accessToken);
13
+ if (user.authentication) {
14
+ user.authentication.storefrontAccessToken = accessToken;
15
+ }
16
+ user.loginShopId = shopId;
17
+ return user;
21
18
  };
22
19
  const refreshUser = async function refreshUser2(context) {
23
20
  assertSession(context);
24
21
  const { accessToken, shopId } = context;
25
22
  const client = new CustomerAPIClient(context);
26
- const user = await client.getMe(shopId);
27
- if (user?.id) {
23
+ try {
24
+ const user = await client.getMe(shopId);
28
25
  context.updateUser({
29
26
  ...user,
30
27
  authentication: user.authentication ? {
@@ -34,8 +31,9 @@ const refreshUser = async function refreshUser2(context) {
34
31
  ...{ loginShopId: shopId }
35
32
  });
36
33
  return { user };
34
+ } catch {
35
+ await context.destroySession();
36
+ return { user: void 0 };
37
37
  }
38
- await context.destroySession();
39
- return { user: void 0 };
40
38
  };
41
39
  export { getUser, fetchUser, refreshUser };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "7.38.0",
3
+ "version": "7.38.1",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",