Skip to content

Commit

Permalink
Merge pull request #3 from plats-network/fe/pontem
Browse files Browse the repository at this point in the history
Fe/pontem
  • Loading branch information
monnozero authored Oct 5, 2024
2 parents d41e15a + 89bdb25 commit 7835b33
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 87 deletions.
22 changes: 22 additions & 0 deletions frontend/src/app/(auth)/logout/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client";
import authApiRequest from "@/apiRequest/auth";
import { useRouter } from "next/navigation";
import React, { useCallback, useEffect } from "react";

const LogoutPage = () => {
const route = useRouter();

const handleLogout = useCallback(async () => {
await authApiRequest.logoutFromNextClientToNextServer(true).then((res) => {
console.log("logged out app");
route.push(`/login`);
});
}, [route]);

useEffect(() => {
handleLogout();
}, [handleLogout]);
return <div className="h-[100vh]"></div>;
};

export default LogoutPage;
11 changes: 8 additions & 3 deletions frontend/src/app/(landing)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export default function Home() {
const accessToken = cookieStore.get("accessToken");
return (
<>

<div className="relative h-[100vh] flex items-center justify-center">
<Header/>
{
!accessToken ? (
<div className="relative h-[100vh] flex items-center justify-center">
<div className="absolute bottom-0 left-0">
<div className="relative md:w-[488px] w-[200px] md:h-[360px] h-[150px]">
<Image
Expand Down Expand Up @@ -63,7 +65,10 @@ export default function Home() {
{/* )} */}
</div>
</div>

) : (
<ProfileView/>
)
}
</>
);
}
18 changes: 10 additions & 8 deletions frontend/src/components/ConnectAccountModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import WalletIcon from "@/assets/WalletIcom";
import { LoadingButton } from "@/components/ui/loading-button";
import { toast } from "@/hooks/use-toast";
import useClickOutside from "@/hooks/useClickOutside";
import { useWallet } from "@manahippo/aptos-wallet-adapter";
import { useRouter } from "next/navigation";
import React, { useRef, useState } from "react";

Expand All @@ -18,12 +19,13 @@ import React, { useRef, useState } from "react";
const ConnectAccountModal = ({
platId,
listAddress,
currentPublicKey

}: {
platId: string;
listAddress: any[];
currentPublicKey: any

}) => {
const { account} = useWallet();


const [isOpen, setIsOpen] = useState<boolean>(false);
Expand Down Expand Up @@ -56,12 +58,12 @@ const ConnectAccountModal = ({
const handleAddNewWallet = async () => {
try {
setIsLoadingWallet(true);
console.log("publicKey", currentPublicKey?.toBase58());
console.log("currentAddress", account?.address);
console.log("listAddress", listAddress);

if (!currentPublicKey) return;
if (!account?.address) return;
if (
listAddress.some((address) => currentPublicKey?.toBase58() === address)
listAddress.some((address) => account?.address === address)
// currentPublicKey === address
) {
toast({
Expand All @@ -72,8 +74,8 @@ const ConnectAccountModal = ({
return;
}
const data = {
public_key: Buffer.from(currentPublicKey.toBytes()).toString("base64"),
address: currentPublicKey?.toBase58(),
public_key: account?.publicKey,
address: account?.address,
};
console.log("🚀 ~ handleAddNewWal ~ data:", data);

Expand Down Expand Up @@ -102,7 +104,7 @@ const ConnectAccountModal = ({
className="bg-gradient-to-r from-[#8737E9] to-[#3AE7E7] rounded-xl w-full py-4 text-base font-bold text-center text-white cursor-pointer flex items-center justify-center gap-2"
>
<AddIcon />
<p> Add New Account</p>
<p> Add New Account </p>
</button>

{/* Main modal */}
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/components/ProfileView/ConnectedAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ConnectAccountModal from "@/components/ConnectAccountModal";
import { sliceAddressWallet, sliceAddressWalletUser } from "@/lib/helper";



import React, { useEffect, useState } from "react";

const ConnectedAccount = ({
Expand All @@ -17,11 +18,12 @@ const ConnectedAccount = ({
isFirstLoad: boolean;
}) => {

// Get the current public key from the wallet adapter

const [currentPublicKey, setCurrentPublicKey] = useState<any>(null)





return (
<div className="relative w-full z-10 lg:max-w-[455px]">
<div
Expand Down Expand Up @@ -92,11 +94,12 @@ const ConnectedAccount = ({
</p>
</div>
)}


<ConnectAccountModal
platId={user?.plat_id}
listAddress={user?.address}
currentPublicKey={currentPublicKey}

/>
</div>
</div>
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/components/ProfileView/ConnectedPlatsApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ const ConnectedPlatsApp = ({ platId }: { platId: string }) => {
type: "entry_function_payload",
function: ADD_PERMISSIONS_FUNCTION,
arguments: [
"nhatnguyen1.ID",
["be94da783e0ee11bbdb9de2f9c6745f15b1f9bfc3007dab7f77ec2eed2400b05"],
"0x998d7cca2dd5ebf2c0d1ff3000f5f0cd49d409a830ab488fb4436e7392480a29",
platId,
[appId],
],
type_arguments: [],
};
Expand All @@ -55,8 +56,9 @@ const ConnectedPlatsApp = ({ platId }: { platId: string }) => {
type: "entry_function_payload",
function: REVOKE_PERMISSIONS_FUNCTION,
arguments: [
"nhatnguyen1.ID",
["be94da783e0ee11bbdb9de2f9c6745f15b1f9bfc3007dab7f77ec2eed2400b05"],
"0x998d7cca2dd5ebf2c0d1ff3000f5f0cd49d409a830ab488fb4436e7392480a29",
platId,
[appId],
],
type_arguments: [],
};
Expand Down
84 changes: 43 additions & 41 deletions frontend/src/components/RegisterIdForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import authApiRequest from "@/apiRequest/auth";
import { toast } from "@/hooks/use-toast";
import { useState } from "react";
import { LoadingButton } from "@/components/ui/loading-button";
import { useWallet } from "@manahippo/aptos-wallet-adapter";



Expand All @@ -28,6 +29,7 @@ const formSchema = z.object({

const RegisterIdForm = ({ authenToken }: { authenToken: string }) => {
const router = useRouter();
const { connected, account, disconnect, wallets, select } = useWallet();


const [isLoading, setIsLoading] = useState<boolean>(false);
Expand All @@ -43,53 +45,53 @@ const RegisterIdForm = ({ authenToken }: { authenToken: string }) => {
// 2. Define a submit handler.
function onSubmit(values: z.infer<typeof formSchema>) {
console.log(values);
// handleRegister(values.platId);
handleRegister(values.platId);
}

// const handleRegister = async (platId: string) => {
// try {
// setIsLoading(true)
// if (!authenToken) return;
// if (!publicKey) return;
const handleRegister = async (platId: string) => {
try {
setIsLoading(true)
if (!authenToken) return;
if (!account?.publicKey) return;

// const data = {
// eoa: publicKey?.toBase58(),
// plat_id: platId+".ID",
// public_key: Buffer.from(publicKey.toBytes()).toString("base64"),
// };

// const response = await authApiRequest.register(data, authenToken);

// toast({
// className: "z-50 text-white",
// description: response.payload.msg,
// });
// if (response) {
// const responseLogin = await authApiRequest.login(authenToken);
// console.log("🚀 ~ handleRegister ~ responseLogin:", responseLogin)
const data = {
eoa: account.address,
plat_id: platId+".ID",
public_key: account?.publicKey,
};

const response = await authApiRequest.register(data, authenToken);

toast({
className: "z-50 text-white",
description: response.payload.msg,
});
if (response) {
const responseLogin = await authApiRequest.login(authenToken);
console.log("🚀 ~ handleRegister ~ responseLogin:", responseLogin)


// await authApiRequest.auth({
// accessToken: responseLogin.payload.data.access_token,
// });
// toast({
// className: "z-50 text-white",
// description: "Login successful",
// });
// router.push("/");
// router.refresh();
// }
// } catch (error) {
// setIsLoading(false)
// console.log("🚀 ~ handleRegister ~ error:", error)
// toast({
// variant: "destructive",
// className:"z-50 text-white",
// description: "Connect wallet failed",
// });
await authApiRequest.auth({
accessToken: responseLogin.payload.data.access_token,
});
toast({
className: "z-50 text-white",
description: "Login successful",
});
router.push("/");
router.refresh();
}
} catch (error) {
setIsLoading(false)
console.log("🚀 ~ handleRegister ~ error:", error)
toast({
variant: "destructive",
className:"z-50 text-white",
description: "Connect wallet failed",
});

// }
// };
}
};

return (
<div className="w-full h-full text-center flex flex-col gap-[120px] items-center justify-center ">
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/components/shared/WalletButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export default function WalletButton({
} catch (error) {}
};






return (
<>
<LoadingButton
Expand Down
50 changes: 23 additions & 27 deletions frontend/src/hooks/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,9 @@ const useLogin = () => {
if (!account?.publicKey) return;
console.log("currentPublicKey",account.publicKey)
// Get nonce
const publicKeyWallet = Buffer.from(account.publicKey.toString()).toString(
"base64"
);

console.log("publickey get Nonce base 64", publicKeyWallet);

const responseNonce = await authApiRequest.nonce(
encodeURIComponent(publicKeyWallet)
encodeURIComponent(account?.publicKey as string)
);
console.log("🚀 ~ handleGetNonce ~ responseNonce:", responseNonce)

Expand Down Expand Up @@ -114,8 +110,8 @@ const useLogin = () => {
if (!account?.publicKey) return;

const data = {
public_key: Buffer.from(account.publicKey.toString()).toString("base64"),
signature: Buffer.from(signature).toString("base64"),
public_key: account.publicKey,
signature: signature.toString(),
};

const responseVerify = await authApiRequest.verify(data);
Expand All @@ -125,25 +121,25 @@ const useLogin = () => {
className: "z-50 text-white",
description: responseVerify.payload.msg,
});
// if (responseVerify) {
// const responseLogin = await authApiRequest.login(
// responseVerify.payload.data.authen_token
// );

// if (responseLogin && responseLogin?.payload.code !== 400) {
// await authApiRequest.auth({
// accessToken: responseLogin.payload.data.access_token,
// });
// toast({
// className: "z-50 text-white",
// description: "Login successful",
// });
// router.push("/");
// router.refresh();
// } else {
// setAuthenToken(responseVerify.payload.data.authen_token);
// }
// }
if (responseVerify) {
const responseLogin = await authApiRequest.login(
responseVerify.payload.data.authen_token
);

if (responseLogin && responseLogin?.payload.code !== 400) {
await authApiRequest.auth({
accessToken: responseLogin.payload.data.access_token,
});
toast({
className: "z-50 text-white",
description: "Login successful",
});
router.push("/");
router.refresh();
} else {
setAuthenToken(responseVerify.payload.data.authen_token);
}
}
} catch (error) {
console.log("🚀 ~ handleVerifySignature ~ error:", error);
setIsLoading(false);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export const sliceAddressWalletUser = (addressUser: string) => {
export const sumBalances = (arr: any[]) => {
return arr.reduce((total, num) => {
const roundedNum = Number(num) < 0 ? 0 : Number(num);
return total + roundedNum;
return (total + roundedNum)/10**15;
}, 0);
};

0 comments on commit 7835b33

Please sign in to comment.