Skip to content

Commit

Permalink
Merge pull request #13 from desci-labs/m0ar/fix-auth-race
Browse files Browse the repository at this point in the history
Fix did auth race, remove `did:key` auth from popup
  • Loading branch information
m0ar authored Sep 28, 2023
2 parents 5aafe5c + 7440408 commit 66f6fb3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
20 changes: 0 additions & 20 deletions components/AuthPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,6 @@ const AuthPrompt = () => {
const [isVisible, setIsVisible] = useState(true);
const clients = useCeramicContext();
const { ceramic, composeClient } = clients;
const isLogged = () => {
return localStorage.getItem("logged_in") == "true";
};

const handleOpen = () => {
if (localStorage.getItem("logged_in")) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};

const handleKeyDid = () => {
localStorage.setItem("ceramic:auth_type", "key");
setIsVisible(false);
authenticateCeramic(ceramic, composeClient);
};

const handleEthPkh = () => {
localStorage.setItem("ceramic:auth_type", "eth");
Expand All @@ -37,9 +20,6 @@ const AuthPrompt = () => {
<div className="popup">
<div className="popup-content">
<h2>Authenticate</h2>
<span>
<button onClick={handleKeyDid}>Key DID</button>
</span>
<span>
<button onClick={handleEthPkh}>Ethereum DID PKH</button>
</span>
Expand Down
1 change: 1 addition & 0 deletions components/ResearchObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ResearchObject = ({
<div className={styles.post}>
<div><big>{title}</big></div>
{ owner?.profile ? (<small>Author: {owner.profile.displayName}</small>) : <></> }
<br/>
<Link href={`https://ipfs.desci.com/ipfs/${manifest}`}>{manifest}</Link>
{children}
</div>
Expand Down
2 changes: 1 addition & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const MyApp = ({ Component, pageProps }: AppProps) => {
// Update to include refresh on auth
useEffect(() => {
if (doInitCheck) {
loadIfUninitialised(composeClient);
loadIfUninitialised(ceramic);
doInitCheck = false;
}
if (localStorage.getItem("logged_in")) {
Expand Down
22 changes: 17 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -15,10 +19,18 @@
"jsx": "preserve",
"incremental": true,
"paths": {
"@/*": ["./*"]
"@/*": [
"./*"
]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "template_data.json"],
"exclude": ["node_modules"],

"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"template_data.json"
],
"exclude": [
"node_modules"
]
}
16 changes: 10 additions & 6 deletions utils/populate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { fromString } from "uint8arrays/from-string"
import templateData from "../template_data.json"
import { ComposeClient } from "@composedb/client"
import { mutationCreateClaim, mutationCreateProfile, mutationCreateResearchObject } from "./queries"
import { CeramicClient } from "@ceramicnetwork/http-client"
import { definition } from '@/src/__generated__/definition'
import { RuntimeCompositeDefinition } from "@composedb/types"

const didFromSeed = async (seed: string) => {
const keyResolver = KeyDIDResolver.getResolver();
Expand All @@ -20,7 +23,13 @@ const didFromSeed = async (seed: string) => {
}

type ProfileIndexResults = { data: { profileIndex: { edges: []}}}
export const loadIfUninitialised = async (composeClient: ComposeClient) => {
export const loadIfUninitialised = async (ceramic: CeramicClient) => {
const composeClient = new ComposeClient(
{
ceramic,
definition: definition as RuntimeCompositeDefinition
}
)
const firstProfile = await composeClient.executeQuery(`
query {
profileIndex(first: 1) {
Expand All @@ -46,7 +55,6 @@ export const loadIfUninitialised = async (composeClient: ComposeClient) => {
* seed runs mutations to create instances of the data
**/
const loadTemplateData = async (composeClient: ComposeClient) => {
const originalDID = composeClient.did
for (const [seed, data] of Object.entries(templateData)) {
composeClient.setDID(await didFromSeed(seed))
const { profile, researchObjects, claims } = data
Expand All @@ -56,8 +64,4 @@ const loadTemplateData = async (composeClient: ComposeClient) => {
))
await Promise.all(claims.map(c => mutationCreateClaim(composeClient, c)))
}

if (originalDID) {
composeClient.setDID(originalDID)
}
}

0 comments on commit 66f6fb3

Please sign in to comment.