Code Monkey home page Code Monkey logo

netlify-plugin-vgs's Introduction

VGS Plugin for Netlify

This plugin streamlines your integration with VGS and Netlify by auto-configuring your VGS vault with the settings required to work with both systems.

Example project

Prerequisites

  1. VGS Dashboard account
  2. VGS CLI service account
  3. Netlify Account
  4. Netlify CLI

Overview

In this guide, we cover:

  • How to install and use vgs-netlify-plugin
  • How to set up a local development environment and work with netlify/VGS collect

Implementation Instructions

Configure Netlify

First, create a folder and initialize netlify site by running:

netlify init

Then, get clientId and clientSecret credentials by following this guide. Now, get the ID of your vault or vaultId, you can find it in the VGS Vault Dashboard

Finally, let's create environment variables on the Netlify side, so they will be available during each build

netlify env:set VGS_CLIENT_ID <clientId>
netlify env:set VGS_CLIENT_SECRET <clientSecret>
netlify env:set VGS_VAULT_ID <vaultId>

Configure your website

Create a simple src/index.html file with a form:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>VGS <-> Netlify Plugin demo</title>
  <link rel="stylesheet" href="style.css" />
</head>
<body>
  <form id="vgs-collect-form">
    <label>Email</label>
    <div id="email" class="form-field"></div>
    <label>Password</label>
    <div id="password" class="form-field"></div>
    <button type="submit">Log in</button>
  </form>
  <script src="https://js.verygoodvault.com/vgs-collect/2.14.0/vgs-collect.js"></script>
  <script type="module" src="app.js"></script>
</body>
</html>

Add some styles to src/style.css file

* {
  box-sizing: border-box;
}

html {
  font-size: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI";
}

iframe {
  width: 100%;
  height: 100%;
}

form {
  width: 400px;
  margin: 20px auto;
}

label {
  display: inline-block;
  font-size: 1.2rem;
  margin-bottom: .6rem;
  text-transform: uppercase;
}

.form-field {
  width: 100%;
  height: 4rem;
  position: relative;
  margin-bottom: 1.6rem;
  border-radius: 4px;
  box-shadow: 0 0 3px 0px #bcbcbc;
  padding: 0 10px;
  border: 1px solid transparent;
}

.form-field-group {
  display: flex;
  flex-flow: wrap;
}

.form-field-group div {
  flex: 0 0 50%;
}

.form-field-group div:first-child div {
  border-radius: 4px 0 0 4px;
  clip-path: inset(-3px 0px -3px -3px);
}

.form-field-group div:last-child div {
  border-radius: 0 4px 4px 0;
}

button[type="submit"] {
  width: 100%;
  color: white;
  background-color: #1890FF;
  margin-top: 1.6rem;
  text-transform: uppercase;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI";
  font-size: 1.4rem;
  height: 4rem;
  font-weight: 400;
  text-align: center;
  border: 1px solid transparent;
  padding: 0.375rem 0.75rem;
  border-radius: 0.25rem;
}

.form-field.vgs-collect-container__focused {
  border-color: #1890FF;
}

.form-field-group .form-field.vgs-collect-container__focused {
  clip-path: none;
}

and src/app.js, which initializes the collect.js library. Take a look at env vars:

  • process.env.VGS_VAULT_ID - it contains vaultId we've added to netlify previously
  • process.env.VGS_ROUTE_ID - this one will be created automatically by the plugin during deployment.
const vgsForm = VGSCollect.create(
  process.env.VGS_VAULT_ID,
  'sandbox',
  (state) => {}).setRouteId(process.env.VGS_ROUTE_ID);

const css = {
  boxSizing: 'border-box',
  fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI"',
  color: '#000000',
  '&::placeholder': {
    color: '#bcbcbc'
  }
};

vgsForm.field('#email', {
  type: 'text',
  name: 'login_email',
  placeholder: 'e.g. [email protected]',
  validations: ['required'],
  css
});

vgsForm.field('#password', {
  type: 'password',
  name: 'login_password',
  validations: ['required'],
  placeholder: 'password',
  css
});

document.getElementById('vgs-collect-form').addEventListener('submit', (e) => {
  e.preventDefault();
  vgsForm.tokenize((status, response) => {
    if (status === 200) {
      /**
       * Retrieve tokens from the response and send them to your server.
       */
      console.log(response);
    }
  }, (error) => {
    console.log(error);
  });
});

Now, create netlify.toml file with the following contents:

[build]
  publish = "dist/"
  command = "npm run build"

[dev]
  command = "npm start"

[[plugins]]
  package = "@vgs/netlify-plugin-vgs"

And the last thing, create a package.json file, it should contain:

  • the plugin itself @vgs/netlify-plugin-vgs
  • parcel to help with the build process
  • an entry point declaration "source": "src/index.html":
{
  "name": "netlify-plugin-example",
  "version": "1.0.0",
  "source": "src/index.html",
  "scripts": {
    "start": "parcel",
    "build": "parcel build"
  },
  "devDependencies": {
    "@vgs/netlify-plugin-vgs": "0.0.6",
    "parcel": "^2.6.2"
  }
}

and install dependencies by running

npm install

Now, your directory structure should look like this:

your-app
├─ ...
├─ src
| |─ index.html
| |─ style.css
| └─ app.js
├─ netlify.toml
└─ package.json

Start local server

Now, you are ready to start the development server, you can do it by running. It will build your app and open the browser with your running app at http://localhost:8888

netlify build && netlify dev

Deploy to production

This command will build and deploy your code to production. In the output you will find a public URL with your app.

netlify build && netlify deploy --production

netlify-plugin-vgs's People

Contributors

dmarynych avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.