You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

145 regels
5.4 KiB

  1. import useJwt from 'auth/jwt/coreUseJwt';
  2. /**
  3. * Return if user is logged in
  4. * This is completely up to you and how you want to store the token in your frontend application
  5. * e.g. If you are using cookies to store the application please update this function
  6. */
  7. // eslint-disable-next-line arrow-body-style
  8. export const hostname = 'localhost';
  9. const hostPort = '8090';
  10. export const hostPath = `http://${hostname}:${hostPort}`;
  11. export const apiPath = window.location.href.match("localhost:3000") ? `${hostPath}/api` : (window.location.href.match(":3000") ? "http://" + window.location.hostname + ":8090/api" : `/api`);
  12. export const paymentPath = window.location.href.match("localhost:3000") ? `${hostPath}/payment` : `/payment`;
  13. export const delBugMode = true;
  14. /**
  15. * Testing:
  16. * Domain: apigw-isit.staging-eid.gov.hk
  17. * URL: hk.gov.iamsmart.testapp://
  18. *
  19. * Production
  20. * Domain: apigw.iamsmart.gov.hk
  21. * URL: hk.gov.iamsmart://
  22. */
  23. export const iAmSmartPath = `https://apigw-isit.staging-eid.gov.hk`;
  24. export const iAmSmartAppPath = `hk.gov.iamsmart.testapp://`;
  25. export const clientId = "cf61fa7c121e4869966f69c8694b1cd2";
  26. export const iAmSmartCallbackPath = () => {
  27. let hostname = window.location.hostname;
  28. if (hostname.match("pnspsuat")) {
  29. hostname = "pnspsuat.gld.gov.hk";
  30. } else {
  31. hostname = "pnspsdev.gld.gov.hk";
  32. }
  33. return hostname;
  34. };
  35. export const getNonce = () => {
  36. let hostname = window.location.hostname;
  37. if (hostname.match("localhost")) {
  38. hostname = "pnspsuat.gld.gov.hk";
  39. }
  40. return hostname;
  41. };
  42. export const getBowserType = () => {
  43. // console.log(navigator.userAgent)
  44. // const regex = /Mobi|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Mi|huawei|Opera Mini|SAMSUNG|Samsung|SGH-[I|N|T]|GT-[I|N]|SM-[A|N|P|T|Z]|SHV-E|SCH-[I|J|R|S]|SPH-L/i;
  45. // if(!regex.test(navigator.userAgent))
  46. if (navigator.userAgent.indexOf("Edg") != -1) {
  47. if (navigator.userAgent.match(/Android/i)) return "Android_Edge"
  48. if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) return "iOS_Edge"
  49. return "PC_Browser"
  50. } else if (navigator.userAgent.indexOf("Chrome") != -1) {
  51. if (navigator.userAgent.match(/Android/i)) return "Android_Chrome"
  52. if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) return "iOS_Chrome"
  53. return "PC_Browser"
  54. } else if (navigator.userAgent.indexOf("Safari") != -1) {
  55. if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) return "iOS_Safari"
  56. return "PC_Browser"
  57. } else if (navigator.userAgent.indexOf("Firefox") != -1) {
  58. if (navigator.userAgent.match(/Android/i)) return "Android_Firefox"
  59. if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) return "iOS_Firefox"
  60. return "PC_Browser"
  61. } else if (navigator.userAgent.match(/SAMSUNG|Samsung|SGH-[I|N|T]|GT-[I|N]|SM-[A|N|P|T|Z]|SHV-E|SCH-[I|J|R|S]|SPH-L/i)) {
  62. return "Android_Samsung"
  63. } else if (navigator.userAgent.match(/huawei/i)) {
  64. return "Android_Huawei"
  65. } else if (navigator.userAgent.match(/Mi/i)) {
  66. return "Android_Xiaomi"
  67. }
  68. return "PC_Browser";
  69. }
  70. export const isAppBowser = () => {
  71. return "PC_Browser" != getBowserType();
  72. }
  73. export const isUserLoggedIn = () => {
  74. return localStorage.getItem('userData') && localStorage.getItem(useJwt.jwtConfig.storageTokenKeyName);
  75. };
  76. export const getUserData = () => JSON.parse(localStorage.getItem('userData'));
  77. export const isGranted = (auth) => {
  78. const abilities = getUserData() ? getUserData()["abilities"] : null;
  79. if (abilities == null || abilities.length == 0) return false;
  80. if (!Array.isArray(auth)) return _checkAuth(abilities, auth);
  81. if (auth.length > abilities.length) return false;
  82. let haveAuth = true;
  83. for (let i = 0; i < auth.length; i++) {
  84. haveAuth = _checkAuth(abilities, auth[i])
  85. if (!haveAuth) return haveAuth;
  86. }
  87. return haveAuth;
  88. };
  89. const _checkAuth = (abilities, auth) => {
  90. for (let i = 0; i < abilities.length; i++) {
  91. if (auth == abilities[i].actionSubjectCombo) return true;
  92. }
  93. return false;
  94. }
  95. export const isGrantedAny = (auth) => {
  96. const abilities = getUserData() ? getUserData()["abilities"] : null;
  97. if (abilities == null || abilities.length == 0) return false;
  98. if (!Array.isArray(auth)) return _checkAuth(abilities, auth);
  99. let haveAuth = false;
  100. for (let i = 0; i < auth.length; i++) {
  101. haveAuth = _checkAuth(abilities, auth[i])
  102. if (haveAuth) return haveAuth;
  103. }
  104. return haveAuth;
  105. };
  106. /**
  107. * This function is used for demo purpose route navigation
  108. * In real app you won't need this function because your app will navigate to same route for each users regardless of ability
  109. * Please note role field is just for showing purpose it's not used by anything in frontend
  110. * We are checking role just for ease
  111. * NOTE: If you have different pages to navigate based on user ability then this function can be useful. However, you need to update it.
  112. * @param {String} userRole Role of user
  113. */
  114. export const getHomeRouteForLoggedInUser = (userRole) => {
  115. if (userRole === 'admin') return '/';
  116. if (userRole === 'user') return '/';
  117. if (userRole === 'client') return { name: 'access-control' };
  118. return { name: 'auth-login' };
  119. };
  120. // open payment record
  121. export const local = { en: "en-us", zh: "zh-hk", cn: "zh-cn" };
  122. export const preferpaymentmethods = ['visa', 'mastercard', 'pps', 'creditcard', 'fps'];
  123. export const getPaymentMethod = (paymentMethod) => {
  124. if(paymentMethod == "online") return 'payOnlineMethod';
  125. if(paymentMethod == "demandNote") return 'payDnMethod';
  126. if(paymentMethod == "office") return 'payNPGOMethod';
  127. return "other";
  128. }