Skip to content

Commit

Permalink
🌒🎓 ↝ Adding graphql endpoint to allow for authentication & signing wi…
Browse files Browse the repository at this point in the history
…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
Gizmotronn committed Dec 29, 2022
1 parent bfd45cd commit 0b0df76
Show file tree
Hide file tree
Showing 46 changed files with 2,803 additions and 9,508 deletions.
2 changes: 1 addition & 1 deletion Server/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ python-dotenv = "*"
[dev-packages]

[requires]
python_version = "3.10"
python_version = "3.9.9"
162 changes: 81 additions & 81 deletions Server/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ def logout():
'none',
expires=datetime.utcnow() + timedelta(second = 5)
)
return res, 200
return res, 200

@app.route('/helloworld')
def helloworld():
return address
16 changes: 16 additions & 0 deletions Server/frontend/components/Navbar.js
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>
)
};
46 changes: 46 additions & 0 deletions Server/frontend/constants/lensConstants.js
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
}
}
}
}
`
94 changes: 94 additions & 0 deletions Server/frontend/context/lensContext.js
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>
);
}
Loading

0 comments on commit 0b0df76

Please sign in to comment.