<?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: tbonesteaks</title>
    <description>The latest articles on DEV Community by tbonesteaks (@tbonesteaks).</description>
    <link>https://dev.to/tbonesteaks</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%2F775132%2Fe28fb306-f9af-45df-a309-0207bf81c108.jpeg</url>
      <title>DEV Community: tbonesteaks</title>
      <link>https://dev.to/tbonesteaks</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tbonesteaks"/>
    <language>en</language>
    <item>
      <title>Authentication with Supabase and SvelteKit (Part 1) - Development Environment Setup</title>
      <dc:creator>tbonesteaks</dc:creator>
      <pubDate>Thu, 16 Dec 2021 18:42:38 +0000</pubDate>
      <link>https://dev.to/tbonesteaks/authentication-with-supabase-and-sveltekit-part-1-development-environment-setup-b32</link>
      <guid>https://dev.to/tbonesteaks/authentication-with-supabase-and-sveltekit-part-1-development-environment-setup-b32</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6zw7okp6jbv3m71myi2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl6zw7okp6jbv3m71myi2.gif" alt="gandalf you shall not pass gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  project preamble
&lt;/h2&gt;

&lt;p&gt;I am not a web developer by trade, my day job consists of me hacking away at open source telecom projects. But, I have always wanted to build my own link aggregator site like reddit, lobste.rs, and hackernews. But, in order to do that I have got caught up in the cycle of starting the project over and over with a different web framework every couple of months. It's mostly a bad habit, and probably laziness too. Feels like a kind of a self-sabatoge sometimes too.&lt;/p&gt;

&lt;p&gt;But, nonetheless, I am putting all that behind me. I got on a kick trying to learn all I could about postgresql since it's a tool that would give me so much leverage in any project, and that led to me finding postgREST, which led me to &lt;a href="//supabase.io"&gt;supabase&lt;/a&gt;. Man, what a cool product. It is minimal enough where you don't feel like the appplication is doing everything for you, but it makes the difficult things much easier. One example being authentication, as we will see.&lt;/p&gt;

&lt;p&gt;supabase is obviouisly not a web framework. It is a &lt;a href="https://supabase.com/docs/architecture" rel="noopener noreferrer"&gt;collection of great open source projects&lt;/a&gt; tied together very nicely. See below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsupabase.com%2Fdocs%2Fassets%2Fimages%2Fsupabase-architecture-9050a7317e9ec7efb7807f5194122e48.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsupabase.com%2Fdocs%2Fassets%2Fimages%2Fsupabase-architecture-9050a7317e9ec7efb7807f5194122e48.png" alt="Supabase Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have messed around with React, Vue, and Svelte at different times. Just playing around with them. Nothing serious. And Svelte, like supabase, seemed to have just enough magic to make tough things easier, but was minimal enough that I felt like I wasn't getting 'project lock-in' (if that's a thing).&lt;/p&gt;

&lt;h2&gt;
  
  
  tl;dr
&lt;/h2&gt;

&lt;p&gt;Not a web developer. Didn't know what tools to use. Found &lt;a href="//supabase.io"&gt;supabase&lt;/a&gt; and &lt;a href="//kit.svelte.dev"&gt;sveltekit&lt;/a&gt;. Liked both. Chose them.&lt;/p&gt;

&lt;p&gt;I won't be going in-depth on &lt;a href="//supabase.io"&gt;supabase&lt;/a&gt; and &lt;a href="//kit.svelte.dev"&gt;sveltekit&lt;/a&gt;. You can look them both up yourself and dive in. I am only trying to document how I got things working with these tools as clearly as possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Note on Caddy Server
&lt;/h3&gt;

&lt;p&gt;This tutorial will also be using &lt;a href="//caddyserver.com"&gt;Caddy Server&lt;/a&gt; to make the local development environment easier to work with. And I think the configuration will work for production deployment too (we shall see).&lt;/p&gt;

&lt;h2&gt;
  
  
  Get started
&lt;/h2&gt;

&lt;p&gt;To get started, we will need the below tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;docker&lt;/li&gt;
&lt;li&gt;node.js / npm (or &lt;a href="//pnpm.io"&gt;pnpm&lt;/a&gt;, check it out. It's pretty cool)&lt;/li&gt;
&lt;li&gt;caddy server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'll leave installing the tools to you for your environment. I am assuming you are familiar enough or experienced enough (or determined enough) to get the tools working on your own. You can email me if you want help. I will try my best to respond.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Build SvelteKit Project
&lt;/h2&gt;

&lt;p&gt;Now build a new sveltekit project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init svelte@next supabase-auth-example
&lt;span class="nb"&gt;cd &lt;/span&gt;supabase-auth-example
npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Change default port
&lt;/h2&gt;

&lt;p&gt;The supabase studio app runs on port 3000 just like sveltekit default does. Let's change that so everything can run.&lt;/p&gt;

&lt;p&gt;Go to package.json and add &lt;code&gt;--port 3333&lt;/code&gt; to the end of the &lt;code&gt;dev&lt;/code&gt; and &lt;code&gt;preview&lt;/code&gt; scripts.&lt;/p&gt;

&lt;p&gt;Note: you can use whatever port you want that's available, but just make note because we will need it later for the caddy server config.&lt;/p&gt;

&lt;p&gt;Change this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"svelte-kit dev"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"svelte-kit build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"preview"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"svelte-kit preview"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"lint"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prettier --ignore-path .gitignore --check --plugin-search-dir=. . &amp;amp;&amp;amp; eslint --ignore-path .gitignore ."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prettier --ignore-path .gitignore --write --plugin-search-dir=. ."&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"svelte-kit dev --port 3333"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"svelte-kit build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"preview"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"svelte-kit preview --port 3333"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"lint"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prettier --ignore-path .gitignore --check --plugin-search-dir=. . &amp;amp;&amp;amp; eslint --ignore-path .gitignore ."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"format"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prettier --ignore-path .gitignore --write --plugin-search-dir=. ."&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Setup Supabase Docker Network
&lt;/h2&gt;

&lt;p&gt;Supabase has an easy to use docker setup for spinning up an instance of supabase and all its componenets.&lt;/p&gt;

&lt;p&gt;The full documentation is &lt;a href="https://supabase.com/docs/guides/hosting/docker" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Below are the instructions from there (probably want to move to a different directory for this).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone &lt;span class="nt"&gt;--depth&lt;/span&gt; 1 https://github.com/supabase/supabase

&lt;span class="nb"&gt;cd &lt;/span&gt;supabase/docker

&lt;span class="nb"&gt;cp&lt;/span&gt; .env.example .env

docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can go to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; to access the Supabase Studio app. We won't use it too much right now. But, its there for when we will need it.&lt;/p&gt;

&lt;p&gt;If you wanted to keep everything together in one repo, you could add supabase as a submodule in the sveltekit project directory/repo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git submodule add https://github.com/supabase/supabase supabase
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Setup Caddy Server
&lt;/h2&gt;

&lt;p&gt;Head over the &lt;a href="https://caddyserver.com/" rel="noopener noreferrer"&gt;Caddy Server website&lt;/a&gt; to download and install the executable.&lt;/p&gt;

&lt;p&gt;In the root of your sveltekit project create a new file called Caddyfile, and put this in there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# To get debug logging on caddy, uncommet below
#{
#   debug
#}

localho.st:8888 {

  @supabase {
      path_regexp supabase ^/(auth|rest|realtime|storage|pg)/.*$
  }

  # reverse proxy requests to supabase paths to the supabase kong http endpoint
  reverse_proxy @supabase http://localho.st:8000 

  # all other request should go to the sveltekit server
    reverse_proxy localho.st:3333
  tls internal
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Fun Fact about localho.st and caddy
&lt;/h3&gt;

&lt;p&gt;Notice how we defined our domain in the Caddyfile as localho.st? localho.st is a domain that automatically routes to localhost, and Caddy will setup a TLS cert for you automactically too. I thought that was pretty neat.&lt;/p&gt;

&lt;p&gt;So, with the simple configuration above, you get a local HTTPS website running without running into any &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="noopener noreferrer"&gt;CORS&lt;/a&gt; issues in the browser when trying to reach localhost APIs from the client.&lt;/p&gt;

&lt;p&gt;This setup gives you something very close to what a production setup could look like.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final caveat for running svelte in dev mode
&lt;/h2&gt;

&lt;p&gt;Because the dev mode of sveltekit (or maybe it's vite doing it) has a local websocket listener to trigger a refresh when you change somethig on the file system, the caddy server's TLS setup causes problems for that websocket connection. You will see an error like this repeat over and over in our browser console if you run unse &lt;code&gt;npm run dev&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftbonesteaks.gitlab.io%2Fblog%2Fimg%2Fwebsocket_error_with_local_tls.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftbonesteaks.gitlab.io%2Fblog%2Fimg%2Fwebsocket_error_with_local_tls.png" alt="Websocket Vite Error with localhost TLS setup"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have not taken the time to figure this out. Instead, what I do is &lt;code&gt;build&lt;/code&gt; and then run the app under &lt;code&gt;preview&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pnpm run build
pnpm run preview
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Putting it all together
&lt;/h2&gt;

&lt;p&gt;With this setup you will need to have three terminals running (and then a fourth to test with curl).&lt;/p&gt;

&lt;h3&gt;
  
  
  Terminal #1
&lt;/h3&gt;

&lt;p&gt;change into the supabase/docker directory. Then run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Terminal #2
&lt;/h3&gt;

&lt;p&gt;change into the sveltekit project directory. Then run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;caddy run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Terminal #3
&lt;/h3&gt;

&lt;p&gt;change into the sveltekit project directory. Then run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pnpm run build
pnpm run preview
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Terminal #4
&lt;/h3&gt;

&lt;p&gt;In this terminal we are going to verify that our caddy server properly matches on the supabase API paths.&lt;/p&gt;

&lt;p&gt;Run the below command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST https://localho.st:8888/auth/v1/signup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you should see this error returns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"No API key found in request"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you can go back to Terminal 1, you should see some output in the logs like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;supabase-kong      | 172.18.0.1 - - &lt;span class="o"&gt;[&lt;/span&gt;16/Dec/2021:15:36:37 +0000] &lt;span class="s2"&gt;"POST /auth/v1/signup HTTP/1.1"&lt;/span&gt; 401 45 &lt;span class="s2"&gt;"-"&lt;/span&gt; &lt;span class="s2"&gt;"curl/7.64.1"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't worry about not having the API key in your curl request. We will handle that in the website.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally, open the site in the browser
&lt;/h2&gt;

&lt;p&gt;Now head to &lt;a href="https://localho.st/" rel="noopener noreferrer"&gt;https://localho.st/&lt;/a&gt; and you should see the main sveltekit starter page.&lt;/p&gt;

&lt;h2&gt;
  
  
  We are all set!
&lt;/h2&gt;

&lt;p&gt;Next we will get basic email/password signup, login, and logout working. Stay tuned!&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>supabase</category>
      <category>authentication</category>
      <category>caddyserver</category>
    </item>
  </channel>
</rss>
