@hhgtech/hhg-components
Version:
Hello Health Group common components
184 lines (180 loc) • 7.94 kB
JavaScript
import { a as __awaiter } from './tslib.es6-ea4dfe68.js';
import { isProduction } from './constantsIsProduction.js';
import { e as normalizeLinkSlash, a as getCurrentSsoUrl } from './normalizeLink-593b397a.js';
import { L as LOCALE_SPECS, e as BEARER_REFRESH_TOKEN_COOKIE } from './index-5e947517.js';
import { getCookie, setCookie } from './miscCookieHelper.js';
import { L as LOCALE } from './Locale-f270bd9d.js';
import { domainLocales } from './constantsDomainLocales.js';
const SSO_V2_PATHS = {
LOGIN_BY_SOCIAL: 'member/social-login',
EMAIL: {
REGISTER_BY_SEND_OTP: 'login/login-with-email',
LOGIN_BY_PASSWORD: 'member/login',
VERIFY_BY_OTP: 'login/email-verify-code',
SEND_OTP: 'login/email-resend-otp-code', // FOR RESEND OTP IN CASE FAIL
},
PHONE: {
REGISTER_BY_SEND_OTP: 'login/login-with-sms',
SEND_OTP: 'login/sms/sent-otp',
VERIFY_BY_OTP: 'login/phone-verify-code',
LOGIN_BY_PASSWORD: 'member/login-by-phone',
UPDATE_PHONE_SEND_OTP: 'login/sms-social/sent-otp',
UPDATE_PHONE_VERIFY_OTP: 'login/sms-social/phone-verify-code',
},
WHATSAPP: {
REGISTER_BY_SEND_OTP: 'login/login-with-whatsapp',
SEND_OTP: 'login/whatsapp/sent-otp',
UPDATE_PHONE_SEND_OTP: 'login/sms-social/whatsapp/sent-otp',
UPDATE_PHONE_VERIFY_OTP: 'login/sms-social/whatsapp-verify-code',
VERIFY_BY_OTP: 'login/whatsapp-verify-code',
},
ZALO: {
REGISTER_BY_SEND_OTP: 'login/login-with-zalo',
SEND_OTP: 'login/zalo/sent-otp',
VERIFY_BY_OTP: 'login/zalo-verify-code',
UPDATE_PHONE_SEND_OTP: 'login/sms-social/zalo/sent-otp',
UPDATE_PHONE_VERIFY_OTP: 'login/sms-social/zalo-verify-code',
},
RESET_PASSWORD: 'login/reset-password',
CHECK_USER_EXIST: 'member/check-user-exist',
UPDATE_USER_INFO: 'login/update-first-info',
SETUP_USER_INFO: 'login/setup-first-info',
ECOM_CARE: {
SEND_OTP: 'login/ecom-care/send-email-otp-code',
VERIFY_OTP: 'login/ecom-care/email-verify-code',
RESEND_OTP: 'login/ecom-care/resend-email-otp-code',
},
};
const locale = process.env.NEXT_PUBLIC_DEPLOY_LOCALE || 'vi-VN';
const websiteId = {
[LOCALE.Vietnam]: '1',
[LOCALE.Indonesia]: '2',
[LOCALE.Malaysia]: '3',
}[locale];
LOCALE_SPECS[locale].PHONE;
const getSSOV2ApiUrl = (path) => {
return normalizeLinkSlash(`${getCurrentSsoUrl(locale)}/api/v2/${path}`);
};
const getSSOApiUrl = (path) => {
return normalizeLinkSlash(`${getCurrentSsoUrl(locale)}/api/${path}`);
};
const BEARER_TOKEN_COOKIE = 'hhg_user_token';
// NOTICE: RUN ONLY CLIENT SIDE
const fetchSSOV2Api = (path, options = {}, noAuth) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
let response;
const authToken = noAuth
? ''
: (_a = document.cookie
.split('; ')
.find((row) => row.startsWith(BEARER_TOKEN_COOKIE))) === null || _a === void 0 ? void 0 : _a.split('=')[1];
try {
if (options.body instanceof FormData) {
options.body.append('ga_client_id', getCookie('_ga') || '');
options.body.append('referrer', window.location.href);
}
response = yield fetch(getSSOV2ApiUrl(path), authToken
? Object.assign(Object.assign({}, options), { headers: Object.assign({ Authorization: `Bearer ${authToken}` }, options === null || options === void 0 ? void 0 : options.headers) }) : options);
}
catch (error) {
console.error(error);
return null;
}
return response === null || response === void 0 ? void 0 : response.json();
});
const fetchSSOApi = (path, options = {}, noAuth) => __awaiter(void 0, void 0, void 0, function* () {
var _b;
let response;
const authToken = noAuth
? ''
: (_b = document.cookie
.split('; ')
.find((row) => row.startsWith(BEARER_TOKEN_COOKIE))) === null || _b === void 0 ? void 0 : _b.split('=')[1];
try {
response = yield fetch(getSSOApiUrl(path), authToken
? Object.assign(Object.assign({}, options), { headers: Object.assign({ Authorization: `Bearer ${authToken}` }, options === null || options === void 0 ? void 0 : options.headers) }) : options);
}
catch (error) {
console.error(error);
return null;
}
return response === null || response === void 0 ? void 0 : response.json();
});
const resetPassword = (password, confirmPassword) => __awaiter(void 0, void 0, void 0, function* () {
const formData = new FormData();
formData.append('password', password);
formData.append('confirm-password', confirmPassword);
return fetchSSOV2Api(SSO_V2_PATHS.RESET_PASSWORD, {
method: 'POST',
body: formData,
});
});
// "email":"kenny_test2201_11@mailinator.com"
// "phone_number":"906911910"
const checkUserExist = ({ email, phone, // no +
}) => __awaiter(void 0, void 0, void 0, function* () {
// "provider": "email",
// "is_pass": true,
// "is_status": "Active"
const formData = new FormData();
if (email) {
formData.append('email', email);
}
else {
formData.append('phone_number', phone);
}
return fetchSSOV2Api(SSO_V2_PATHS.CHECK_USER_EXIST, {
method: 'POST',
body: formData,
});
});
// "name": "Huy Tran 888",
// "birthday": "1989-04-26",
// "email":"kenny_test2201_02@mailinator.com", // optional chỉ require khi source là care | ecom
// "password": "Kenny2103",
// "confirm_password":"Kenny2103",
// "phone_number":"906911912", // require khi user register bằng email hoặc social
// "area_code":"+84", // require khi có phone number
// "gender": 1,
// "validate": false, // true validate sau khi validate xong truyền false để update
// "source": "care" | "ecom" // default sẽ ko require email. nếu source là care sẽ require email. ==> support cho business care
const updateUserInfo = (data) => __awaiter(void 0, void 0, void 0, function* () {
const formData = new FormData();
Object.keys(data).forEach((key) => {
const value = data[key];
if (typeof value !== 'undefined' &&
value !== null &&
(typeof value !== 'string' || !!value.trim())) {
formData.append(key, data[key]);
}
});
return fetchSSOV2Api(SSO_V2_PATHS.SETUP_USER_INFO, {
method: 'POST',
body: formData,
});
});
const GoogleAuthClientId = isProduction
? '289442006438-040a42cbidr6v5d178f3iqi9q95821r3.apps.googleusercontent.com'
: '289442006438-cihobuq1h3r4426pur7u0e43fqdnep59.apps.googleusercontent.com';
const FacebookAppId = '2759888574045636';
const getUserInfo = () => __awaiter(void 0, void 0, void 0, function* () {
return fetchSSOApi('user/profile', {
method: 'POST',
});
});
const handleAuthSuccess = (res) => {
(res === null || res === void 0 ? void 0 : res.token) &&
setCookie(BEARER_TOKEN_COOKIE, res.token, {
domain: window.location.hostname === 'localhost'
? 'localhost'
: `.${domainLocales[locale]}`,
expires: new Date(res.expire_in * 1000).toUTCString(),
});
(res === null || res === void 0 ? void 0 : res.refresh_token) &&
setCookie(BEARER_REFRESH_TOKEN_COOKIE, res.refresh_token, {
domain: window.location.hostname === 'localhost'
? 'localhost'
: `.${domainLocales[locale]}`,
});
};
export { BEARER_TOKEN_COOKIE as B, FacebookAppId as F, GoogleAuthClientId as G, SSO_V2_PATHS as S, checkUserExist as c, fetchSSOV2Api as f, getUserInfo as g, handleAuthSuccess as h, locale as l, resetPassword as r, updateUserInfo as u, websiteId as w };