<?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: ut0n</title>
    <description>The latest articles on DEV Community by ut0n (@ut0n69).</description>
    <link>https://dev.to/ut0n69</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%2F268828%2Fd06e7aa4-3421-4008-94c8-3b2c9a26dab1.png</url>
      <title>DEV Community: ut0n</title>
      <link>https://dev.to/ut0n69</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ut0n69"/>
    <language>en</language>
    <item>
      <title>How to add Basic Auth in Next.js apps with Firebase Hosting and SSR</title>
      <dc:creator>ut0n</dc:creator>
      <pubDate>Wed, 01 Apr 2020 09:51:04 +0000</pubDate>
      <link>https://dev.to/ut0n69/how-to-add-basic-auth-in-next-js-apps-with-firebase-hosting-and-ssr-4d8a</link>
      <guid>https://dev.to/ut0n69/how-to-add-basic-auth-in-next-js-apps-with-firebase-hosting-and-ssr-4d8a</guid>
      <description>&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Next.js&lt;/code&gt; has no method for Basic Auth. So we gonna use &lt;code&gt;Express.js&lt;/code&gt;.&lt;br&gt;
Also I'll use template: &lt;a href="https://github.com/zeit/next.js/tree/canary/examples/with-firebase-hosting-and-typescript"&gt;with-firebase-hosting-and-typescript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Require:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js v10.x.x&lt;/li&gt;
&lt;li&gt;firebase-tools@latest&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Steps
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Set up Next.js app
&lt;/h2&gt;

&lt;p&gt;We check this &lt;a href="https://github.com/zeit/next.js/blob/canary/examples/with-firebase-hosting-and-typescript/README.md"&gt;README&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# bash

yarn create next-app --example with-firebase-hosting-and-typescript with-firebase-hosting-and-typescript-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Install modules
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;express&lt;/li&gt;
&lt;li&gt;@types/express&lt;/li&gt;
&lt;li&gt;next-routes&lt;/li&gt;
&lt;li&gt;basic-auth-connect
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# bash

yarn add express next-routes basic-auth-connect
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;bash&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# bash

yarn add -D @types/express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Error handling
&lt;/h2&gt;

&lt;p&gt;(If you get this error)&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Error: &amp;gt; Couldn't find a `pages` directory. Please create one under the project root&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;We gonna fix &lt;code&gt;functions&lt;/code&gt;, because this template can't deploy firebase functions. (Apr 1st.2020)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/functions/index.ts

const app = next({ dev, conf: { distDir: 'next' } })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;↓&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/functions/index.ts

const app = next({ dev: false, conf: { distDir: 'next' } })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Develop
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/functions/index.ts

import * as functions from 'firebase-functions';
import next from 'next';
import express from 'express';

/* eslint-disable @typescript-eslint/no-var-requires */
const routes = require('next-routes');
const basicAuth = require('basic-auth-connect');

const USERNAME = 'user';
const PASSWORD = 'password';

const server = express();

const app = next({ dev: false, conf: { distDir: 'next' } });
const handler = routes().getRequestHandler(app);
server.use(basicAuth(USERNAME, PASSWORD));
server.get('*', (req, res) =&amp;gt; handler(req, res));

export const nextApp = functions.https.onRequest(server);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set up Firebase Project
&lt;/h2&gt;

&lt;p&gt;We gonna change this: &lt;code&gt;&amp;lt;project-name-here&amp;gt;&lt;/code&gt; to our firebase project name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# .firebaserc

{
  "projects": {
    "default": "&amp;lt;project-name-here&amp;gt;"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Run
&lt;/h1&gt;

&lt;h2&gt;
  
  
  At local
&lt;/h2&gt;

&lt;p&gt;We gonna use &lt;code&gt;firebase-emulators&lt;/code&gt; for testing on local.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build
&lt;/h3&gt;



&lt;p&gt;&lt;code&gt;yarn preserve&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Run
&lt;/h3&gt;



&lt;p&gt;&lt;code&gt;firebase emulators:start&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploy
&lt;/h2&gt;



&lt;p&gt;&lt;code&gt;firebase deploy&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Have fun ☺️&lt;br&gt;
Thanks.&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>firebase</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
