Coder Social home page Coder Social logo

fingerprintjs / fingerprintjs-vue-example Goto Github PK

View Code? Open in Web Editor NEW
4.0 10.0 2.0 427 KB

An example of how to implement FingerprintJS Pro in a Vue app.

JavaScript 18.87% Vue 60.85% EJS 20.28%
browser npm-module fingerprinting security vue vuejs browser-fingerprinting browser-fingerprint

fingerprintjs-vue-example's Introduction

๐Ÿ•ธ๏ธ THIS PROJECT IS ARCHIVED AND NOT MAINTAINED ๐Ÿ•ธ๏ธ

๐Ÿš™ USE THIS INSTEAD: fingerprintjs/fingerprintjs-pro-vue

FingerprintJS Pro working in Vue

Using create-vue-app:

First, create an app with the following command:

$ npx create-vue-app my-project

After the app is created, enter the directory:

$ cd my-project

and host a server locally:

$ yarn dev

If all goes well, you should be notified in terminal that your app is being hosted on port 4000. Any changes you make to the source files will automatically update.

Editing files

Inside the src/components folder, you will find a file called App.vue. This is the only file altered in order to use FingerprintJS Pro for this example.

  1. First, you will want to install the npm package for FingerprintJS. Go to the console and run:
$ npm install @fingerprintjs/fingerprintjs-pro --save

You can now import the package at the top of the script section:

// App.vue
import FingerprintJS from "@fingerprintjs/fingerprintjs-pro";
  1. Using Tokens.

If you assign your tokens to variables directly in the file, make sure to protect them by whitelisting the domains in the customer dashboard. Another option is to keep these tokens in a .env file and reference them from there. For the server API, it is recommended to use basic auth in request headers instead of using a token. You can read more about it here.

  1. Getting a visitor ID.

In order to get a visitor ID, use the FingerprintJS object:

// App.vue
mounted: function () {
    FingerprintJS.load({
      token: BROWSERAPIKEY,
    })
      .then((fp) => fp.get())
      .then((result) => {
        this.visitorId = result.visitorId;
      });
},

In this example, I am using the pre-defined mounted method inherited from the Vue class. This method will be called when the window is loaded, making the request for the visitorID automatic. The value is then stored to a class attribute called visitorId which can then be referenced.

  1. Querying the server API for visitor history:

The following function will query the server API. Please note that if your account is registered to the EU region, your base URL should be: https://eu.api.fpjs.io.

In the query below the visitorID and token is passed, as well as a "limitTo" variable, which will limit the amount of responses returned by the API. You can learn more about the query options here

// App.vue
methods: {
    // When the button is clicked, this function will
    // run and get visitor history from the server
    callServerAPI: function () {
      fetch(
        `https://api.fpjs.io/visitors/${this.visitorId}?limit=${limitTo}&token=${SERVERAPIKEY}`
      )
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          this.responseSummary = `Received history of ${data.visits.length} visits:`;
          data = JSON.stringify(data.visits, null, 4);
          this.serverData = data;
        });
    },
},

As you can see, a method has been saved for the class component and will be called from the onclick attribute in the html button :

<button v-on:click="callServerAPI">Get Visit History</button>

Further Steps

If you would like to learn more, please visit our Documentation to see best practices for using FingerprintJS and guides on how to implement them.

If you have any questions, please contact us at [email protected].

fingerprintjs-vue-example's People

Contributors

dcorso21 avatar valve avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.