-
Notifications
You must be signed in to change notification settings - Fork 55
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
Closed
Add authentication #1041
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ea817ad
Configure FCL (#1026)
chasefleming 4697a61
Add FCL auth in nav (#1028)
chasefleming 63b34ba
Improve design system buttons (#1029)
chasefleming ee0dda1
Add event for auth
chasefleming 270d5ac
Merge branch 'main' into feature/gs
chasefleming 0e15413
Merge branch 'feature/gs' into cf/auth-event
chasefleming c1c8739
Merge pull request #1034 from onflow/cf/auth-event
nialexsan 7ee0735
Hash addr for ga (#1040)
chasefleming e7eb6c6
Add flow.json (#1039)
chasefleming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,6 @@ docs/build/core-contracts/flow-nft/* | |
docs/ecosystem/overview/* | ||
|
||
docs/.obsidian | ||
|
||
# flow | ||
imports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0x87c62162e859f621e11e74742aa66e7d3ad135d9dad476b753c50990a872f635 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
}); | ||
} | ||
|
||
configureFCL(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add WalletConnect 😉