In this section, we're just going to add the public key that we generated to our PhoenixChat component, then update our component to handle that key.
In phoenix-chat-frontend, all we need to do is add the token to our PhoenixChat component. Obviously, add the proper token instead of NlkfVrAM9/.
...export class Home extends React.Component { ... render() { if (this.props.user.email) { return ( <Chat> <PhoenixChat token="NlkfVrAM9/" /> </Chat> ) } return ( <div className={style.leader}> <h1 className={style.title}>Phoenix Chat</h1> { this.state.formState === "signup" ? <Signup /> : null } { this.state.formState === "login" ? <Login /> : null } { this.renderToggleContent() } <PhoenixChat token="NlkfVrAM9/" /> <img role="presentation" className={style.circles} src="/learnphoenix-static-assets/images/circles-full.png" /> </div> ) }}...Since we're already passing this.props.user as the params in our Chat component when we connect to the socket, we don't need to change anything else.
And that's it for our frontend. Now your phoeinx-chat component has access to token via this.props.token. Make sure you have npm run watch going in your phoenix-chat directory so your app will update when you make changes.
Now all we need to do is add the 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.
...export class PhoenixChat extends React.Component { ... componentDidMount() { if (!localStorage.phoenix_chat_uuid) { localStorage.phoenix_chat_uuid = uuid.v4() } this.uuid = localStorage.phoenix_chat_uuid const params = { uuid: this.uuid, public_key: this.props.token } this.socket = new Socket("ws://localhost:4000/socket", { params }) this.socket.connect() this.configureChannels(this.uuid) } ...}...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.
Previous: Create Organization from Settings RouteNext: Routing Messages Using API Keys