DEV Community

Dominik Aksamit
Dominik Aksamit

Posted on

2 2

Deploy static vuepress site using now.sh and basic authentication

Vuepress is a minimalistic, vue-powered static site generator. Now is free and easy-to-deploy static hosting. Both used together let us deploy generated static sites for free in seconds.

So, what if we would like to quickly deploy our site and restrict access with username and password? Here, I'm gonna show you how to set up a project step by step.

TL;DR:
Repo: https://github.com/daksamit/vuepress-basic-auth
Demo: https://vuepress-basic-auth.now.sh (credentials: admin | admin)

Set up a new project

First of all, we need to create a project directory (vuepress-new-auth), init yarn project and add vuepress dev dependencies.

mkdir vuepress-now-auth
cd vuepress-now-auth
yarn init -y
yarn add -D vuepress
Enter fullscreen mode Exit fullscreen mode

For better dev experience, we add useful scripts to our package.json file.

"scripts": {
  "start": "vuepress dev src",
  "build": "rm -rf public && vuepress build src && mv src/.vuepress/dist public"
}
Enter fullscreen mode Exit fullscreen mode

Vuepress development

Let's create our first vuepress page. If src directory doesn't exist yet, we need to make it. Let's add heading to our README.md and start dev server.

mkdir src
echo "# Vuepress now auth" > src/README.md
yarn start
Enter fullscreen mode Exit fullscreen mode

Now deployment

If we don't have now installed yet, we can install it globally: yarn global add now. Then we have to sign in: now login
To deploy our site, we just need to run now and in a moment we have generated deployment links. Now our project is live :)

Basic Authentication

The only thing left to do is adding basic authentication to our vuepress project.
the deployed site is available for everyone. We can protect app by adding static-auth module, creating now.json and index.js in root working directory. Here is what we need to do:

Add now.json configuration file

// now.json

{
  "version": 2,
  "name": "vuepress-now-auth",
  "builds": [
    { "src": "index.js", "use": "@now/node" }
  ],
  "routes": [
    { "src": "/.*", "dest": "index.js" }
  ],
  "env": {
    "USERNAME": "admin",
    "PASSWORD": "admin"
  }
}
Enter fullscreen mode Exit fullscreen mode

Add static-auth module

yarn add static-auth
Enter fullscreen mode Exit fullscreen mode

add index.js for now server

// index.js

const protect = require('static-auth')
const route = '/'
const { USERNAME, PASSWORD } = process.env
const isAuthenticated = (user, pass) => (user === USERNAME && pass === PASSWORD)
const options = {
  directory: __dirname + '/public'
}
const app = protect(route, isAuthenticated, options)
module.exports = app
Enter fullscreen mode Exit fullscreen mode

Finally, just run yarn build && now --prod and enjoy :)

Of course, you can read more about vuepress and now by following links:

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (1)

Collapse
 
bakersbakebread profile image
Sean Thorne

For windows peeps:

"rmdir /s dist && vuepress build src && move src/.vuepress/dist dist"

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more