Skip to content

Commit

Permalink
Add IP status endpoint response schema
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad committed Jun 17, 2024
1 parent 880c1fb commit ad6dd98
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useBlockedPages(): (keyof typeof paths)[] {
// biome-ignore lint/correctness/useHookAtTopLevel: <explanation>
const vpnCheck = useVpnCheck({ authUrl: apiUrl })

if (vpnCheck.data && blockedPagesByCountryCode[vpnCheck.data.countryCode] !== undefined) {
if (vpnCheck.data?.countryCode && blockedPagesByCountryCode[vpnCheck.data.countryCode] !== undefined) {
return blockedPagesByCountryCode[vpnCheck.data.countryCode]!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ export function useIPAndAddressCheck(): UseIPAndAddressCheck {
return { blocked: true, reason: 'vpn-detected' }
}

if (vpnCheck.data && blockedCountryCodes.includes(vpnCheck.data.countryCode)) {
if (vpnCheck.data?.countryCode && blockedCountryCodes.includes(vpnCheck.data.countryCode)) {
return { blocked: true, reason: 'region-blocked', data: { countryCode: vpnCheck.data.countryCode } }
}

if (addressCheck.data?.addressAllowed === false) {
return { blocked: true, reason: 'address-not-allowed' }
}

if (vpnCheck.data && isCurrentPageBlocked) {
if (vpnCheck.data?.countryCode && isCurrentPageBlocked) {
return {
blocked: true,
reason: 'page-not-available-in-region',
Expand Down
17 changes: 9 additions & 8 deletions packages/app/src/features/compliance/logic/useVpnCheck.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { useQuery } from '@tanstack/react-query'

import { z } from 'zod'
import { ReadHookParams } from './types'

type VpnResponse = {
isConnectedToVpn: boolean
countryCode: string
countryCode: string | null | undefined
}

const apiResponseSchema = z.object({
is_vpn: z.boolean(),
country_code: z.string().nullable().optional(),
})

async function checkVpn(authUrl: string): Promise<VpnResponse> {
if (!authUrl) {
throw new Error('Missing auth URL')
}

let isConnectedToVpn = false
let countryCode = ''
// TODO is this the best way to get a user's IP address?
const ipRes = await fetch('https://api.ipify.org/?format=json')
if (!ipRes.ok) {
Expand All @@ -26,12 +30,9 @@ async function checkVpn(authUrl: string): Promise<VpnResponse> {
throw new Error('Could not fetch VPN status')
}

const { country_code, is_vpn } = (await vpnRes.json()) as any

countryCode = country_code
isConnectedToVpn = is_vpn
const { country_code, is_vpn } = apiResponseSchema.parse(await vpnRes.json())

return { countryCode, isConnectedToVpn }
return { countryCode: country_code, isConnectedToVpn: is_vpn }
}

type Props = ReadHookParams<VpnResponse> & { authUrl: string }
Expand Down

0 comments on commit ad6dd98

Please sign in to comment.