Skip to content

Commit

Permalink
feat: fetch current customer info on new login method
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-marco authored and kris-liu-smile committed Jul 31, 2023
1 parent e947abe commit eb2a5d2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
12 changes: 10 additions & 2 deletions apps/storefront/src/components/HeadlessController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ export default function HeadlessController({
logInWithStorefrontToken: (customerJWTToken: string) =>
getCurrentCustomerInfo(dispatch, customerJWTToken),
graphqlBCProxy: B3Request.graphqlBCProxy,
loginWithB2BStorefrontToken: (b2bStorefrontJWTToken: string) =>
B3SStorage.set('B2BToken', b2bStorefrontJWTToken),
loginWithB2BStorefrontToken: async (
b2bStorefrontJWTToken: string
) => {
B3SStorage.set('B2BToken', b2bStorefrontJWTToken)
await getCurrentCustomerInfo(
dispatch,
undefined,
b2bStorefrontJWTToken
)
},
},
shoppingList: {
addProductFromPage: () => {
Expand Down
4 changes: 3 additions & 1 deletion apps/storefront/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ declare interface Window {
customerJWTToken: string
) => Promise<{ role: number; userType: string } | undefined>
graphqlBCProxy: typeof import('@/shared/service/request/b3Fetch').default.graphqlBCProxy
loginWithB2BStorefrontToken: (b2bStorefrontJWTToken: string) => void
loginWithB2BStorefrontToken: (
b2bStorefrontJWTToken: string
) => Promise<void>
}
shoppingList: {
addProductFromPage: () => void
Expand Down
2 changes: 1 addition & 1 deletion apps/storefront/src/shared/service/bc/graphql/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getCustomer = () => `query customer {
}`

const getCustomerInfo = (): CustomFieldItems =>
B3Request.graphqlBC({
B3Request.graphqlBCProxy({
query: getCustomer(),
})

Expand Down
2 changes: 1 addition & 1 deletion apps/storefront/src/shared/service/request/b3Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const B3Request = {
*/
graphqlBCProxy: function post<T>(data: T): Promise<any> {
const config = {
Authorization: `Bearer ${B3SStorage.get('B3B2BToken') || ''}`,
Authorization: `Bearer ${B3SStorage.get('B2BToken') || ''}`,
}
return graphqlRequest(RequestType.BCProxyGraphql, data, config)
},
Expand Down
13 changes: 8 additions & 5 deletions apps/storefront/src/utils/loginInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ export const getCompanyUserInfo = async (

export const getCurrentCustomerInfo = async (
dispatch: DispatchProps,
jwtToken?: string
jwtToken?: string,
b2bToken?: string
) => {
try {
const {
Expand All @@ -333,10 +334,12 @@ export const getCurrentCustomerInfo = async (
if (companyUserInfo && customerId) {
const { userType, role, id } = companyUserInfo

await getB2BTokenWithJWTToken(
userType,
jwtToken || (await getCurrentJwt())
)
if (!b2bToken) {
await getB2BTokenWithJWTToken(
userType,
jwtToken || (await getCurrentJwt())
)
}

const [companyInfo] = await Promise.all([
getCompanyInfo(id, role, userType),
Expand Down

0 comments on commit eb2a5d2

Please sign in to comment.