Skip to content

Commit

Permalink
feat: add NextJS example
Browse files Browse the repository at this point in the history
  • Loading branch information
ilfa committed Apr 14, 2022
1 parent eda093b commit 863e3cb
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 228 deletions.
1 change: 1 addition & 0 deletions examples/next/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_FPJS_PUBLIC_API_KEY=<your-public-api-key>
5 changes: 4 additions & 1 deletion examples/next/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "next/core-web-vitals"
"root": true,
"extends": [
"next/core-web-vitals"
]
}
37 changes: 16 additions & 21 deletions examples/next/README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
## Setting up
In order to try out these examples, you set your public API key as the value of the NEXT_PUBLIC_FPJS_PUBLIC_API_KEY variable\
in the `.local.env`, you can just copy `.env` file, located in this directory.

## Getting Started
### FingerprintJS public API key
- Go to [FingerprintJS Dashboard](https://dashboard.fingerprintjs.com/)*
- Open the _API keys_ page from the sidebar
- Find your _Public_ API key

First, run the development server:
*If you don't have a FingerprintJS Pro account, [sign up for free](https://dashboard.fingerprintjs.com/signup/)).

```bash
npm run dev
# or
### Installing dependencies
1. Go to the root of the project, install dependencies and build it (`cd ../../ && yarn install && yarn build`)
2. Come back to the example folder (`cd examples/next`) and run `yarn install`.

After you've completed all the steps and inserted the API key, you can run:
```shell
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
Runs the app in the development mode.\
Open [http://localhost:3002](http://localhost:3002) to view it in the browser.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
11 changes: 6 additions & 5 deletions examples/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "next dev --port=3002",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@fingerprintjs/fingerprintjs-pro-react": "link:../../",
"next": "12.1.5",
"react": "18.0.0",
"react-dom": "18.0.0"
"react": "link:../../node_modules/react",
"react-dom": "link:../../node_modules/react-dom"
},
"devDependencies": {
"@types/node": "17.0.24",
"@types/react": "18.0.5",
"@types/react-dom": "18.0.0",
"@types/react": "^17.0.20",
"@types/react-dom": "^17.0.9",
"eslint": "8.13.0",
"eslint-config-next": "12.1.5",
"typescript": "4.6.3"
Expand Down
9 changes: 8 additions & 1 deletion examples/next/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import '../styles/globals.css'
import type { AppProps } from 'next/app'
import { FpjsProvider } from '@fingerprintjs/fingerprintjs-pro-react'

const fpjsPublicApiKey = process.env.NEXT_PUBLIC_FPJS_PUBLIC_API_KEY as string;

function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
return <FpjsProvider
loadOptions={{
apiKey: fpjsPublicApiKey
}}><Component {...pageProps} />
</FpjsProvider>
}

export default MyApp
13 changes: 0 additions & 13 deletions examples/next/pages/api/hello.ts

This file was deleted.

96 changes: 41 additions & 55 deletions examples/next/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,70 +1,56 @@
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import { useVisitorData } from "@fingerprintjs/fingerprintjs-pro-react"
import { useState } from "react"

const Home: NextPage = () => {
const [extendedResult, updateExtendedResult] = useState(false);
const {
isLoading,
error,
data,
getData,
} = useVisitorData({extendedResult}, {immediate: true});

const reloadData = () => {
getData({ignoreCache: true});
}

const onChangeExtendedResult = (e: React.ChangeEvent<HTMLInputElement>) => {
updateExtendedResult(e.target.checked)
}

return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<title>FingerprintJS Pro NextJS Demo</title>
<meta name="description" content="Check if fingerprintjs-pro-react integration works with NextJS SSR" />
<link rel="icon" href="/favicon.ico" />
</Head>

<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>

<p className={styles.description}>
Get started by editing{' '}
<code className={styles.code}>pages/index.tsx</code>
</p>

<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h2>Documentation &rarr;</h2>
<p>Find in-depth information about Next.js features and API.</p>
</a>

<a href="https://nextjs.org/learn" className={styles.card}>
<h2>Learn &rarr;</h2>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>

<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className={styles.card}
>
<h2>Examples &rarr;</h2>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>

<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h2>Deploy &rarr;</h2>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
<h1>FingerprintJS Pro NextJS Demo</h1>
<div className={styles.testArea}>
<div className={styles.description}>Lets load FingerprintJS Pro Agent using react integration and check next things:</div>
<ol className={styles.actionPoints}>
<li>There is no errors on server</li>
<li>There is no errors on client</li>
<li>In the field below visitor data was loaded</li>
<li>Try controls to test additional params</li>
</ol>
<div className={styles.controls}>
<button onClick={reloadData} type="button">Reload data</button>
<label>
<input type="checkbox" onChange={onChangeExtendedResult} checked={extendedResult}/>
Extended result
</label>
</div>
</main>

<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
<h4>VisitorId: <span className={styles.visitorId}>
{isLoading ? 'Loading...' : data?.visitorId}
</span></h4>
<h4>Full visitor data:</h4>
<pre className={styles.data}>{error ? error.message : JSON.stringify(data, null, 2)}</pre>
</div>
</div>
)
}
Expand Down
Binary file modified examples/next/public/favicon.ico
Binary file not shown.
4 changes: 0 additions & 4 deletions examples/next/public/vercel.svg

This file was deleted.

118 changes: 18 additions & 100 deletions examples/next/styles/Home.module.css
Original file line number Diff line number Diff line change
@@ -1,116 +1,34 @@
.container {
padding: 0 2rem;
}

.main {
min-height: 100vh;
padding: 4rem 0;
flex: 1;
padding: 100px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

.footer {
display: flex;
flex: 1;
padding: 2rem 0;
border-top: 1px solid #eaeaea;
justify-content: center;
align-items: center;
.testArea {
border: 2px solid #FF5D22;
box-shadow: 2px 2px 4px rgba(255, 93, 34, 0.6);
border-radius: 16px;
padding: 20px;
}

.footer a {
display: flex;
justify-content: center;
.controls {
display: grid;
grid-auto-columns: 150px;
grid-auto-flow: column;
gap: 20px;
justify-items: start;
align-items: center;
flex-grow: 1;
}

.title a {
color: #0070f3;
text-decoration: none;
}

.title a:hover,
.title a:focus,
.title a:active {
text-decoration: underline;
}

.title {
margin: 0;
line-height: 1.15;
font-size: 4rem;
}

.title,
.description {
text-align: center;
}

.description {
margin: 4rem 0;
line-height: 1.5;
font-size: 1.5rem;
}

.code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}

.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
}

.card {
margin: 1rem;
padding: 1.5rem;
text-align: left;
color: inherit;
text-decoration: none;
border: 1px solid #eaeaea;
border-radius: 10px;
transition: color 0.15s ease, border-color 0.15s ease;
max-width: 300px;
}

.card:hover,
.card:focus,
.card:active {
color: #0070f3;
border-color: #0070f3;
}

.card h2 {
margin: 0 0 1rem 0;
font-size: 1.5rem;
}

.card p {
margin: 0;
font-size: 1.25rem;
line-height: 1.5;
.actionPoints > li + li {
margin-top: 4px;
}

.logo {
height: 1em;
margin-left: 0.5rem;
.visitorId {
font-weight: normal;
}

@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
.data {
overflow: scroll;
}
16 changes: 14 additions & 2 deletions examples/next/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell',
'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
color: rgba(0, 0, 0, 0.87);
}

a {
Expand All @@ -14,3 +15,14 @@ a {
* {
box-sizing: border-box;
}

button {
padding: 8px 12px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
background-color: #FF5D22;
color: #fff;
border-radius: 4px;
border: none;
}
Loading

0 comments on commit 863e3cb

Please sign in to comment.