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

How do you keep secrets? #107

Closed
kevinsimper opened this issue Oct 27, 2016 · 16 comments
Closed

How do you keep secrets? #107

kevinsimper opened this issue Oct 27, 2016 · 16 comments

Comments

@kevinsimper
Copy link

If I for example what to use the GIthub API, I don't want to expose my secret? How would I do that?

@ghost
Copy link

ghost commented Oct 27, 2016

@kevinsimper you can use Hashicorp Vault to store your secret key.

@kevinsimper
Copy link
Author

But I was talking about client/server? How does vault prevent that?

@atinux
Copy link

atinux commented Oct 27, 2016

I think one way of doing it is by setting environment variables.
And then you can use process.env.GITHUB_API_KEY for example (only available from the server side)

@luisrudge
Copy link

That won't work either. You need an api to proxy those requests to github.

@mping
Copy link

mping commented Oct 27, 2016

You will need to proxy calls to you server api.

@nkzawa
Copy link
Contributor

nkzawa commented Nov 14, 2016

environment variables should work.

@nkzawa nkzawa closed this as completed Nov 14, 2016
@sergical
Copy link

Would like to reopen this as environment variable will work on the first request, however, navigating using <Link> would make process.env.VAR undefined in the browser.

Any workarounds?

@timneutkens
Copy link
Member

@416serg This is the way to go on that 😄 https://github.com/zeit/next.js/tree/master/examples/with-universal-configuration

@sergical
Copy link

@timneutkens Thanks Tim!

I've set that up for it to work, which is awesome, however, I still am worried about passing secret keys to it as when I inspect the page with this structure, I still see the key on client.

Example:

env-config.js

const prod = process.env.NODE_ENV === 'production'

module.exports = {
  'API_KEY': prod ? 'companySecret' : 'companySecret'
}

index.js

/* global API_KEY */

import React from 'react'
import { Jumbotron, Button } from 'react-bootstrap'
import Link from 'next/link'
import 'isomorphic-fetch'
import Layout from '../components/layout'
import JobList from '../components/joblist'

export default class HomePage extends React.Component {
  static async getInitialProps () {
    console.log(API_KEY)
    const res = await fetch(`https://api.lever.co/v0/postings/${API_KEY}`)
    const json = await res.json()
    return { jobs: json }
  }

  render () {
    return (
      <Layout title="Careers at ClearCapital">
        <Jumbotron>
          <h1>Work at ClearCapital</h1>
        </Jumbotron>
        <JobList jobs={this.props.jobs} />
      </Layout>
    )
  }
}

Is there anything I can do to avoid exposing the secret key to the client, yet still have the functionality to go back and forth between <Link/>s

Thanks again! 😄

@timneutkens
Copy link
Member

@416serg the best thing you can do in that case is move the api call into a microservice and call that microservice. Possibly using https://github.com/zeit/micro 👍

@sergical
Copy link

@timneutkens is there an example where I can see how to connect to a micro service from my nextjs app?

@tim-phillips
Copy link
Contributor

@416serg you would simply use fetch as normal

https://github.com/zeit/next.js/tree/master/examples/data-fetch

@mattbrunetti
Copy link

@416serg I believe you could use browserify transforms (bpb || inline-process-browser) && unreachable-branch-transform (with transform-loader of course, to adapt them for use with webpack)

@tim-phillips
Copy link
Contributor

@mattbrunetti though that would mean the secret is in the browser. If the browser has the secret, the user has the secret.

@mattbrunetti
Copy link

@babenzele The combination is used to strip server-side-only code from your browser build.

source: const secret = process.browser ? 'nope, sorry' : 'I liked The Notebook'
after bpb: const secret = true ? 'nope, sorry' : 'I liked The Notebook'
after unreachable-branch-transform: const secret = 'nope, sorry'

@mattbrunetti
Copy link

@babenzele If you're using a a minifier like uglify i guess that would do the job of the second transform

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
ForsakenHarmony pushed a commit that referenced this issue Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants