{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/pass-api-key-from-frontend","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Pass API Key from Frontend\",\n  \"order\": 47,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"react\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Pass API Key from Frontend\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Add API key to phoenix-chat\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Pass key via socket\")), mdx(\"p\", null, \"In this section, we're just going to add the public key that we generated to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"PhoenixChat\"), \" component, then update our component to handle that key.\"), mdx(\"h2\", null, \"Add token to PhoenixChat\"), mdx(\"p\", null, \"In \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat-frontend\"), \", all we need to do is add the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"token\"), \" to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"PhoenixChat\"), \" component. Obviously, add the proper token instead of \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"NlkfVrAM9/\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n\\nexport class Home extends React.Component {\\n  ...\\n\\n  render() {\\n    if (this.props.user.email) {\\n      return (\\n        <Chat>\\n          <PhoenixChat token=\\\"NlkfVrAM9/\\\" />\\n        </Chat>\\n      )\\n    }\\n    return (\\n      <div className={style.leader}>\\n        <h1 className={style.title}>Phoenix Chat</h1>\\n        { this.state.formState === \\\"signup\\\" ? <Signup /> : null }\\n        { this.state.formState === \\\"login\\\" ? <Login /> : null }\\n        { this.renderToggleContent() }\\n        <PhoenixChat token=\\\"NlkfVrAM9/\\\" />\\n        <img\\n          role=\\\"presentation\\\"\\n          className={style.circles}\\n          src=\\\"/learnphoenix-static-assets/images/circles-full.png\\\" />\\n      </div>\\n    )\\n  }\\n}\\n...\\n\")), mdx(\"p\", null, \"Since we're already passing \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"this.props.user\"), \" as the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"params\"), \" in our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component when we connect to the socket, we don't need to change anything else.\"), mdx(\"p\", null, \"And that's it for our frontend. Now your \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoeinx-chat\"), \" component has access to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"token\"), \" via \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"this.props.token\"), \". Make sure you have \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm run watch\"), \" going in your \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat\"), \" directory so your app will update when you make changes.\"), mdx(\"h2\", null, \"Update phoenix-chat\"), mdx(\"p\", null, \"Now all we need to do is add the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"token\"), \" to our parameters before passing it along to the socket. Alternatively, we could pass the public key along with every message, but passing it along in the socket will save us some bandwidth.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n\\nexport class PhoenixChat extends React.Component {\\n  ...\\n\\n  componentDidMount() {\\n    if (!localStorage.phoenix_chat_uuid) {\\n      localStorage.phoenix_chat_uuid = uuid.v4()\\n    }\\n\\n    this.uuid = localStorage.phoenix_chat_uuid\\n    const params = { uuid: this.uuid, public_key: this.props.token }\\n    this.socket = new Socket(\\\"ws://localhost:4000/socket\\\", { params })\\n    this.socket.connect()\\n\\n    this.configureChannels(this.uuid)\\n  }\\n  ...\\n\\n}\\n...\\n\")), mdx(\"p\", null, \"And believe it or not, that's it! We're done configuring the frontend with API keys. The rest of the work will be in our API to route messages to the appropriate organization.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Pass API Key from Frontend Add API key to phoenix-chat Pass key via socket In this section, we're just going to add the public key that we…","frontmatter":{"title":"Pass API Key from Frontend","order":47,"active":true,"free":false,"tech":["react"]}}},"pageContext":{"slug":"pass-api-key-from-frontend","prevNode":{"frontmatter":{"title":"Create Organization from Settings Route","active":true,"free":false,"order":46,"tech":["react","redux"]},"fields":{"slug":"create-organization-from-settings-route"}},"nextNode":{"frontmatter":{"title":"Routing Messages Using API Keys","active":true,"free":false,"order":48,"tech":["phoenix"]},"fields":{"slug":"routing-messages-using-api-keys"}}}},"staticQueryHashes":["1723017916"]}