init commit,
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
# Get User Location
|
||||
|
||||
This example shows how you can get user location based on the IP provided in X-Forwarded-For header in a request.
|
||||
|
||||
You will need to signup for an account in https://ipinfo.io and provide it as `IPINFO_TOKEN` environment variable ([learn how to set environment variables to your functions](https://supabase.com/docs/guides/functions#secrets-and-environment-variables)).
|
||||
|
||||
## Develop locally
|
||||
|
||||
```bash
|
||||
supabase functions serve --env-file ./supabase/.env.local --no-verify-jwt
|
||||
```
|
||||
|
||||
Navigate to http://localhost:54321/functions/v1/location
|
||||
|
||||
## Deploy
|
||||
|
||||
```bash
|
||||
supabase functions deploy location --no-verify-jwt
|
||||
```
|
@@ -0,0 +1,28 @@
|
||||
// Follow this setup guide to integrate the Deno language server with your editor:
|
||||
// https://deno.land/manual/getting_started/setup_your_environment
|
||||
// This enables autocomplete, go to definition, etc.
|
||||
|
||||
function ips(req: Request) {
|
||||
return req.headers.get('x-forwarded-for')?.split(/\s*,\s*/)
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
const clientIps = ips(req) || ['']
|
||||
const res = await fetch(
|
||||
`https://ipinfo.io/${clientIps[0]}?token=${Deno.env.get('IPINFO_TOKEN')}`,
|
||||
{
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}
|
||||
)
|
||||
if (res.ok) {
|
||||
const { city, country } = await res.json()
|
||||
|
||||
return new Response(JSON.stringify(`You're accessing from ${city}, ${country}`), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
} else {
|
||||
return new Response(await res.text(), {
|
||||
status: 400,
|
||||
})
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user