Skip to content
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

feat(web3mail): Add API routes for storage ability #1

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ model User {
// Custom
walletAddress String? @unique
generatedMessage String?
protectedDataAddress String? @unique

twitterId String? @unique
twitterUsername String?
Expand Down
33 changes: 33 additions & 0 deletions src/app/api/mailing/retrieveProtectedDataAddress/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextResponse, NextRequest } from "next/server";
import { getServerSession } from "next-auth";
import { nextAuthOptions } from "@/app/api/auth/[...nextauth]/options";
import { prisma } from "@/lib/db";

export const GET = async () => {
const session = await getServerSession(nextAuthOptions);

// If not session return error
if (!session) {
return NextResponse.json({
success: false,
entries: 0,
error: "Twitter account not connected",
});
}

// Fetch user
const user = await prisma.user.findFirst({
where: {
id: session.user.id,
},
select: {
protectedDataAddress: true,
}
});
if (!user || !user?.protectedDataAddress) {
return NextResponse.json({ success: false, error: "User already has no protected data address" });
}

// Return success
return NextResponse.json({ success: true, address: user.protectedDataAddress });
}
50 changes: 50 additions & 0 deletions src/app/api/mailing/saveProtectedDataAddress/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { NextResponse, NextRequest } from "next/server";
import { getServerSession } from "next-auth";
import { nextAuthOptions } from "@/app/api/auth/[...nextauth]/options";
import { prisma } from "@/lib/db";

export const POST = async (request: NextRequest) => {
const session = await getServerSession(nextAuthOptions);

// If not session return error
if (!session) {
return NextResponse.json({
success: false,
entries: 0,
error: "Twitter account not connected",
});
}

// Retrieve body data
const data = await request.json();
const protectedDataAddress = data.protectedDataAddress;

// Ensure user has no protected data address registered
const userExists = await prisma.user.count({
where: {
id: session.user.id,
protectedDataAddress: null
},
});
if (!userExists) {
return NextResponse.json({ success: false, error: "User already has protected data address" });
}

// Ensure there is a protectedDataAddress
if (!protectedDataAddress) {
return NextResponse.json({ success: false, error: "Missing protected data address" });
}

// Update the user's protectedDataAddress
await prisma.user.update({
where: {
id: session.user.id,
},
data: {
protectedDataAddress
},
});

// Return success
return NextResponse.json({ success: true });
}
11 changes: 7 additions & 4 deletions src/app/app/mail/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client'
"use client";
import { type NextPage } from "next";

import React, { useEffect, useState } from 'react';
import { useSwitchChain, useChainId, useAccount } from 'wagmi'
import ExplanationCard from "@/components/mail/ExplanationCard";
Expand All @@ -9,13 +10,15 @@ import ProtectedDataBox from "@/components/mail/ProtectedDataBox";
import { type Address } from "@iexec/web3mail";
import { checkIsProtected, handleSubmitProtection, checkAppIsGrantedAccess } from "@/components/mail/utils/utils";


const Page: NextPage = () => {
const currentChainId = useChainId();
const { switchChain } = useSwitchChain();
const { address } = useAccount();
const userAddress = address as string;
const desiredChainId = 134; // For iExec sidechain.


const [protectedDataAddress, setProtectedDataAddress] = useState<string>('');
const [isProtected, setIsProtected] = useState(false);
const [hasJustRegistered, setHasJustRegistered] = useState(false);
Expand Down Expand Up @@ -63,7 +66,7 @@ const Page: NextPage = () => {
} catch (err) {
setError(`Failed to check protection status: ${(err as Error).message}`);
}
}
};

const handleSubmitProtectionWrapper = async (email: string) => {
try {
Expand All @@ -76,7 +79,7 @@ const Page: NextPage = () => {
} catch (err) {
setError(`Failed to submit protection: ${(err as Error).message}`);
}
}
};

const checkAppIsGrantedAccessWrapper = async () => {
try {
Expand Down Expand Up @@ -113,4 +116,4 @@ const Page: NextPage = () => {
);
};

export default Page;
export default Page;