cloudcommerce 0.0.109 → 0.0.110
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 +21 -0
- package/package.json +1 -1
- package/packages/api/lib/api.d.ts +4 -4940
- package/packages/api/lib/types.d.ts +17 -3
- package/packages/api/package.json +1 -1
- package/packages/api/src/types.ts +45 -23
- package/packages/apps/correios/package.json +1 -1
- package/packages/apps/custom-shipping/package.json +1 -1
- package/packages/apps/discounts/package.json +1 -1
- package/packages/apps/frenet/lib-mjs/calculate-frenet.mjs +26 -23
- package/packages/apps/frenet/package.json +1 -1
- package/packages/apps/tiny-erp/lib/event-to-tiny.js +94 -94
- package/packages/apps/tiny-erp/lib/index.js +1 -1
- package/packages/apps/tiny-erp/lib/integration/after-tiny-queue.js +71 -74
- package/packages/apps/tiny-erp/lib/integration/export-order-to-tiny.js +70 -73
- package/packages/apps/tiny-erp/lib/integration/export-product-to-tiny.js +49 -53
- package/packages/apps/tiny-erp/lib/integration/helpers/format-tiny-date.js +3 -3
- package/packages/apps/tiny-erp/lib/integration/import-order-from-tiny.js +76 -75
- package/packages/apps/tiny-erp/lib/integration/import-product-from-tiny.js +137 -140
- package/packages/apps/tiny-erp/lib/integration/parsers/order-from-tiny.js +40 -39
- package/packages/apps/tiny-erp/lib/integration/parsers/order-to-tiny.js +173 -178
- package/packages/apps/tiny-erp/lib/integration/parsers/product-from-tiny.js +173 -171
- package/packages/apps/tiny-erp/lib/integration/parsers/product-to-tiny.js +123 -127
- package/packages/apps/tiny-erp/lib/integration/parsers/status-from-tiny.js +32 -32
- package/packages/apps/tiny-erp/lib/integration/parsers/status-to-tiny.js +37 -37
- package/packages/apps/tiny-erp/lib/integration/post-tiny-erp.js +42 -43
- package/packages/apps/tiny-erp/lib/tiny-erp.js +8 -6
- package/packages/apps/tiny-erp/lib/tiny-webhook.js +76 -73
- package/packages/apps/tiny-erp/package.json +1 -1
- package/packages/cli/package.json +1 -1
- package/packages/config/package.json +1 -1
- package/packages/events/package.json +1 -1
- package/packages/firebase/package.json +1 -1
- package/packages/i18n/lib/en_us.d.ts +1 -0
- package/packages/i18n/lib/en_us.js +1 -0
- package/packages/i18n/lib/en_us.js.map +1 -1
- package/packages/i18n/lib/pt_br.d.ts +1 -0
- package/packages/i18n/lib/pt_br.js +1 -0
- package/packages/i18n/lib/pt_br.js.map +1 -1
- package/packages/i18n/package.json +1 -1
- package/packages/i18n/src/en_us.ts +1 -0
- package/packages/i18n/src/pt_br.ts +1 -0
- package/packages/modules/lib/firebase/ajv.js +24 -19
- package/packages/modules/lib/firebase/call-app-module.js +110 -116
- package/packages/modules/lib/firebase/checkout.js +149 -154
- package/packages/modules/lib/firebase/checkout.js.map +1 -1
- package/packages/modules/lib/firebase/functions-checkout/fix-items.js +187 -194
- package/packages/modules/lib/firebase/functions-checkout/get-custumerId.js +33 -27
- package/packages/modules/lib/firebase/functions-checkout/get-custumerId.js.map +1 -1
- package/packages/modules/lib/firebase/functions-checkout/handle-order-transaction.js +111 -125
- package/packages/modules/lib/firebase/functions-checkout/handle-order-transaction.js.map +1 -1
- package/packages/modules/lib/firebase/functions-checkout/new-order.js +178 -178
- package/packages/modules/lib/firebase/functions-checkout/new-order.js.map +1 -1
- package/packages/modules/lib/firebase/functions-checkout/request-to-module.js +53 -60
- package/packages/modules/lib/firebase/functions-checkout/request-to-module.js.map +1 -1
- package/packages/modules/lib/firebase/functions-checkout/utils.js +197 -195
- package/packages/modules/lib/firebase/handle-module.js +146 -144
- package/packages/modules/lib/firebase/handle-module.js.map +1 -1
- package/packages/modules/lib/firebase/proxy-apps.js +1 -2
- package/packages/modules/lib/firebase/serve-modules-api.js +53 -52
- package/packages/modules/lib/firebase/serve-modules-api.js.map +1 -1
- package/packages/modules/lib/firebase.js +6 -4
- package/packages/modules/lib/index.js +17 -12
- package/packages/modules/lib/index.js.map +1 -1
- package/packages/modules/package.json +2 -1
- package/packages/modules/schemas/@checkout.cjs +13 -1
- package/packages/modules/schemas/list_payments.cjs +6 -0
- package/packages/modules/src/firebase/checkout.ts +22 -49
- package/packages/modules/src/firebase/functions-checkout/get-custumerId.ts +23 -17
- package/packages/modules/src/firebase/functions-checkout/handle-order-transaction.ts +8 -45
- package/packages/modules/src/firebase/functions-checkout/new-order.ts +4 -9
- package/packages/modules/src/firebase/functions-checkout/request-to-module.ts +2 -11
- package/packages/modules/src/firebase/handle-module.ts +1 -1
- package/packages/modules/src/firebase/serve-modules-api.ts +1 -1
- package/packages/modules/src/index.ts +2 -0
- package/packages/passport/lib/firebase/authenticate-customer.js +81 -0
- package/packages/passport/lib/firebase/authenticate-customer.js.map +1 -0
- package/packages/passport/lib/firebase/serve-passport-api.js +20 -30
- package/packages/passport/lib/firebase/serve-passport-api.js.map +1 -1
- package/packages/passport/lib/firebase.js +1 -13
- package/packages/passport/lib/firebase.js.map +1 -1
- package/packages/passport/lib/index.js +1 -1
- package/packages/passport/lib/index.js.map +1 -1
- package/packages/passport/package.json +1 -1
- package/packages/passport/src/firebase/authenticate-customer.ts +94 -0
- package/packages/passport/src/firebase/serve-passport-api.ts +21 -49
- package/packages/passport/src/firebase.ts +1 -21
- package/packages/passport/src/index.ts +4 -1
- package/packages/ssr/package.json +1 -1
- package/packages/storefront/content/header.json +6 -0
- package/packages/storefront/dist/client/LoginDrawer.e69d1c74.js +1 -0
- package/packages/storefront/dist/client/assets/_...d5980663.css +4 -0
- package/packages/storefront/dist/client/chunks/LoginDrawer.0c26574f.js +1 -0
- package/packages/storefront/dist/client/chunks/{LoginForm.c995d3d4.js → LoginForm.892d64a5.js} +109 -109
- package/packages/storefront/dist/client/chunks/runtime-dom.esm-bundler.7f8f570f.js +1 -0
- package/packages/storefront/dist/client/{client.6736232b.js → client.246ab4a4.js} +1 -1
- package/packages/storefront/dist/client/sw.js +1 -1
- package/packages/storefront/dist/server/entry.mjs +2204 -26
- package/packages/storefront/package.json +1 -1
- package/packages/storefront/src/assets/pico.css +3 -400
- package/packages/storefront/src/lib/components/{AOffcanvas.vue → ADrawer.vue} +3 -3
- package/packages/storefront/src/lib/components/LoginDrawer.vue +80 -0
- package/packages/storefront/src/lib/components/LoginForm.vue +31 -14
- package/packages/storefront/src/lib/layouts/PagesHeader.astro +18 -7
- package/packages/storefront/src/lib/state/customer-session.ts +137 -0
- package/packages/types/index.ts +22 -0
- package/packages/types/modules/@checkout:params.d.ts +9 -1
- package/packages/types/modules/list_payments:params.d.ts +4 -0
- package/packages/types/package.json +1 -1
- package/packages/passport/lib/firebase/handle-passport.js +0 -112
- package/packages/passport/lib/firebase/handle-passport.js.map +0 -1
- package/packages/passport/src/firebase/handle-passport.ts +0 -142
- package/packages/storefront/dist/client/LoginOffcanvas.28d2f50c.js +0 -1
- package/packages/storefront/dist/client/assets/_...41f686e3.css +0 -4
- package/packages/storefront/dist/client/chunks/LoginOffcanvas.a1f3fe3b.js +0 -1
- package/packages/storefront/dist/client/chunks/runtime-dom.esm-bundler.62d623e7.js +0 -1
- package/packages/storefront/src/lib/components/LoginOffcanvas.vue +0 -41
|
@@ -3,16 +3,17 @@ import type { Categories } from '@cloudcommerce/api/types';
|
|
|
3
3
|
import type CmsHeader from '../../types/cms-header';
|
|
4
4
|
import type CmsContacts from '../../types/cms-contacts';
|
|
5
5
|
import type { PageContext } from '../ssr-context';
|
|
6
|
+
import { i19buyOnWhatsApp } from '@cloudcommerce/i18n/src/pt_br';
|
|
6
7
|
import { getImage } from '../ssr/image';
|
|
7
8
|
import TopBar, { Props as TopBarProps } from '../components/TopBar.vue';
|
|
8
9
|
import TheHeader, { Props as TheHeaderProps } from '../components/TheHeader.vue';
|
|
9
|
-
import
|
|
10
|
+
import LoginDrawer, { Props as LoginDrawerProps } from '../components/LoginDrawer.vue';
|
|
10
11
|
|
|
11
12
|
export interface Props {
|
|
12
13
|
pageContext: PageContext;
|
|
13
14
|
topBarProps?: Partial<TopBarProps>;
|
|
14
15
|
theHeaderProps?: Partial<TheHeaderProps>;
|
|
15
|
-
|
|
16
|
+
loginDrawerProps?: Partial<LoginDrawerProps>;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
const {
|
|
@@ -23,7 +24,7 @@ const {
|
|
|
23
24
|
},
|
|
24
25
|
topBarProps,
|
|
25
26
|
theHeaderProps,
|
|
26
|
-
|
|
27
|
+
loginDrawerProps,
|
|
27
28
|
} = Astro.props as Props;
|
|
28
29
|
const header = cms('header') as CmsHeader;
|
|
29
30
|
const contacts = cms('contacts') as CmsContacts;
|
|
@@ -57,11 +58,20 @@ if (header.categories_list) {
|
|
|
57
58
|
const hasMegamenu: boolean | undefined = header.desktop_megamenu;
|
|
58
59
|
const isAlphabeticalOrderSubmenu: boolean | undefined = header.alphabetical_order_submenu;
|
|
59
60
|
const isFullWidthSubmenu: boolean | undefined = header.full_width_submenu;
|
|
61
|
+
const loginDrawerAdditionalLinks: LoginDrawerProps['additionalLinks'] = header.drawer_links
|
|
62
|
+
?.map(({ title, link }) => ({ href: link, innerHTML: title })) || [];
|
|
63
|
+
if (contacts.whatsapp) {
|
|
64
|
+
loginDrawerAdditionalLinks.unshift({
|
|
65
|
+
href: `https://wa.me/55${contacts.whatsapp.replace(/\D/g, '')}`,
|
|
66
|
+
innerHTML: `<i class="i-whatsapp text-lg"></i> ${i19buyOnWhatsApp}`,
|
|
67
|
+
isBlank: true,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
60
70
|
|
|
61
71
|
declare global {
|
|
62
72
|
var TopBarProps: TopBarProps;
|
|
63
73
|
var TheHeaderProps: TheHeaderProps;
|
|
64
|
-
var
|
|
74
|
+
var LoginDrawerProps: LoginDrawerProps;
|
|
65
75
|
}
|
|
66
76
|
globalThis.TopBarProps = {
|
|
67
77
|
marketingStripe: header.marketing_stripe,
|
|
@@ -83,8 +93,9 @@ globalThis.TheHeaderProps = {
|
|
|
83
93
|
},
|
|
84
94
|
...theHeaderProps,
|
|
85
95
|
};
|
|
86
|
-
globalThis.
|
|
87
|
-
|
|
96
|
+
globalThis.LoginDrawerProps = {
|
|
97
|
+
additionalLinks: loginDrawerAdditionalLinks,
|
|
98
|
+
...loginDrawerProps,
|
|
88
99
|
};
|
|
89
100
|
---
|
|
90
101
|
|
|
@@ -94,7 +105,7 @@ globalThis.LoginOffcanvasProps = {
|
|
|
94
105
|
<slot name="header">
|
|
95
106
|
<TheHeader {...globalThis.TheHeaderProps}>
|
|
96
107
|
<Fragment slot="buttons">
|
|
97
|
-
<
|
|
108
|
+
<LoginDrawer client:idle {...globalThis.LoginDrawerProps} />
|
|
98
109
|
</Fragment>
|
|
99
110
|
</TheHeader>
|
|
100
111
|
</slot>
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Customers } from '@cloudcommerce/api/types';
|
|
2
|
+
import api from '@cloudcommerce/api';
|
|
3
|
+
import { nickname as getNickname } from '@ecomplus/utils';
|
|
4
|
+
import { map, computed, onSet } from 'nanostores';
|
|
5
|
+
import {
|
|
6
|
+
getAuth,
|
|
7
|
+
onAuthStateChanged,
|
|
8
|
+
isSignInWithEmailLink,
|
|
9
|
+
signInWithEmailLink,
|
|
10
|
+
// updateProfile,
|
|
11
|
+
} from 'firebase/auth';
|
|
12
|
+
import '../scripts/firebase-app';
|
|
13
|
+
|
|
14
|
+
const emptySession = {
|
|
15
|
+
customer: {
|
|
16
|
+
display_name: '',
|
|
17
|
+
main_email: '',
|
|
18
|
+
},
|
|
19
|
+
auth: null,
|
|
20
|
+
};
|
|
21
|
+
const session = map<{
|
|
22
|
+
customer: Partial<Customers>,
|
|
23
|
+
auth: null | {
|
|
24
|
+
access_token: string,
|
|
25
|
+
expires: string,
|
|
26
|
+
customer_id: string,
|
|
27
|
+
},
|
|
28
|
+
}>(emptySession);
|
|
29
|
+
const storageKey = 'SESSION';
|
|
30
|
+
const sessionJson = localStorage.getItem(storageKey);
|
|
31
|
+
if (sessionJson) {
|
|
32
|
+
try {
|
|
33
|
+
session.set(JSON.parse(sessionJson));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
localStorage.removeItem(storageKey);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
onSet(session, () => {
|
|
39
|
+
localStorage.setItem(storageKey, JSON.stringify(session.get()));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const isAuthenticated = computed(session, ({ auth }) => {
|
|
43
|
+
return auth && new Date(auth.expires).getTime() - Date.now() > 1000 * 10;
|
|
44
|
+
});
|
|
45
|
+
const customer = computed(session, (_session) => _session.customer);
|
|
46
|
+
const customerName = computed(customer, (_customer) => getNickname(_customer));
|
|
47
|
+
const customerEmail = computed(customer, (_customer) => _customer.main_email);
|
|
48
|
+
|
|
49
|
+
const setCustomerEmail = (email: string) => session.setKey('customer', {
|
|
50
|
+
...customer.get(),
|
|
51
|
+
main_email: email,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const firebaseAuth = getAuth();
|
|
55
|
+
if (isSignInWithEmailLink(firebaseAuth, window.location.href)) {
|
|
56
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
57
|
+
const email = urlParams.get('email');
|
|
58
|
+
if (email) {
|
|
59
|
+
signInWithEmailLink(firebaseAuth, email, window.location.href)
|
|
60
|
+
.catch(console.error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const isLogged = computed(isAuthenticated, (_isAuthenticated) => {
|
|
65
|
+
return _isAuthenticated || !!firebaseAuth.currentUser;
|
|
66
|
+
});
|
|
67
|
+
const logout = () => {
|
|
68
|
+
session.set(emptySession);
|
|
69
|
+
localStorage.removeItem(storageKey);
|
|
70
|
+
firebaseAuth.signOut();
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const authenticate = async () => {
|
|
74
|
+
const authToken = await firebaseAuth.currentUser.getIdToken();
|
|
75
|
+
const { domain } = window.storefront.settings;
|
|
76
|
+
try {
|
|
77
|
+
const resAuth = await fetch(`https://${domain}/api/passport/token`, {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
'Content-Type': 'application/json',
|
|
81
|
+
Authorization: `Bearer ${authToken}`,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
session.setKey('auth', await resAuth.json());
|
|
85
|
+
} catch (err) {
|
|
86
|
+
console.error(err);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const getAccessToken = async () => {
|
|
91
|
+
if (!isAuthenticated.get()) {
|
|
92
|
+
await authenticate();
|
|
93
|
+
}
|
|
94
|
+
return session.get().auth.access_token;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const fetchCustomer = async () => {
|
|
98
|
+
const accessToken = await getAccessToken();
|
|
99
|
+
const { auth } = session.get();
|
|
100
|
+
const { data } = await api.get(`customers/${auth.customer_id}`, {
|
|
101
|
+
accessToken,
|
|
102
|
+
});
|
|
103
|
+
session.setKey('customer', data);
|
|
104
|
+
return data;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
onAuthStateChanged(firebaseAuth, async (user) => {
|
|
108
|
+
if (user) {
|
|
109
|
+
if (user.emailVerified) {
|
|
110
|
+
const isEmailChanged = user.email !== customerEmail.get();
|
|
111
|
+
if (isEmailChanged || !isAuthenticated.get()) {
|
|
112
|
+
await authenticate();
|
|
113
|
+
if (isEmailChanged || !customerName.get()) {
|
|
114
|
+
await fetchCustomer();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
logout();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
export default session;
|
|
124
|
+
|
|
125
|
+
export {
|
|
126
|
+
session,
|
|
127
|
+
isAuthenticated,
|
|
128
|
+
customer,
|
|
129
|
+
customerName,
|
|
130
|
+
customerEmail,
|
|
131
|
+
setCustomerEmail,
|
|
132
|
+
isLogged,
|
|
133
|
+
logout,
|
|
134
|
+
authenticate,
|
|
135
|
+
getAccessToken,
|
|
136
|
+
fetchCustomer,
|
|
137
|
+
};
|
package/packages/types/index.ts
CHANGED
|
@@ -10,6 +10,17 @@ import type {
|
|
|
10
10
|
Stores,
|
|
11
11
|
Applications,
|
|
12
12
|
Authentications,
|
|
13
|
+
ProductSet,
|
|
14
|
+
CategorySet,
|
|
15
|
+
BrandSet,
|
|
16
|
+
CollectionSet,
|
|
17
|
+
GridSet,
|
|
18
|
+
CartSet,
|
|
19
|
+
OrderSet,
|
|
20
|
+
CustomerSet,
|
|
21
|
+
StoreSet,
|
|
22
|
+
ApplicationSet,
|
|
23
|
+
AuthenticationSet,
|
|
13
24
|
Resource,
|
|
14
25
|
ResourceId,
|
|
15
26
|
ResourceListResult,
|
|
@@ -79,6 +90,17 @@ export type {
|
|
|
79
90
|
Stores,
|
|
80
91
|
Applications,
|
|
81
92
|
Authentications,
|
|
93
|
+
ProductSet,
|
|
94
|
+
CategorySet,
|
|
95
|
+
BrandSet,
|
|
96
|
+
CollectionSet,
|
|
97
|
+
GridSet,
|
|
98
|
+
CartSet,
|
|
99
|
+
OrderSet,
|
|
100
|
+
CustomerSet,
|
|
101
|
+
StoreSet,
|
|
102
|
+
ApplicationSet,
|
|
103
|
+
AuthenticationSet,
|
|
82
104
|
Resource,
|
|
83
105
|
ResourceId,
|
|
84
106
|
ResourceListResult,
|
|
@@ -198,6 +198,14 @@ export interface CheckoutBody {
|
|
|
198
198
|
*/
|
|
199
199
|
discount_coupon?: string;
|
|
200
200
|
};
|
|
201
|
+
/**
|
|
202
|
+
* Designator of currency according to ISO 4217 (3 uppercase letters)
|
|
203
|
+
*/
|
|
204
|
+
currency_id?: string;
|
|
205
|
+
/**
|
|
206
|
+
* Graphic symbol used as a shorthand for currency's name
|
|
207
|
+
*/
|
|
208
|
+
currency_symbol?: string;
|
|
201
209
|
/**
|
|
202
210
|
* Language two letters code, sometimes with region, eg.: pt_br, fr, en_us
|
|
203
211
|
*/
|
|
@@ -254,7 +262,7 @@ export interface CheckoutBody {
|
|
|
254
262
|
/**
|
|
255
263
|
* Customer object
|
|
256
264
|
*/
|
|
257
|
-
customer
|
|
265
|
+
customer: {
|
|
258
266
|
/**
|
|
259
267
|
* Customer ID
|
|
260
268
|
*/
|
|
@@ -312,6 +312,10 @@ export interface ListPaymentsParams {
|
|
|
312
312
|
*/
|
|
313
313
|
name?: string;
|
|
314
314
|
};
|
|
315
|
+
/**
|
|
316
|
+
* Number of installments chosen (if payment selected)
|
|
317
|
+
*/
|
|
318
|
+
installments_number?: number;
|
|
315
319
|
/**
|
|
316
320
|
* Whether list payments can be refetched on client when payment selected
|
|
317
321
|
*/
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { logger } from 'firebase-functions';
|
|
2
|
-
import api from '@cloudcommerce/api';
|
|
3
|
-
import getEnv from '@cloudcommerce/firebase/lib/env';
|
|
4
|
-
|
|
5
|
-
const findCustomerByEmail = async (email) => {
|
|
6
|
-
try {
|
|
7
|
-
const { data } = await api.get(`customers?main_email=${email}`);
|
|
8
|
-
if (data.result.length) {
|
|
9
|
-
return data.result[0];
|
|
10
|
-
}
|
|
11
|
-
return null;
|
|
12
|
-
} catch (e) {
|
|
13
|
-
logger.error(e);
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
const checkFirebaseAuth = async (auth, authToken) => {
|
|
18
|
-
if (authToken && !Array.isArray(authToken)) {
|
|
19
|
-
try {
|
|
20
|
-
const customer = await auth.verifyIdToken(authToken);
|
|
21
|
-
return customer;
|
|
22
|
-
} catch (e) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
} else {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
const createCustomer = async (customer) => {
|
|
30
|
-
try {
|
|
31
|
-
const { data } = await api.post('customers', customer);
|
|
32
|
-
return data._id;
|
|
33
|
-
} catch (e) {
|
|
34
|
-
logger.error(e);
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
const generateAccessToken = async (firestore, customerId) => {
|
|
39
|
-
const { apiAuth } = getEnv();
|
|
40
|
-
const docRef = firestore.doc(`customerTokens/${customerId}`);
|
|
41
|
-
const doc = await docRef.get();
|
|
42
|
-
const expires = doc.data()?.expires;
|
|
43
|
-
if (expires && new Date(expires).getTime() - Date.now() >= 2 * 60 * 1000) {
|
|
44
|
-
return doc.data();
|
|
45
|
-
}
|
|
46
|
-
try {
|
|
47
|
-
const { data } = await api({
|
|
48
|
-
endpoint: 'authenticate',
|
|
49
|
-
method: 'post',
|
|
50
|
-
body: {
|
|
51
|
-
_id: apiAuth.authenticationId,
|
|
52
|
-
api_key: apiAuth.apiKey,
|
|
53
|
-
customer_id: customerId,
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
const accessToken = {
|
|
57
|
-
access_token: data.access_token,
|
|
58
|
-
expires: data.expires,
|
|
59
|
-
customer_id: customerId,
|
|
60
|
-
};
|
|
61
|
-
docRef.set(accessToken).catch(logger.error);
|
|
62
|
-
return accessToken;
|
|
63
|
-
} catch (e) {
|
|
64
|
-
logger.error(e);
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
const getAuthCustomerApi = async (firestore, authtoken, authFirebase) => {
|
|
69
|
-
const customerFirebaseAuth = await checkFirebaseAuth(authFirebase, authtoken);
|
|
70
|
-
if (customerFirebaseAuth !== null && customerFirebaseAuth.email) {
|
|
71
|
-
const customer = await findCustomerByEmail(customerFirebaseAuth.email);
|
|
72
|
-
if (customer !== null) {
|
|
73
|
-
return generateAccessToken(firestore, customer._id);
|
|
74
|
-
}
|
|
75
|
-
const newCustomer = {
|
|
76
|
-
display_name: customerFirebaseAuth.name || '',
|
|
77
|
-
main_email: customerFirebaseAuth.email,
|
|
78
|
-
emails: [{
|
|
79
|
-
address: customerFirebaseAuth.email,
|
|
80
|
-
verified: customerFirebaseAuth.email_verified,
|
|
81
|
-
}],
|
|
82
|
-
oauth_providers: [{
|
|
83
|
-
provider: customerFirebaseAuth.firebase.sign_in_provider,
|
|
84
|
-
user_id: customerFirebaseAuth.user_id,
|
|
85
|
-
}],
|
|
86
|
-
};
|
|
87
|
-
const customerId = await createCustomer(newCustomer);
|
|
88
|
-
if (customerId) {
|
|
89
|
-
return generateAccessToken(firestore, customerId);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// TODO: Find customer by phone number, generate token if found, otherwise unauthorize
|
|
93
|
-
return null;
|
|
94
|
-
};
|
|
95
|
-
const sendError = (res, msg, status = 400) => {
|
|
96
|
-
if (msg) {
|
|
97
|
-
res.status(status).json({
|
|
98
|
-
status,
|
|
99
|
-
error: msg,
|
|
100
|
-
});
|
|
101
|
-
} else {
|
|
102
|
-
res.status(500).json({
|
|
103
|
-
status: 500,
|
|
104
|
-
error: 'Internal server error',
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
export {
|
|
110
|
-
sendError, getAuthCustomerApi, findCustomerByEmail, generateAccessToken,
|
|
111
|
-
};
|
|
112
|
-
// # sourceMappingURL=handle-passport.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"handle-passport.js","sourceRoot":"","sources":["../../src/firebase/handle-passport.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,GAAG,MAAM,oBAAoB,CAAC;AACrC,OAAO,MAAM,MAAM,iCAAiC,CAAC;AAErD,MAAM,mBAAmB,GAAG,KAAK,EAAE,KAAyB,EAAE,EAAE;IAC9D,IAAI;QACF,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,GAAG,CAAC,GAAG,CAAC,wBAAwB,KAAK,EAAE,CAAC,CAAC;QAChE,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;YACtB,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;SACvB;QACD,OAAO,IAAI,CAAC;KACb;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,CAAC,KAAK,CAAC,CAAC,CAA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AO,EAAE,oBAAoB,CAAC,KAAK;oBACnC,QAAQ,EAAE,oBAAoB,CAAC,cAAc;iBAC9C,CAAC;YACF,eAAe,EAAE,CAAC;oBAChB,QAAQ,EAAE,oBAAoB,CAAC,QAAQ,CAAC,gBAAgB;oBACxD,OAAO,EAAE,oBAAoB,CAAC,OAAO;iBACtC,CAAC;SACU,CAAC;QACf,MAAM,UAAU,GAAG,MAAM,cAAc,CAAC,WAAW,CAAC,CAAC;QACrD,IAAI,UAAU,EAAE;YACd,OAAO,mBAAmB,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;SACnD;KACF;IACD,sFAAsF;IACtF,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,SAAS,GAAG,CAChB,GAAa,EACb,GAAY,EACZ,MAAM,GAAG,GAAG,EACZ,EAAE;IACF,IAAI,GAAG,EAAE;QACP,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC;YACtB,MAAM;YACN,KAAK,EAAE,GAAG;SACX,CAAC,CAAC;KACJ;SAAM;QACL,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACnB,MAAM,EAAE,GAAG;YACX,KAAK,EAAE,uBAAuB;SAC/B,CAAC,CAAC;KACJ;AACH,CAAC,CAAC;AAEF,OAAO,EACL,SAAS,EACT,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,GACpB,CAAC"}
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import type { Response } from 'firebase-functions';
|
|
2
|
-
import type { Customers } from '@cloudcommerce/types';
|
|
3
|
-
import type { Firestore } from 'firebase-admin/firestore';
|
|
4
|
-
// eslint-disable-next-line import/no-unresolved
|
|
5
|
-
import { Auth } from 'firebase-admin/auth';
|
|
6
|
-
import { logger } from 'firebase-functions';
|
|
7
|
-
import api from '@cloudcommerce/api';
|
|
8
|
-
import getEnv from '@cloudcommerce/firebase/lib/env';
|
|
9
|
-
|
|
10
|
-
const findCustomerByEmail = async (email: string | undefined) => {
|
|
11
|
-
try {
|
|
12
|
-
const { data } = await api.get(`customers?main_email=${email}`);
|
|
13
|
-
if (data.result.length) {
|
|
14
|
-
return data.result[0];
|
|
15
|
-
}
|
|
16
|
-
return null;
|
|
17
|
-
} catch (e) {
|
|
18
|
-
logger.error(e);
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const checkFirebaseAuth = async (
|
|
24
|
-
auth: Auth,
|
|
25
|
-
authToken: string | undefined | string [],
|
|
26
|
-
) => {
|
|
27
|
-
if (authToken && !Array.isArray(authToken)) {
|
|
28
|
-
try {
|
|
29
|
-
const customer = await auth.verifyIdToken(authToken);
|
|
30
|
-
return customer;
|
|
31
|
-
} catch (e) {
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const createCustomer = async (customer: Customers) => {
|
|
40
|
-
try {
|
|
41
|
-
const { data } = await api.post('customers', customer);
|
|
42
|
-
return data._id;
|
|
43
|
-
} catch (e) {
|
|
44
|
-
logger.error(e);
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const generateAccessToken = async (
|
|
50
|
-
firestore: Firestore,
|
|
51
|
-
customerId: string,
|
|
52
|
-
): Promise<null | {
|
|
53
|
-
customer_id: string,
|
|
54
|
-
access_token: string,
|
|
55
|
-
expires: string,
|
|
56
|
-
}> => {
|
|
57
|
-
const { apiAuth } = getEnv();
|
|
58
|
-
const docRef = firestore.doc(`customerTokens/${customerId}`);
|
|
59
|
-
const doc = await docRef.get();
|
|
60
|
-
const expires: string | undefined = doc.data()?.expires;
|
|
61
|
-
if (expires && new Date(expires).getTime() - Date.now() >= 2 * 60 * 1000) {
|
|
62
|
-
return doc.data() as { customer_id: string, access_token: string, expires: string };
|
|
63
|
-
}
|
|
64
|
-
try {
|
|
65
|
-
const { data } = await api({
|
|
66
|
-
endpoint: 'authenticate',
|
|
67
|
-
method: 'post',
|
|
68
|
-
body: {
|
|
69
|
-
_id: apiAuth.authenticationId,
|
|
70
|
-
api_key: apiAuth.apiKey,
|
|
71
|
-
customer_id: customerId,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
const accessToken = {
|
|
75
|
-
access_token: data.access_token,
|
|
76
|
-
expires: data.expires,
|
|
77
|
-
customer_id: customerId,
|
|
78
|
-
};
|
|
79
|
-
docRef.set(accessToken).catch(logger.error);
|
|
80
|
-
return accessToken;
|
|
81
|
-
} catch (e) {
|
|
82
|
-
logger.error(e);
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const getAuthCustomerApi = async (
|
|
88
|
-
firestore: Firestore,
|
|
89
|
-
authtoken: string | string[] | undefined,
|
|
90
|
-
authFirebase: Auth,
|
|
91
|
-
) => {
|
|
92
|
-
const customerFirebaseAuth = await checkFirebaseAuth(authFirebase, authtoken);
|
|
93
|
-
if (customerFirebaseAuth !== null && customerFirebaseAuth.email) {
|
|
94
|
-
const customer = await findCustomerByEmail(customerFirebaseAuth.email);
|
|
95
|
-
if (customer !== null) {
|
|
96
|
-
return generateAccessToken(firestore, customer._id);
|
|
97
|
-
}
|
|
98
|
-
const newCustomer = {
|
|
99
|
-
display_name: customerFirebaseAuth.name || '',
|
|
100
|
-
main_email: customerFirebaseAuth.email,
|
|
101
|
-
emails: [{
|
|
102
|
-
address: customerFirebaseAuth.email,
|
|
103
|
-
verified: customerFirebaseAuth.email_verified,
|
|
104
|
-
}],
|
|
105
|
-
oauth_providers: [{
|
|
106
|
-
provider: customerFirebaseAuth.firebase.sign_in_provider,
|
|
107
|
-
user_id: customerFirebaseAuth.user_id,
|
|
108
|
-
}],
|
|
109
|
-
} as Customers;
|
|
110
|
-
const customerId = await createCustomer(newCustomer);
|
|
111
|
-
if (customerId) {
|
|
112
|
-
return generateAccessToken(firestore, customerId);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
// TODO: Find customer by phone number, generate token if found, otherwise unauthorize
|
|
116
|
-
return null;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
const sendError = (
|
|
120
|
-
res: Response,
|
|
121
|
-
msg?: string,
|
|
122
|
-
status = 400,
|
|
123
|
-
) => {
|
|
124
|
-
if (msg) {
|
|
125
|
-
res.status(status).json({
|
|
126
|
-
status,
|
|
127
|
-
error: msg,
|
|
128
|
-
});
|
|
129
|
-
} else {
|
|
130
|
-
res.status(500).json({
|
|
131
|
-
status: 500,
|
|
132
|
-
error: 'Internal server error',
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
export {
|
|
138
|
-
sendError,
|
|
139
|
-
getAuthCustomerApi,
|
|
140
|
-
findCustomerByEmail,
|
|
141
|
-
generateAccessToken,
|
|
142
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{_ as i}from"./chunks/LoginOffcanvas.a1f3fe3b.js";import"./chunks/preload-helper.f15ab524.js";import"./chunks/runtime-dom.esm-bundler.62d623e7.js";/* empty css */export{i as default};
|