Skip to content

Commit

Permalink
Merge pull request #186 from pedrolivaresanchez/fix/add-auth-endpoint
Browse files Browse the repository at this point in the history
Fix/add auth endpoint
  • Loading branch information
patrickwebsdev authored Nov 10, 2024
2 parents f20201b + 5360914 commit 286bfa3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
36 changes: 21 additions & 15 deletions src/app/api/address/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextRequest } from 'next/server';
import { createClient } from '@/lib/supabase/server';

const mapsTranslationToDbTowns: { [key: string]: string } = {
Aldaya: 'Aldaia',
Expand All @@ -22,6 +23,16 @@ const GOOGLE_URL = `https://maps.googleapis.com/maps/api/geocode/json?key=${proc

export type AddressAndTown = { address: string; town: string };

async function checkAuthentication(): Promise<boolean> {
const supabase = await createClient();
const { data, error } = await supabase.auth.getUser();

if (error || !data?.user) {
return false;
}
return true;
}

function normalizeData({ address, town }: AddressAndTown): AddressAndTown {
const normalizedTown = Object.keys(mapsTranslationToDbTowns).includes(town) ? mapsTranslationToDbTowns[town] : town;
const normalizedAddress = address.replace(town, normalizedTown);
Expand Down Expand Up @@ -57,34 +68,29 @@ function extractAddressAndTown(googleResponse: any) {
return { address, town };
}

export async function POST(request: NextRequest) {
export async function POST(request: NextRequest, response: any) {
// will return Response object on error
if (!(await checkAuthentication())) {
return Response.json({ error: 'Unauthenticated: User must be logged in' }, { status: 401 });
}

const body = await request.json();
if (!body.latitude || !body.longitude) {
return Response.json({
error: 'Latitude and longitude are mandatory fields!',
});
return Response.json({ error: 'Latitude and longitude are mandatory fields!' }, { status: 401 });
}

try {
const response = await fetch(`${GOOGLE_URL}${body.latitude},${body.longitude}`, {
headers: {
Referer: request.headers.get('Referer') ?? '',
},
}).then((value) => value.json());
const response = await fetch(`${GOOGLE_URL}${body.latitude},${body.longitude}`).then((value) => value.json());

if (response.error_message) {
return Response.json({
error: response.error_message,
});
return Response.json({ error: `Error de google: ${response.error_message}` }, { status: 502 });
}

const extractedData = extractAddressAndTown(response);

return Response.json(extractedData);
} catch (exception) {
console.error(exception);
return Response.json({
error: 'An error occured calling google - check logs',
});
return Response.json({ error: 'An error occured calling google - check logs' }, { status: 500 });
}
}
2 changes: 1 addition & 1 deletion src/app/casos-activos/solicitudes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function Solicitudes() {
))}
</select>
</div>
{/*<div className="flex flex-row flex-1 justify-end">
{/*<div className="flex flex-row flex-1 justify-end">
<Toggle
handleChange={handleToggleChange}
checked={isStringTrue(filtroData.soloSinAsignar)}
Expand Down
5 changes: 3 additions & 2 deletions src/components/AddressMap.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use client';

import GeoLocationMap, { LngLat } from '@/components/map/GeolocationMap';
import { useRef, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { locationService } from '@/lib/service';
import { useDebouncedFunction, useThrottledFunction } from '@/helpers/hooks';
import GooglePlacesAutocomplete from 'react-google-places-autocomplete';
import { OnChangeValue } from 'react-select';
import { supabase } from '../lib/supabase/client';

export type AddressMapProps = {
onNewAddressDescriptor: (onNewAddressDescriptor: AddressDescriptor) => void;
Expand Down Expand Up @@ -66,7 +67,7 @@ export default function AddressMap({ onNewAddressDescriptor, initialAddressDescr
String(coordinates.lat),
);
if (error) {
throw { message: `Error inesperado con la api de google: ${error}` };
throw { message: `Error inesperado con el geocoding endpoint: ${error}` };
}

const newAddressDescriptor: AddressDescriptor = {
Expand Down

0 comments on commit 286bfa3

Please sign in to comment.