-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌒🎓 ↝ Adding graphql endpoint to allow for authentication & signing wi…
…th Lens Protocol We'll eventually redesign the layout of the post form (publication) to be in-line with the proposal layout in Signal-K/Silfur#25 User inputs & signature actions will be sent to the server as desired in Signal-K/Silfur#24 And of course, the user will now be authoring publications from the frontend, Signal-K/Silfur#22
- Loading branch information
1 parent
bfd45cd
commit 0b0df76
Showing
46 changed files
with
2,803 additions
and
9,508 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ python-dotenv = "*" | |
[dev-packages] | ||
|
||
[requires] | ||
python_version = "3.10" | ||
python_version = "3.9.9" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
import { ConnectButton } from "web3uikit"; | ||
|
||
|
||
export default function Navbar() { | ||
return ( | ||
<ul> | ||
<li>Home</li> | ||
<li>Create Proposal</li> | ||
<li> | ||
<div> | ||
<ConnectButton moralisAuth={false} /> | ||
</div> | ||
</li> | ||
</ul> | ||
) | ||
}; |
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,46 @@ | ||
import { ApolloClient, InMemoryCache, gql } from "@apollo/client"; | ||
|
||
const API_URL = 'https://api.lens.dev'; | ||
|
||
export const lensClient = new ApolloClient({ | ||
uri: API_URL, | ||
cache: new InMemoryCache(), | ||
}); | ||
|
||
export const challenge = gql` | ||
query Challenge($address: EthereumAddress!) { | ||
challenge(request: { address: $address }) { | ||
text | ||
} | ||
} | ||
`; | ||
|
||
// Provide access & refresh token to determine Lens profile ID of the authenticated address | ||
export const lensAuthenticate = gql` | ||
mutation Authenticate($address: EthereumAddress!, $signature: Signature!) { | ||
authenticate(request: { address: $address, signature: $signature }) { | ||
accessToken | ||
refreshToken | ||
} | ||
} | ||
`; | ||
|
||
export const getDefaultProfile = gql` | ||
query DefaultProfile($request: DefaultProfileRequest!) { | ||
defaultProfile(request: $request) { | ||
id | ||
} | ||
} | ||
` | ||
|
||
export const getFollowing = gql` | ||
query Query($request: FollowingRequest!) { | ||
following(request: $request) { | ||
items: { | ||
profile { | ||
id | ||
} | ||
} | ||
} | ||
} | ||
` |
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,94 @@ | ||
import { useState, useEffect, createContext, useContext } from "react"; | ||
import { challenge, lensClient, lensAuthenticate, getDefaultProfile } from '../constants/lensConstants'; | ||
import { useMoralis } from "react-moralis"; | ||
import { ethers } from "ethers"; | ||
|
||
export const lensContext = createContext(); | ||
export const useLensContext = () => { | ||
return useContext(lensContext); | ||
} | ||
|
||
export function LensProvider({ children }) { | ||
const [profileId, setProfileId] = useState(); | ||
const [token, setToken] = useState(); | ||
const { account } = useMoralis(); | ||
|
||
const signIn = async function () { | ||
try { | ||
const challengeInfo = await lensClient.query({ | ||
query: challenge, | ||
variables: { address: account }, | ||
}); | ||
|
||
const provider = new ethers.providers.Web3Provider(window.ethereum); | ||
const signer = provider.getSigner(); | ||
const signature = await signer.signMessage(challengeInfo.data.challenge.text); | ||
|
||
const authData = await lensClient.mutate({ | ||
mutation: lensAuthenticate, | ||
variables: { | ||
address: account, | ||
signature, | ||
}, | ||
}); | ||
|
||
const { | ||
data: { | ||
authenticate: { accessToken }, | ||
}, | ||
} = authData; | ||
setToken(accessToken); | ||
console.log(accessToken); | ||
} catch (error) { | ||
console.error("Error signing in, ", error); | ||
} | ||
}; | ||
|
||
const getProfileId = async function () { | ||
const defaultProfile = await lensClient.query({ | ||
query: getDefaultProfile, | ||
variables: { | ||
request: { | ||
ethereumAddress: account, | ||
}, | ||
}, | ||
}); | ||
|
||
if (defaultProfile.data.defaultProfile) {// Check to see if the connected wallet has a lens acct | ||
console.log(defaultProfile.data.defaultProfile.id) | ||
return defaultProfile.data.defaultProfile.id; | ||
} | ||
return null; | ||
} | ||
|
||
useEffect(() => { | ||
const readToken = window.localStorage.getItem("lensToken"); | ||
if (readToken) { | ||
setToken(readToken); | ||
} | ||
|
||
if (account && !token && !readToken) { | ||
signIn(); | ||
} | ||
|
||
if (!account) { | ||
window.localStorage.removeItem("lensToken"); | ||
} | ||
|
||
if (account) { | ||
getProfileId().then((id) => setProfileId(id)); | ||
} | ||
}, [account]); | ||
|
||
useEffect(() => { | ||
if (token) { | ||
window.localStorage.setItem("lensToken", token); | ||
} | ||
}, [token]); // Whenever the token changes | ||
|
||
return ( | ||
<lensContext.Provider value={{ profileId, token }}> | ||
{children} | ||
</lensContext.Provider> | ||
); | ||
} |
Oops, something went wrong.