Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #35 from NEARFoundation/KTA-100-bug-country-missing
Browse files Browse the repository at this point in the history
Kta 100 bug country missing
  • Loading branch information
sandoche authored Dec 1, 2022
2 parents 4b75bb6 + 1eb7604 commit 0e303e1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "near-kyc-onfido",
"version": "1.1.5",
"version": "1.1.6",
"private": true,
"scripts": {
"dev": "next dev",
Expand All @@ -19,6 +19,9 @@
"dependencies": {
"@onfido/api": "^2.1.1",
"@tanstack/react-query": "^4.10.1",
"add": "^2.0.6",
"country-by-timezone": "^1.0.0",
"country-code-lookup": "^0.0.20",
"edge-csrf": "^0.2.1",
"next": "^12.2.0",
"next-swagger-doc": "^0.3.6",
Expand All @@ -27,7 +30,8 @@
"react-bootstrap": "^2.5.0",
"react-dom": "^18.1.0",
"react-error-boundary": "^3.1.4",
"react-hook-form": "^7.39.1"
"react-hook-form": "^7.39.1",
"yarn": "^1.22.19"
},
"devDependencies": {
"@faker-js/faker": "^7.6.0",
Expand Down
4 changes: 4 additions & 0 deletions pages/api/generate-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { COOKIE_NUMBER_OF_TRIES_NAME, COOKIES_EXPIRATION_TIME, MAX_NUMBER_OF_TRI
import getOnfido from '../../helpers/onfido';
import type ApplicantProperties from '../../types/ApplicantProperties';
import type ApplicantTokenPair from '../../types/ApplicantTokenPair';
import getCountryCodeFromTimezone from '../../utils/getCountryCodeFromTimezone';
import { FORBIDDEN, SERVER_ERROR, SUCCESS } from '../../utils/statusCodes';

const endpointName = 'generate-token';
Expand Down Expand Up @@ -50,11 +51,14 @@ const onfido = getOnfido();
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApplicantTokenPair | unknown>) {
try {
const ipAddress = req.headers['x-real-ip'] ?? req.connection.remoteAddress;
const countryOfResidence = getCountryCodeFromTimezone(req.body.timezone);
console.log('countryOfResidence', countryOfResidence);

const applicantProperties: ApplicantProperties = {
...req.body,
location: {
ipAddress,
countryOfResidence,
},
consents: [
{
Expand Down
5 changes: 4 additions & 1 deletion services/apiService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type ApplicantProperties from '../types/ApplicantProperties';
import type ApplicantTokenPair from '../types/ApplicantTokenPair';
import type { CheckResults } from '../types/CheckResults';
import getTimezoneFromBrowser from '../utils/getTimezoneFromBrowser';

const tokenFactoryUrl = process.env.NEXT_PUBLIC_TOKEN_FACTORY_URL ?? '';

Expand All @@ -21,9 +22,11 @@ export const initCheck = async (data: { applicantId: string; csrf_token: string
};

export const getToken = async (applicantProperties: ApplicantProperties): Promise<ApplicantTokenPair> => {
const timezone = getTimezoneFromBrowser();

const tokenOptions = {
method: 'POST',
body: JSON.stringify(applicantProperties),
body: JSON.stringify({ ...applicantProperties, timezone }),
headers: {
'Content-Type': 'application/json',
},
Expand Down
27 changes: 27 additions & 0 deletions utils/getCountryCodeFromTimezone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import getCountryByTimezone from 'country-by-timezone';
import lookup from 'country-code-lookup';

const DEFAULT_COUNTRY = 'USA';

const getCodeFromTimezone = (timezone: string) => {
const countryData = getCountryByTimezone(timezone);
return countryData.countryCode;
};

const getIsoCodeFromCode = (code: string) => {
const countryData = lookup.byIso(code);
return countryData?.iso3;
};

const getCountryCodeFromTimezone = (timezone: string): string => {
try {
const code = getCodeFromTimezone(timezone);
const isoCode = getIsoCodeFromCode(code);

return isoCode ?? DEFAULT_COUNTRY;
} catch (error) {
return DEFAULT_COUNTRY;
}
};

export default getCountryCodeFromTimezone;
5 changes: 5 additions & 0 deletions utils/getTimezoneFromBrowser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getTimezoneFromBrowser = () => {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
};

export default getTimezoneFromBrowser;
27 changes: 27 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,11 @@ acorn@^8.8.0:
resolved "https://registry.npmjs.org/acorn/-/acorn-8.8.0.tgz"
integrity sha512-QOxyigPVrpZ2GXT+PFyZTl6TtOFc5egxHIP9IlQ+RbupQuX4RkT/Bee4/kQuC02Xkzg84JcT7oLYtDIQxp+v7w==

add@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/add/-/add-2.0.6.tgz#248f0a9f6e5a528ef2295dbeec30532130ae2235"
integrity sha512-j5QzrmsokwWWp6kUcJQySpbG+xfOBqqKnup3OIk1pz+kB/80SLorZ9V8zHFLO92Lcd+hbvq8bT+zOGoPkmBV0Q==

agent-base@6:
version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
Expand Down Expand Up @@ -2391,6 +2396,18 @@ core-js@^3.21.1:
resolved "https://registry.npmjs.org/core-js/-/core-js-3.25.5.tgz"
integrity sha512-nbm6eZSjm+ZuBQxCUPQKQCoUEfFOXjUZ8dTTyikyKaWrTYmAVbykQfwsKE5dBK88u3QCkCrzsx/PPlKfhsvgpw==

country-by-timezone@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/country-by-timezone/-/country-by-timezone-1.0.0.tgz#78ef5d2b02fa2d8ee9d25db92a50f58085dccd2c"
integrity sha512-FauzrEiUuknjZ/oKX4ugp1czB1OizDMGfEMtpoGeIxr5b4iJAObLokCgNJAxXbrXmaoGwiptm6+r5by47s+FCA==
dependencies:
esm-utils "^2.0.0"

country-code-lookup@^0.0.20:
version "0.0.20"
resolved "https://registry.yarnpkg.com/country-code-lookup/-/country-code-lookup-0.0.20.tgz#d04c71cd94de4bd44dab6822f637638ed3e55359"
integrity sha512-mcNlekdiVyiKO0lrjj3FrZ36vxlwjUwXTHpdRtVu9917SAYsHrlORmpHdPXeEXwTmw/yiKiTX4t6LC5uP5KVFg==

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz"
Expand Down Expand Up @@ -2981,6 +2998,11 @@ eslint@^8.24.0:
strip-json-comments "^3.1.0"
text-table "^0.2.0"

esm-utils@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/esm-utils/-/esm-utils-2.2.0.tgz#c950c0f3ea191f3830f0187f246b0cf2620c1e66"
integrity sha512-kYj4yNRo4W3by0f1mj4AfRh1nsRTTpQG921Ik3AfyUq6upGlkI1fnMLypHn6XtFzZPdCYH1k9mtQA5MyZF9m+w==

espree@^9.4.0:
version "9.4.0"
resolved "https://registry.npmjs.org/espree/-/espree-9.4.0.tgz"
Expand Down Expand Up @@ -5793,6 +5815,11 @@ yargs@^17.3.1:
y18n "^5.0.5"
yargs-parser "^21.1.1"

yarn@^1.22.19:
version "1.22.19"
resolved "https://registry.yarnpkg.com/yarn/-/yarn-1.22.19.tgz#4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
integrity sha512-/0V5q0WbslqnwP91tirOvldvYISzaqhClxzyUKXYxs07yUILIs5jx/k6CFe8bvKSkds5w+eiOqta39Wk3WxdcQ==

yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz"
Expand Down

0 comments on commit 0e303e1

Please sign in to comment.