<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David Poxon</title>
    <description>The latest articles on DEV Community by David Poxon (@olsnacky).</description>
    <link>https://dev.to/olsnacky</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F553023%2F4c4dc479-2d43-48a6-92f5-0715c2b60b3f.jpeg</url>
      <title>DEV Community: David Poxon</title>
      <link>https://dev.to/olsnacky</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olsnacky"/>
    <language>en</language>
    <item>
      <title>Send cookies in Electron + React app</title>
      <dc:creator>David Poxon</dc:creator>
      <pubDate>Mon, 04 Jan 2021 08:39:03 +0000</pubDate>
      <link>https://dev.to/olsnacky/send-cookies-in-electron-react-app-10el</link>
      <guid>https://dev.to/olsnacky/send-cookies-in-electron-react-app-10el</guid>
      <description>&lt;p&gt;In this article I discuss using cookies to make an authenticated web call to an external domain.&lt;br&gt;
Over the past few months I have been tinkering with an Electron + React app. I had never worked with these technologies before so mainly my goal has been to learn. I am using the opportunity to improve my developer experience at work. I like to call it my &lt;em&gt;developer console&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Anyway, I wanted to create a small tool inside the developer console that would allow me to create reproducible data configurations for the software I work on, i.e., a one-button-click system for creating complex, reproducible test data scenarios. This required me to communicate and authenticate with my local development environment, something I hadn't previously had to do in the developer console.&lt;/p&gt;

&lt;p&gt;Communication was an easy &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch"&gt;&lt;code&gt;fetch&lt;/code&gt; call&lt;/a&gt;, but the authentication element was the unknown. How could I send cookies for a domain as part of my fetch? A quick internet search didn't give me &lt;em&gt;exactly&lt;/em&gt; what I wanted, but I was able to piece together what I needed from various sources. Below is a snippet for how to set the cookies for a domain in Electron, and how to include them in a &lt;code&gt;fetch&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;electron&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;electron&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;performExternalRequest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cookieJar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;electron&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;remote&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultSession&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cookie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://youdomain.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-cookie-name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-cookie-value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nx"&gt;cookieJar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://your-api.domain/endpoint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;include&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A gist for the above is &lt;a href="https://gist.github.com/olsnacky/8f2bd20e916c01e1ec9575b42cf2f19f"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The key elements in the above are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;adding the cookie to the cookies for the session; and&lt;/li&gt;
&lt;li&gt;setting the &lt;code&gt;credentials&lt;/code&gt; property on the fetch request options to &lt;code&gt;include&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The former sets the cookie so it can be included in the request. The latter ensures the cookie is included in the request.&lt;/p&gt;

&lt;p&gt;Setting the &lt;code&gt;credentials&lt;/code&gt; option warrants a bit of discussion. Depending on your browser version, the default value for &lt;code&gt;credentials&lt;/code&gt; will either be &lt;code&gt;omit&lt;/code&gt; or &lt;code&gt;same-origin&lt;/code&gt;. If the former, then &lt;strong&gt;no cookie will be sent&lt;/strong&gt;. If the latter, and if your electron app is on a different domain than the server you want to communicate with, which is likely, then also &lt;strong&gt;no cookies will be sent to the server&lt;/strong&gt;. The only way to send the cookies to the server when your app is on a different domain is to set the &lt;code&gt;credentials&lt;/code&gt; option as &lt;code&gt;include&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For more information, see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.electronjs.org/docs/api/cookies"&gt;Electron documentation on cookies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch"&gt;Mozilla documentation on &lt;code&gt;fetch&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; While &lt;code&gt;fetch&lt;/code&gt; is not exclusive to React, I mention React in this post as it was a key element of the context in which my questions existed.&lt;/p&gt;

</description>
      <category>react</category>
      <category>electron</category>
      <category>webdev</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
