Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the googlemaps api key? #313

Open
jlemonz opened this issue Nov 9, 2024 · 5 comments
Open

Using the googlemaps api key? #313

jlemonz opened this issue Nov 9, 2024 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@jlemonz
Copy link

jlemonz commented Nov 9, 2024

📚 What are you trying to do?

Hi All,

I am really stuck with this. So i use the demo code

<ScriptGoogleMaps
    ref="googleMapsRef"
    :center="query"
    :markers="markers"
    :api-key="googleMapsApiKey"
    class="group w-full h-64 rounded-lg"
    above-the-fold
    @ready="handleReady"
  />

const googleMapsApiKey = process.env.NUXT_GOOGLE_MAPS_API_KEY;

but this doesnt work? When i use my api key directly in the file it works. But putting it in the .env i wont load the map? I thought placing your api key is not secure? Already asked chatgpt but no solution.

🔍 What have you tried?

I looked at the docs. Nothing about it. I tried chatgpt and different solutions nothing works.

ℹ️ Additional context

No response

@jlemonz jlemonz added the help wanted Extra attention is needed label Nov 9, 2024
@TheAlexLichter
Copy link
Member

You should use runtimeConfig

@harlan-zw
Copy link
Collaborator

The setup instructions need to be improved for this page. Using .env is supported but you need to follow the format of NUXT_PUBLIC_SCRIPTS_GOOGLE_MAPS_API_KEY

// nuxt.config.ts
export default defineNuxtConfig({
  // you need to provide a runtime config to access the environment variables
  runtimeConfig: {
    public: {
      scripts: {
        googleMaps: {
          apiKey: '', // NUXT_PUBLIC_SCRIPTS_GOOGLE_MAPS_API_KEY
        },
      },
    },
  },
})
# .env
NUXT_PUBLIC_SCRIPTS_GOOGLE_MAPS_API_KEY=<your-token>

I'll leave open to track doc update

@harlan-zw harlan-zw added documentation Improvements or additions to documentation and removed help wanted Extra attention is needed labels Nov 10, 2024
@jlemonz
Copy link
Author

jlemonz commented Nov 10, 2024

Thank you i got it working. Is using public not a security issue? I read that I can enable on google cloud what site can use the api key. Or is the key hidden in the script?

@TheAlexLichter
Copy link
Member

Thank you i got it working. Is using public not a security issue? I read that I can enable on google cloud what site can use the api key. Or is the key hidden in the script?

Google Maps needs a public API key. For other APIs you definitely don't want it public (e.g. the stripe private key). Talked a bit about that in this video

@harlan-zw
Copy link
Collaborator

Yes just to add on to this, make sure you lock down your google maps API key to specific hosts!

Very important step https://developers.google.com/maps/api-security-best-practices#restricting-api-keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants