Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authentication #1041

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ TYPESENSE_SEARCH_ONLY_API_KEY="<TYPESENSE_SEARCH_ONLY_API_KEY>"
TYPESENSE_ADMIN_API_KEY="<TYPESENSE_ADMIN_API_KEY>"

MIXPANEL_PROJECT_TOKEN="<MIXPANEL_PROJECT_TOKEN>"

FLOW_NETWORK=testnet
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ docs/build/core-contracts/flow-nft/*
docs/ecosystem/overview/*

docs/.obsidian

# flow
imports
14 changes: 14 additions & 0 deletions docusaurus.config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { DocusaurusConfig } from '@docusaurus/types';

declare module '@generated/docusaurus.config' {
interface CustomFields {
flowNetwork: string;
}

interface CustomDocusaurusConfig extends DocusaurusConfig {
customFields: CustomFields;
}

const config: CustomDocusaurusConfig;
export default config;
}
10 changes: 10 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const { remarkCodeHike } = require('@code-hike/mdx');
const path = require('path');
const fs = require('fs');

const flowNetwork = process.env.FLOW_NETWORK || 'testnet';

const externalDataSourceLocation = './src/data/data-sources.json';
let cachedRepositories;

Expand Down Expand Up @@ -282,6 +284,10 @@ const config = {
label: 'Ecosystem',
activeBasePath: '/ecosystem',
},
{
type: 'custom-connectButton',
position: 'right',
},
{
href: 'https://github.com/onflow',
html: '<img src="" alt="GitHub" id="navbar-github" class="box-content h-32 w-32"/><span class="p-2 desktop:hidden">Github</span>',
Expand Down Expand Up @@ -637,6 +643,10 @@ const config = {
},
],
clientModules: [require.resolve('./src/modules/toolscards.ts')],

customFields: {
flowNetwork,
},
};

module.exports = config;
1 change: 1 addition & 0 deletions emulator-account.pkey
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x87c62162e859f621e11e74742aa66e7d3ad135d9dad476b753c50990a872f635
17 changes: 17 additions & 0 deletions flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"networks": {
"emulator": "127.0.0.1:3569",
"mainnet": "access.mainnet.nodes.onflow.org:9000",
"testing": "127.0.0.1:3569",
"testnet": "access.devnet.nodes.onflow.org:9000"
},
"accounts": {
"emulator-account": {
"address": "f8d6e0586b0a20c7",
"key": {
"type": "file",
"location": "emulator-account.pkey"
}
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@fortawesome/react-fontawesome": "^0.2.0",
"@headlessui/react": "1.7.13",
"@mdx-js/react": "^3.1.0",
"@onflow/fcl": "^1.13.1",
"@tailwindcss/aspect-ratio": "0.4.2",
"@tailwindcss/forms": "0.5.6",
"@tailwindcss/typography": "0.5.10",
Expand Down
10 changes: 3 additions & 7 deletions src/components/addNetworkButton.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import React, { useEffect, useState } from 'react';
import {
Button,
ButtonLink,
} from '@site/src/ui/design-system/src/lib/Components/Button/index';
import { Button } from '@site/src/ui/design-system/src/lib/Components/Button/index';

const targetChains = [
{
Expand Down Expand Up @@ -89,7 +86,6 @@ export const AddNetworkButton = (): JSX.Element => {
<Button
key={chain.id}
disabled={isNetworkAdded}
variant="secondary"
onClick={() => {
addFlowNetwork(chain).catch((e) => {
console.error(e);
Expand All @@ -103,8 +99,8 @@ export const AddNetworkButton = (): JSX.Element => {
))}
</div>
) : (
<ButtonLink variant="primary" href="https://metamask.io/download/">
<Button variant="primary" href="https://metamask.io/download/">
Install MetaMask
</ButtonLink>
</Button>
);
};
15 changes: 15 additions & 0 deletions src/components/connect-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { useCurrentUser } from '@site/src/hooks/use-current-user';
import { Button } from '@site/src/ui/design-system/src/lib/Components/Button';

const ConnectButton: React.FC = () => {
const { user, logIn, logOut } = useCurrentUser();

return (
<Button size="sm" className="mr-2" onClick={user.loggedIn ? logOut : logIn}>
{user.loggedIn ? 'Disconnect' : 'Connect'}
</Button>
);
};

export default ConnectButton;
18 changes: 18 additions & 0 deletions src/config/fcl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as fcl from '@onflow/fcl';
import config from '@generated/docusaurus.config';

const flowNetwork = config.customFields.flowNetwork;

console.log('Dapp running on network:', flowNetwork);

export function configureFCL(): void {
fcl.config({
'flow.network': flowNetwork,
'accessNode.api': flowNetwork ?
'https://rest-testnet.onflow.org' :
'https://rest-mainnet.onflow.org',
'discovery.wallet': `https://fcl-discovery.onflow.org/${flowNetwork}/authn`,
});
}
Comment on lines +9 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add WalletConnect 😉


configureFCL();
75 changes: 75 additions & 0 deletions src/hooks/use-current-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use client';

import { useEffect, useState } from 'react';
import * as fcl from '@onflow/fcl';
import { event } from '@site/src/utils/gtags.client';

interface FlowUser {
addr: string | null;
loggedIn: boolean;
}

interface UseCurrentUserReturn {
user: FlowUser;
logIn: () => Promise<void>;
logOut: () => void;
}

async function hashAddress(address: string): Promise<string> {
const encoder = new TextEncoder();
const data = encoder.encode(address);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((byte) => byte.toString(16).padStart(2, '0'))
.join('');
return hashHex;
}

export function useCurrentUser(): UseCurrentUserReturn {
const [user, setUser] = useState<FlowUser>({ addr: null, loggedIn: false });

useEffect(() => {
const unsubscribe = fcl.currentUser.subscribe(setUser);
return () => unsubscribe();
}, []);

const logIn = async (): Promise<void> => {
try {
await fcl.authenticate();

const snapshot = await fcl.currentUser.snapshot();
const userAddrSnapshot = snapshot?.addr;

if (userAddrSnapshot) {
const hashedAddr = await hashAddress(userAddrSnapshot);

event({
category: 'auth',
action: 'login',
label: `user_${hashedAddr}`,
value: 1,
});
} else {
event({
category: 'auth',
action: 'login',
label: 'unknown_user',
value: 1,
});
}
} catch (error) {
console.error('Error during login:', error);
}
};

const logOut = (): void => {
fcl.unauthenticate();
};

return {
user,
logIn,
logOut,
};
}
1 change: 1 addition & 0 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import HomePage from '../ui/design-system/src/lib/Pages/HomePage';
// import { refreshTools } from '../cms/tools.server'
import { externalLinks } from '../data/external-links';
import { contentNavigationListItems } from '../data/pages/home';
import '../config/fcl';

const data = {
discordUrl: externalLinks.discord,
Expand Down
7 changes: 7 additions & 0 deletions src/theme/NavbarItem/ComponentTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ComponentTypes from '@theme-original/NavbarItem/ComponentTypes';
import ConnectButton from '@site/src/components/connect-button';

export default {
...ComponentTypes,
'custom-connectButton': ConnectButton,
};
Loading
Loading