Skip to content

Instantly share code, notes, and snippets.

@polluterofminds
Last active April 7, 2022 13:00
Show Gist options
  • Save polluterofminds/c7761b627b915c6b4c7b4848864ff53c to your computer and use it in GitHub Desktop.
Save polluterofminds/c7761b627b915c6b4c7b4848864ff53c to your computer and use it in GitHub Desktop.
Members Only Step 8
import { getSession } from "next-auth/react"
const API_ENDPOINT = 'https://managed.mypinata.cloud/api/v1'
const IDENTIFIER = '3d5468b3-f0ae-4a16-b726-6f09d009723b'
export default async function handler(req, res) {
const session = await getSession({ req })
if(req.method === "POST") {
try {
if(!session) {
return res.status(401).send("Not signed in");
}
// This is where we will upload our media
} catch (error) {
console.log(error);
res.status(500).send("Server error");
}
} else {
try {
if(!session) {
return res.status(401).send("Not signed in");
}
const query = `metadata[keyvalues]={"uuid":{"value":"${IDENTIFIER}","op":"eq"}}`
const imagesRes = await fetch(`${API_ENDPOINT}/content?${query}`, {
headers: {
"x-api-key": process.env.SUBMARINE_KEY
}
});
const imageIndex = await imagesRes.json()
let indexFile = [];
if(imageIndex.items.length > 0) {
const { id, cid } = imageIndex.items[0];
const body = {
timeoutSeconds: 3600,
contentIds: [id],
};
const tokenRes = await fetch(`${API_ENDPOINT}/auth/content/jwt`, {
method: "POST",
headers: {
"x-api-key": process.env.SUBMARINE_KEY
},
body: JSON.stringify(body)
});
const token = await tokenRes.json();
const dataRes = await fetch(`${process.env.GATEWAY_URL}/ipfs/${cid}?accessToken=${token}`);
indexFile = await dataRes.json();
}
res.status(200).json({imageIndex: indexFile});
} catch (error) {
console.log(error);
res.status(500).send("Server error");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment