import { AUTH_SERVICE } from '../config/routes'

export type FailedResponse = {
  error?: string,
  ok: false,
}

export type SuccessResponse = {
  ok: true,
}

export const checkDevice = async (token: string) => {
  const url = `${AUTH_SERVICE}/authorize/check-device?access_token=${token}`

  const config = {
    method: 'GET',
  }

  const response = await fetch(url, config)

  const body: SuccessResponse | FailedResponse | undefined = await response.json()

  return (body && !body.ok) ? Promise.reject(body) : Promise.resolve()
}
